Repository navigation
fix(tmux): tmux 未回收 pane 进程时从内核取真实退出码,不再把 3/127 报成 1 - #2457
Merged
Merged
Conversation
tmux 只有 waitpid() 回收了 pane 进程之后才发布 pane_dead_status;CI 上偶发 tmux 一直没回收(pane 进程是 tmux 服务端的僵尸子进程,pane 一直读作「已死、 无码」),有界轮询等不到码,真实退出码就落成哨兵 1(#2453)。 僵尸进程的退出状态在 /proc/<pid>/stat 第 52 字段(waitpid 编码),只要它还是 tmux 服务端未回收的僵尸子进程,pid 就不会被占用,这个值就是 pane 自己的码。 探针确认尸体和收尾读两处的格式带上 pane_pid 与服务端 pid,读到「已死、无码」 时先问内核:拿到退出码立即采用,信号死亡立即判无码,否则照旧问 tmux。退出码 0、停止状态、非僵尸、父进程不对、读不到 /proc(macOS)都不采信。 回归测试用屏蔽 SIGCHLD 启动的 tmux 服务端确定复现「永不回收」:去掉修复时 exit 3 稳定报 1。 Co-Authored-By: Claude Opus 5.5 <[email protected]>
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
October 28, 2026 01:12
This was referenced Oct 28, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Owner
Author
|
已合入 a17dd9e(squash,标题带 #2457、正文为空)。合前复核:
follow-up:
|
This was referenced Oct 28, 2026
deepcoldy
pushed a commit
to darylchen97/botmux
that referenced
this pull request
Oct 28, 2026
…coldy#2446/deepcoldy#2457 退出码修复、deepcoldy#2426 读次上界) # Conflicts: # src/adapters/backend/tmux-pipe-backend.ts # src/worker.ts # test/tmux-pipe-backend.test.ts # test/tmux-pipe-exit-code.test.ts
This was referenced Oct 28, 2026
|
🚀 Released in v3.55.0 |
This was referenced Nov 7, 2026
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.
Closes #2453。
现象
#2446 之后,
test/tmux-pipe-exit-code.test.ts的真实退出码用例族在 CI 上仍会偶发把 CLI 的退出码报成 1(exit 3→ 1、127→ 1),见 #2453 里的两次 run。#2434 / #2446 处理的是「探针确认尸体时读码,收尾读不到时用锁存值」,但这次两边都没读到码。根因
在 ubuntu 24.04 + tmux 3.4 的容器里(
--cpus=2,8 个并发 worker 循环跑这几条用例,外加 6 个空转进程压满 CPU)复现了 3 次(约 900 次运行),每次都是同一个形状:#{pane_dead}:#{pane_dead_status}一直是1:,#{pane_dead_signal}也为空,持续到用例结束(约 18 秒);Zs <defunct>,ppid = tmux server)。tmux 只有在
waitpid()回收了 pane 进程之后才会发布pane_dead_status;在此之前 pane 读作「已死、无码」。正常情况下这段间隙只有几毫秒,现有的有界轮询(约 1 秒)足够;但这几次 tmux 一直没回收这个子进程,问 tmux 多少遍、等多久都拿不到码 —— 所以这不是超时长短的问题,调超时、重跑都不能根治。修法:读内核里已经有的那个码
僵尸进程的退出状态一直留在内核里:
/proc/<pid>/stat第 52 个字段exit_code(Linux ≥ 3.5,waitpid()编码),正是 tmux 回收时会读到的值。只要这个进程还是 tmux 服务端尚未回收的僵尸子进程,它的 pid 就不可能被别的进程占用,这个值就一定是这个 pane 自己的退出码。src/adapters/backend/unreaped-pane-exit.ts:parseUnreapedPaneExit(stat, serverPid)(纯函数):只在「状态是Z」且「父进程是这台 tmux 服务端」时才采信,按waitpid编码解出exited <code>或signaled <sig>;字段从最后一个)开始数,进程名里带空格或括号也不会错位;/proc(macOS);0x7f)不是退出,也不采信。resolvePaneDeath多一个可选参数unreapedExit。读到「已死、无码」的那一行时先问它:/proc。capturePaneDeath)和收尾读(readPaneDeath)两处的读取格式加上#{pane_pid}:#{pid}(pane 进程 pid、服务端 pid),并把上面的函数传进去。超时、轮询次数、重试逻辑都没动。影响面
TmuxPipeBackend(默认 tmux 会话)判定 pane 已死之后取退出码这一步,pane 存活/缺失判定、尸体清理、重连路径的逻辑不变。tmux 正常发布状态时行为与之前完全一样(多读的两个字段被忽略)。TmuxBackend(只读#{pane_dead})、PTY 后端、其它 CLI 适配器不涉及。/proc:一律「不知道」,回到原有行为。daemon 实际跑在 Linux。pipe-pane启动失败时给用户看的「立即退出」提示仍按 tmux 的状态显示,没改。回归测试
test/tmux-pipe-exit-code.test.ts新增一组,Linux + python3):用 python 屏蔽 SIGCHLD 后 exec tmux 起服务端。tmux 守护化时会恢复继承来的信号屏蔽字,于是它永远收不到子进程退出通知、永远不回收 pane —— 每次都是 test(tmux): #2446 之后 tmux-pipe-exit-code 仍把真实退出码报成 1(3/127 → 1) #2453 里那个尸体形状,不再靠负载碰运气。1:,pane 进程是 ppid = 服务端的Z进程(防止实验失效时下面两条假绿);exit 3→ 报{code: 3}。去掉修复后这一条稳定报{code: 1},正是 test(tmux): #2446 之后 tmux-pipe-exit-code 仍把真实退出码报成 1(3/127 → 1) #2453 的症状;kill -9的信号死亡仍走普通通道(报 1),尸体会话被清掉。resolvePaneDeath:未发布的码从内核取且不再轮询、传入的 pid 正确;信号死亡立即返回无码;内核不知道时继续问 tmux;tmux 已发布码、pane 存活时不读内核。test/unreaped-pane-exit.test.ts:退出码 / 信号(含 core dump 位)解码;进程名里有) S 1 (也不错位;非僵尸、父进程不对、缺第 52 字段、乱码、退出码 0、停止状态都不采信;路径是/proc/<pane pid>/stat;pid 缺失或读失败返回不知道;以及在当前内核上读一个真实的未回收子进程(python fork 后不回收),确认字段位置和编码,并确认换一个父进程 pid 就不采信。test/tmux-pipe-backend.test.ts:验证
bun run build通过。test/tmux*.test.ts21 个文件 +unreaped-pane-exit、transient-snapshot、session-env-backends、kill-worker-orphaned-backend、orphan-tmux-reaper,以及全仓护栏process-table-max-buffer、test-hygiene、npm-binary-distribution)在isolate-run里:29 个文件 615 例全绿。bun test --timeout=120000下 41 例全绿(与 CI 的 bun-test 腿同运行时)。isolate-run内逐个改、跑测试、还原)15 处全部变红:exit 3报 1(即 test(tmux): #2446 之后 tmux-pipe-exit-code 仍把真实退出码报成 1(3/127 → 1) #2453 的症状);)数字段、采信退出码 0、把停止当信号、信号值带上 core dump 位、读了服务端而不是 pane 的 stat、读失败当成退出。isolate-run,4 核;另起 6 个空转进程压满 CPU,8 个 worker 并发、各循环 6 轮,本机 tmux 3.3a):bun test跑 3 条真实退出码用例 + 3 条实验室用例,48 轮 288 例全部通过。修复前,tmux 3.4 容器里用同样的压法,约 900 次运行失败 3 次。偶发频率这么低,压测只能说明没引入回归;根治的证据是上面那组确定复现的实验室用例。同步 master
双审(head 891692b)之后:#2461(本 PR 首跑 bun-test 撞上的 voice 偶发 #2460 的根治)与 #2452(单元测试 4 分片)已合入,按规矩把 master 普通 merge 进来(无冲突),并补了上面那条探针支路的用例 → head c0df82e。master 合入 #2452 后的 run 40624827209 里 bun-test 又一次把
exit 77报成 1,正是本 PR 修的这个问题。参考:#2434、#2446(探针时锁存真实退出码)、#2445(读不到时延后再读)。
🤖 Generated with Claude Code