emscripten-core / emscripten-core/emscripten

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

Open
#26,708 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**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:**
```c
#include
#include
#include

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;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.