`read` on a FIFO whose writer closed blocks where Linux returns 0

未关闭
#524 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c, linux, macos

调研方向

Start with sys_read and io_xfer in src/syscall/io.c, then compare how their readiness wait handles a drained FIFO with no writer against the host behavior described here. The issue suggests trying a nonblocking read before waiting or retrying between bounded waits; investigate how either approach preserves guest syscall semantics. Done when the reproducer and FIFO stdin case return EOF instead of hanging, without regressing the pipe behavior noted in the issue.

由索引模型根据 Issue 内容生成。

描述

What happens

A guest read on a FIFO blocks for good once the last writer has closed and the FIFO is empty. Linux returns 0 (end of file). sys_read in src/syscall/io.c waits for readiness through io_xfer with POLLIN before it reads, and macOS reports nothing for such a FIFO: on the host, poll with POLLIN and a kqueue EVFILT_READ filter both time out on a drained FIFO with no writer, while a plain host read on it returns 0 at once. Linux reports POLLHUP there, so the wait is never reached.

Evidence

elfuse-probe-diff on the probe below, upstream/main a20dfab, Linux 6.18.63:

side result
Linux exit 0, bytes=1 last=0
elfuse killed by the probe's own alarm(3), status 142, no output

The same happens to a FIFO the shell passes as stdin: build/elfuse <probe> < fifo keeps running after the shell closes the only write end, where host cat < fifo exits.

Cause

Checked on the host side, not by tracing elfuse: after the writer closes and the data is read, macOS poll(POLLIN) returns 0 and a kqueue EVFILT_READ registration returns no event for the FIFO read end, while read returns 0. io_xfer therefore never sees the fd ready and never calls read. A guest pipe is not affected (the fork child in tests/test-registry-stale-pid.c sees EOF on its release pipe).

Reproduce

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    char path[] = "/tmp/fifo-eof-XXXXXX";
    if (!mkdtemp(path))
        return 2;
    char f[64];
    snprintf(f, sizeof f, "%s/f", path);
    if (mkfifo(f, 0600) != 0)
        return 2;
    pid_t pid = fork();
    if (pid == 0) {
        int w = open(f, O_WRONLY);
        write(w, "x", 1);
        usleep(200000);
        close(w);
        _exit(0);
    }
    int r = open(f, O_RDONLY);
    alarm(3);
    char b[8];
    ssize_t n, total = 0;
    while ((n = read(r, b, sizeof b)) > 0)
        total += n;
    printf("bytes=%zd last=%zd\n", total, n);
    waitpid(pid, NULL, 0);
    unlink(f);
    rmdir(path);
    return 0;
}

Possible fix

Before parking in the readiness wait on a FIFO, try the read once without blocking: read returning 0 with no writer left is end of file and goes back to the guest. Or wait in bounded slices and retry the read between them.

Origin

Found while answering a review on #521: jserv's suggestion to end the hold process on EOF from a FIFO (https://github.com/sysprog21/elfuse/pull/521#discussion_r4558628935) depends on it. Related: #424, #425 (pipe hangup reporting in poll and epoll).

主要语言
C
星标
287
派生
33
平均合并
2 天 17 小时
30 天内合并 PR
18

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

sysprog21/elfuse 的其他 Issue

查看 sysprog21/elfuse 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。