Repository navigation
fix(pty): 读端收尾前先读完 PTY 里剩下的输出,子进程退出前的最后几行不再丢失 - #2154
Merged
Merged
Conversation
- Node:libuv 在 epoll 报 POLLHUP 且一次读未读满时直接判 EOF,PTY 内核里排队的输出被丢;EOF 前先把 master 读干 - 两种运行时:node-pty 在子进程退出 200ms 后强制销毁读端,事件循环停顿时会丢掉未读输出;销毁前先读干,Bun 读循环有在途 read 时等它落地再销毁 - 回归用例:exit-burst(超出 n_tty 单次读量后立即退出)、exit-stall(退出时注入停顿、后台进程占着终端) - child-env 的 node-pty 用例改走生产包装,去掉 exit 后的 250ms 宽限 Refs #2153 Co-Authored-By: Claude Opus 5.5 <[email protected]>
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
October 22, 2026 10:09
Owner
Author
|
已合并 → 8c8f232(squash)。
follow-up(均不阻断):
未做:发版、部署(需另行指令)。 |
This was referenced Oct 22, 2026
|
🚀 Released in v3.50.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2153
Fixes #2152
改了什么
src/utils/pty.ts的 Unix 包装(Node 与 Bun 都走):PTY 读端收尾时,先用非阻塞readSync把 master 里剩下的输出读完,再放行收尾。push(null)):Node 下 libuv 只要看到 POLLHUP 且这次没读满,就直接判 EOF,不会再读。但 PTY 的 n_tty 每次最多只交出 4095 字节,子进程最后的输出可能还排在内核里,读端一收尾就丢了。destroy()之前:node-pty 在子进程退出 200ms 后会强制销毁读端。事件循环那一刻停顿的话,没读走的输出会被丢掉。Bun 的读循环如果正好有一次异步 read 在途,就先等它返回再销毁,否则这次 read 读到的数据会被socket.destroyed判断直接丢弃。测试:
test/pty-reader-compat.test.ts新增两条用例,Node 和 Bun 各跑一遍:exit-burst:子进程写出 16KB(单次读量的 4 倍)后立即退出,退出前读端一次都没读。exit-stall:一个忽略 SIGHUP 的后台进程占着终端;在 node-pty 挂上收尾定时器的那一刻,注入超过其延时的事件循环停顿。断言输出完整,并且退出事件在后台进程还活着时就到达(收尾没有被推迟卡住)。test/child-env.test.ts的 node-pty 用例改走生产包装,env 走的还是同一条 native spawn 路径;同时去掉 exit 后的 250ms 宽限期,改为在 exit 时直接判定。这条用例 10-13 也因为同一根因红过(got "R=UNSET\r\n")。为什么
pty-reader-compat第 40 行(expected 'READY31 99\r\n' to contain 'reply:hello')在 CI 上偶发失败,已出现多次,最近一次挡住了 #2089,详见 #2153。这不是测试搭法的问题,而是生产包装在子进程退出时会丢掉它最后写出的输出。strace 显示:epoll报EPOLLIN|EPOLLHUP→read(…, 65536) = 4095→ 紧接着close(fd)。没有放宽任何超时,也没有在测试里加等待。影响面
src/utils/pty.tsspawn 的终端,包括 CLI 会话(PtyBackend)、program-status relay、宿主网页终端、zellij/herdr attach,以及 worker 内的 tmux/zellij attach。改动只发生在读端收尾那一刻;读取热路径、写入、resize、退出码都不变。数据仍然先于onExit交付。pause()着,Readable 缓冲里的数据仍会随 destroy 一起丢弃,这和改动前一样。CLI 会话(PtyBackend)不会 pause 终端;program-status relay 和宿主网页终端做流控时会 pause,这两处在暂停中收尾的行为与改动前一致。验证
bun run build、tsc --noEmit:通过vitest run --project unit test/pty-reader-compat.test.ts:14/14;bun test test/pty-reader-compat.test.ts:14/14vitest run --project unit test/child-env.test.ts:43/43pty.ts同样失败,原因是本机 worktree 路径长,与本改动无关。前 5 个文件在 bun test 下全绿,后 2 个用了vi.hoisted,按 runner 规则只在 vitest 跑。pty-reader-compat的 vitest 50/50、bun test 50/50;child-env的 node-pty 用例 vitest 50/50exit-burst在 Node 下只收到 4095/16388 字节(3/3 次);exit-stall在 Bun 下丢尾巴(20/20 次)pty.ts全量测试交给 CI。
🤖 Generated with Claude Code