Skip to content

read() on a pipe returns -1 instead of 0 after the write end is closed #26708

@Ryan2026R

Description

@Ryan2026R

Version of emscripten/emsdk:

5.0.6

After closing the write end of a pipe(), read() on the read end should return 0 to signal EOF. Under Emscripten it returns -1 with errno=EAGAIN, potentially breaking pipe-based producer/consumer loop that waits for EOF.

Failing command line in full:

$ emcc -O0 -Wall pipe_eof.c -o a.out.js
$ node a.out.js
read(pipe_read after close_write): ret=-1 errno=6

The same program on native Linux prints read(pipe_read after close_write): ret=0 errno=0.

Example code snippet:

#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int main(void) {
    int pipefd[2];
    char buf[16];
    pipe(pipefd);
    close(pipefd[1]);                          // close write end

    errno = 0;
    ssize_t r = read(pipefd[0], buf, sizeof(buf));
    printf("read(pipe_read after close_write): ret=%zd errno=%d\n", r, errno);
    close(pipefd[0]);
    return (r == 0) ? 0 : 1;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions