Skip to content

eventfd(EFD_SEMAPHORE) read returns full counter instead of 1 #6145

Description

@artemyarulin

In WASIX, eventfd created with EFD_SEMAPHORE does not follow Linux semantics: each read should return 1 and decrement the counter by one. Our implementation decrements the counter correctly, but instead of returning 1 it returns the current counter value, which breaks counting‑semaphore expectations and can cause incorrect behaviour in runtimes that rely on that.

#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/eventfd.h>
#include <unistd.h>

int main(void) {
    // Semaphore with 4 permits
    int fd = eventfd(0, EFD_SEMAPHORE | EFD_NONBLOCK);
    uint64_t four = 4;
    write(fd, &four, sizeof(four));

    uint64_t out = 0;
    for (int i = 0; i < 4; i++) {
        assert(read(fd, &out, sizeof(out)) == 8);
        // Expected: semaphore mode returns 1 per read.
        if (out != 1) {
            printf("BUG: expected 1, got %" PRIu64 "\n", out);
            return 1;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions