编程 EPOLLET 的边沿不是「有数据」:pipe 非空再写在内核 5.5–5.13 不唤醒 ET 读者

2026-11-30 00:06:24

EPOLLET 的边沿不是“有数据”:pipe 非空再写在内核 5.5–5.13 不唤醒 ET 读者

线程停在 epoll_wait,缓冲区还剩 1KB

典型现场是:读端 rfd 用 EPOLLIN | EPOLLET 注册到 epoll,epoll_wait 返回过一次,read(rfd, buf, 1024) 返回 1024,但 pipe 里还有 1024 字节没读。线程没有继续 read 到 EAGAIN,直接回到 epoll_wait,然后永久阻塞。应用层没有日志,因为线程没走到业务回调;strace 里能看到最后一次 epoll_wait 不再返回,ioctl(fd, FIONREAD, &avail) 仍返回 avail == 1024。这就是 ET 事件已经被消费,但缓冲区没有读空。

边沿是状态变化,不是“有数据”

man epoll(7) 给的例子很直接:rfd 是 pipe 读端,注册到 epoll;写端写 2KB;epoll_wait 返回 rfd 就绪;读端只读 1KB;再次 epoll_wait。若 rfd 用 EPOLLET 注册,第 5 步大概会挂住,尽管缓冲区里还有 1KB。原因是 ET 只在被监视 fd 发生变化时递送事件:第 2 步的写产生的事件在第 3 步被消费,第 4 步的读没有消费完整个缓冲区,第 5 步就可能无限期阻塞。

所以判据不是“缓冲区里有数据”,而是“状态是否发生了变化”。Linus 在 linux-kernel 回复里把问题说得很清楚:“An edge is not 'somebody wrote more data'. An edge is 'there was no data, now there is data'.” 同一回复里也指出,level-triggered signal 是 “there is data”。

ET 下的正确用法是:使用非阻塞 fd;收到事件后把 fd 视为就绪,直到下一次非阻塞 read/write 返回 -1 且 errno == EAGAIN(或 EWOULDBLOCK)之前,都不要回 epoll_wait 等新事件。对数据报/令牌类文件,唯一判断方式是读到 EAGAIN;对流式文件(pipe、FIFO、stream socket)也可以用 read 返回字节数小于请求量来判断已读空,但如果不能保证总是流式文件,不要用这个技巧。

EPOLLONESHOT 解决的是另一个问题:fd 收到一次事件后被禁用,之后必须由调用者用 epoll_ctl(EPOLL_CTL_MOD) 重新 rearm。多个线程阻塞在同一 epoll fd 上时,ET 就绪只会唤醒一个线程,避免惊群;EPOLLONESHOT 可以进一步保证同一 fd 不会被多个线程同时处理。

5.5 到 5.13:pipe 非空时 write 不产生 ET 唤醒

2019 年底 David Howells 改 pipe 代码,导致构建变慢/挂住,Linus 随后做了修复。5.5 内核(2020 年 1 月发布)包含该改动:pipe_write() 只在操作开始时 pipe 为空的情况下才唤醒等待者;如果 pipe 里已有待读数据,写入只追加数据、不唤醒。理由是轮询系统调用在已有数据时会立即返回,因此不该有等待者。

但 EPOLLET 模式不同:请求边沿触发的进程在有数据时不会立刻从 epoll_wait 返回,而是等状态变化。于是 5.5 起,在已有数据时发起 ET 等待的进程看不到“边沿”,不会唤醒。

此前 pipe_write() 里有一段注释:“Always wake up, even if the copy fails. Otherwise we lock up (O_NONBLOCK-)readers that sleep due to syscall merging. FIXME! Is this really true?” 事实证明是真的。多个 Android 库(如 Realm)依赖“即便 pipe 里已有数据,新写入也产生 ET 唤醒”的行为:它们等到缓冲区满再一次性读完。这些库在 5.10 进入 Android 12 beta 后崩溃/失效。

Sandeep Patil 的补丁说明围绕 commit 1b6b26ae7053(pipe: fix and clarify pipe write wakeup logic)描述这段逻辑:把 pipe 写逻辑改成只在 pipe 为空时唤醒读者。补丁 fs/pipe.c 是 10 insertions(+), 9 deletions(-)。realm-core issue #4666 把 5.5 的改动记为 6a965666b7e7(“Don't wake up readers on a pipe if there was already data in it when we added more.”):external_commit_helper 用 pipe 做信号机制,该改动使其失效;除非把 pipe 读空否则不触发通知,但读空就丢掉了多进程通知。程序在 5.4 上正常,在 5.10 上失败,5.5 大概率也失败。作者本人也说程序用 EPOLLET 的方式是错的,但它以前能用。

修复在 2021 年 7 月底合并进 5.14-rc4,并进入 5.10.56 和 5.13.8 稳定版:并不会完全恢复旧行为,而是每次写操作只执行一次唤醒。后续副作用是 Intel 自动化测试报告 hackbench 回归约 13%;Linus 表示不确定 hackbench 的意义,只在与轮询函数配合时提供更接近旧行为的行为。

排查时先看 read 返回值,不要先猜内核

  1. strace -f -e trace=epoll_wait,read,write,epoll_ctl 看最后一次 epoll_wait 是否超时返回 0,以及此前 read 是返回 1024,还是返回 -1/EAGAIN。如果 read 返回 1024 而请求是 2048,说明没读空。
  2. 在 read 前用 ioctl(fd, FIONREAD, &avail) 看剩余字节。若 avail > 0 而 epoll_wait 不再返回,基本就是 ET 事件已消费、缓冲区未读空。
  3. 检查 read 返回值大小:n == 1024 小于请求的 2048,只能说明还有数据;不要把短读当成“本次事件处理完”,ET 下要读到 -1/EAGAIN 才退出 drain。
  4. 检查分支是否漏掉 EAGAIN/EWOULDBLOCK:很多代码在 n > 0 后直接回 epoll_wait,没有 for(;;) 到 EAGAIN,这就是根因。

修复:读到 EAGAIN,再统一 rearm

drain 循环按 read 返回值分三类:n > 0 继续读;n == 0 是 EOF;`n 0) {
/* 处理 n 字节 /
continue;
}
if (n == 0) {
/
EOF:pipe 写端已全部关闭 /
return 0;
}
if (errno == EINTR) {
continue;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 1; /
已读空,可以回 epoll_wait */
}
return -1;
}
}


`EPOLLONESHOT` 下,事件处理完统一收尾 rearm,不要在 drain 循环中途 rearm:

```c
/* EOF/错误路径走 EPOLL_CTL_DEL */
struct epoll_event rearm = {
.events = EPOLLIN | EPOLLET | EPOLLONESHOT,
.data.fd = fd,
};
if (epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &rearm)
#include
#include
#include
#include
#include
#include
#include

static void die(const char *msg) {
perror(msg);
exit(1);
}

static void set_nonblock(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
if (flags  %zd\n", n);

int rc = epoll_wait(epfd, &ev, 1, 1000);
printf("epoll_wait #1 -> %d\n", rc);
if (rc != 1) return 2;

n = read(p[0], buf, 1024);
if (n != 1024) die("read 1024");
int avail = -1;
if (ioctl(p[0], FIONREAD, &avail)  %zd, FIONREAD=%d\n", n, avail);

n = write(p[1], buf, 1024);
if (n != 1024) die("write 1024");
printf("write 1024 while pipe non-empty -> %zd\n", n);

rc = epoll_wait(epfd, &ev, 1, 1000);
if (rc == 0) {
printf("epoll_wait #2 -> 0 (timeout): ET reader not woken\n");
return 0;
}
if (rc  %d (woken)\n", rc);
return 0;
}

编译:cc -Wall -O2 repro.c -o repro。如果内核已经修复,第二次 write 会唤醒一次,但不要依赖这个行为;正确做法仍然是读到 EAGAIN 再回 epoll_wait。

参考

推荐文章

程序员茄子在线接单