Repository navigation
fix(tmux): reap orphaned tmux servers so restored sessions don't lock against the previous generation - #2149
Conversation
A hard daemon kill/upgrade leaves the shared tmux server running. When the
new daemon's tmux traffic cannot reach it (probe timeout under load, socket
path cleaned from /tmp, …), a SECOND server is born and rebinds the default
socket path; the old server keeps its unlinked socket and becomes
unreachable — `tmux list-sessions` can never see it again, so botmux can
neither manage nor kill it or its managed panes.
On session restore the new daemon cold-resumes every restored CLI inside the
fresh server. The previous generation's long-lived CLIs (codex …) are still
alive inside the displaced server holding each session's per-session file
lock (~/.codex/thread-writer-locks/<id>.lock), so the resumed CLI cannot
acquire it and parks on:
🔒 This conversation is open in another app
Close it there and press R to continue here.
Measured in production (2026-10-22, botmux 3.48 / tmux 3.6a): 19/48
restored sessions blocked after one restart; the orphan tree leaked ~13 GB
RSS and a few CLIs kept burning tokens on forked sub-threads.
reapOrphanWorkers() only reaps `__worker` processes; it deliberately never
touches the tmux server they used, so this gap was unhandled.
This adds ensureServerThenReapOrphanTmuxServers(), invoked at daemon
startup right after reapOrphanWorkers() and BEFORE session restore:
1. pin/identify the reachable server with a disposable per-pid bootstrap
tmux session (bare `tmux start-server` cannot pin: with the default
exit-empty=on it exits on client disconnect);
2. census unix sockets bound to the default socket pathname
(/proc/net/unix + /proc/<pid>/fd on Linux, lsof -U on macOS);
3. any OTHER tmux process listening on that pathname is a displaced,
unreachable server — SIGTERM it (orderly shutdown releases the managed
CLIs' file locks, so resumed sessions recover immediately).
Conservatism:
• reaps nothing when no reachable server can be positively identified
(timeout/spawn failure/ECONNREFUSED — a healthy server's accept backlog
can overflow) — "can't tell" never escalates into a kill;
• only reaps processes provably botmux-spawned (booting argv carries
`new-session -s bmx-<id>`/`bmx-diag-<id>`, or a descendant pane carries
BOTMUX_MANAGED_SHELL=1); a user's own interactive tmux is never touched;
• TOCTOU re-probe of the reachable pid before signaling;
• cross-daemon safe (per-bot daemons share the socket; unique bootstrap
names, racing peers just get ESRCH);
• BOTMUX_DISABLE_ORPHAN_TMUX_REAP=1 opt-out.
Includes unit tests (identification, false-positive guards, parsers) and
verified live: healthy host reaps 0 with no residue; a real displaced server
in an isolated TMUX_TMPDIR is detected and cleaned with its child panes.
Co-Authored-By: Claude Code <[email protected]>
|
自动评审初步意见(最终以维护者审阅为准)。 整体认可这个修复的方向和保守策略:无可达 server 不杀、TOCTOU 复核、botmux 指纹、opt-out 都到位。我在 Linux(tmux 3.3a,daemon 实际运行平台)用隔离 有 2 个 P2 建议在合入前处理: P2-1|bootstrap 自己 boot 出来的 server,下一代会漏识别(已实测复现)
实测:用 建议把 bootstrap 名字纳入指纹,例如: // bmx(?:-diag)?-[0-9a-f]{8} → (?:bmx(?:-diag)?-[0-9a-f]{8}|bmx-reap-bootstrap-\d+)并补一条对应用例。 P2-2|SIGTERM 后忽略 SIGHUP 的 pane 会存活,锁不一定释放(已实测) tmux server 收到 SIGTERM 的有序退出只向 pane 进程组发 SIGHUP,没有 SIGKILL 兜底。实测:pane 直接是 建议:发信号前快照 P3(不阻断,供参考):
验证记录: |
- 指纹正则补 bmx-reap-bootstrap-<pid>:reaper 在空 socket 上 boot 出的
server,cmdline 永久停在 bootstrap 名(后续真会话 attach 不改 server
argv),旧正则不匹配;实测该形态 server 被 displaced 后 REAPED=0 漏网,
本次恢复反而造出下一代回收不掉的孤儿
- 删除 managed-pane 后裔 argv 指纹:BOTMUX_MANAGED_SHELL=1 经 /usr/bin/env
前缀注入,execve 后只在 environ 不在 argv,而 listProcesses 读的是
/proc/pid/cmdline(生产 141 个 pane 零命中),该分支永不成立,是虚假
安全感;用户会话 boot 的 default server 即使后来挂了 bmx pane 也不杀,
避免连带终止用户自己的会话
- SIGTERM 后增加 3s 有界存活等待:信号返回不等于 pane 已关、锁已释放;
超时仅告警且不计入回收数,不升级 SIGKILL(忽略 SIGHUP 的 pane 由恢复
路径的 session-scope teardown 独立兜底)
- TOCTOU 二次探针与存活探针 seam 化
- 测试:修 SOCK 写死 {} 未跟随 tmux fence(TMUX_TMPDIR)导致 vitest
unit project 下 2 例必红的假绿;补 bootstrap 指纹、非 tmux 二进制闸门、
uid 守卫(记录式 kill)、有界等待超时、TOCTOU 用例
Co-Authored-By: Claude Code <[email protected]>
daemon 启动自动 SIGTERM tmux server 属于自动杀进程,按合并标准必须默认 关闭、由运维显式开启,不能只有默认开启的环境变量 opt-out: - 开关位置:Dashboard「全局设置 → 实验性配置 → 回收上一代遗留的 tmux 服务进程」,中英双语 help 说明语义、风险、紧急停用方式 - global-config dashboard 段新增 reapOrphanTmuxServers,normalize 白名单 逐字保留显式 false;config 增加 live getter(absent/非法值 = 关), 与既有实验性开关一致 - daemon 门禁抽成 orphanTmuxReaperEnabled(backendType, enabled) 纯函数: 仅 tmux 后端且开关打开才执行;BOTMUX_DISABLE_ORPHAN_TMUX_REAP=1 保留 为独立的紧急 kill-switch - settings 写入链路补类型/校验/错误码(invalid_reapOrphanTmuxServers) - 测试:开关默认关闭/实时读写/非法值丢弃,写入校验双向用例,门禁四象限 Co-Authored-By: Claude Code <[email protected]>
追加提交说明 + 一个需要你来改的核心问题(#2107 后的 socket 对齐)感谢这个 PR,方向和保守策略两轮评审都认可。我(维护者侧)已在你的分支上追加了两个 commit(你的原始提交原样保留,只多了一个 merge master 的 merge commit + 两个新 commit,没有 force-push、没有改写你的提交):
需要你来改:P1 — reaper 必须覆盖 #2107 的独立 socket(当前 PR 在现网等于空转)你写 PR 时 master 还没有 #2107;它合入后( 可以直接复用 #2107 已有的东西(都在
期望的形态:
测试请注意:仓库 CI 用 改完直接 push 到这个分支即可,我会安排复审增量并盯 CI 首跑。如果上面第 2 点(普查优先、不为探测 boot default)你觉得有更好的方案,也完全可以按你的判断来,在评论里说明取舍就行。 |
|
补充复审(复审者)在这两个追加 commit 上的反馈:必修项已全部通过,无阻塞项。下面 3 点请在你做 P1 socket 改造时一并处理:
另记录两条不阻塞的 P3(本 PR 不必做):bootstrap 包装层建议后续补一条私有 你改完 push 后我们会重 fetch 核 head 并只复审增量;新 head 的 CI 首跑(含 bun-test)需全绿。 |
…epcoldy#2107) The original reaper only censused the DEFAULT socket. After deepcoldy#2107 (5ec8e61) every NEW managed session goes to the dedicated `tmux -L botmux` socket ($TMUX_TMPDIR/tmux-<uid>/botmux); the default socket keeps only pre-deepcoldy#2107 records and /adopt. On a host whose managed sessions were all created after isolation, a default-only census finds no managed listeners and reaps nothing while the displaced -L botmux server keeps every per-session file lock — the "conversation is open in another app" outage is not fixed. Reproduced: REAPED=0 with the displaced server on -L botmux. Changes: • defaultSocketPath() takes the frozen socket name (undefined = default, 'botmux'/recorded name = dedicated), reusing tmux-socket.ts helpers (preferredManagedTmuxSocket / managedTmuxCandidateSockets / tmuxSocketArgs). • Census-first per socket: the kernel census (/proc/net/unix, lsof -U) is taken once; a socket pathname with NO listener is skipped without a probe and without booting anything. Critically the default socket is never booted just to probe (that would recreate the shared footprint deepcoldy#2107 removed). display-message on a missing socket fails ENOENT without spawning a server (verified tmux 3.3a/3.6a), so probing a path that does have a listener is side-effect-free. • Only when a listener exists but no reachable owner answers (tmux- authoritative "down") is a disposable per-pid bmx-reap-bootstrap-<pid> session created to pin a fresh reachable owner; then re-probe → reap displaced fingerprinted listeners → remove the bootstrap. 'unknown' (timeout/spawn failure) skips that socket. • daemon.ts passes the candidate set: every socket an active persisted session is frozen to (persistentBackendTarget.socketName) plus default and the current preferred socket. • The bmx-reap-bootstrap-<pid> argv fingerprint is socket-agnostic — it must match whichever socket the bootstrap (or a real session) booted. • macOS verified: lsof -U keeps reporting the original bound pathname after the socket file is unlinked (no "(deleted)" shape change). Tests (23 reaper cases): -L botmux displaced-server reap; empty socket ⇒ no probe AND no boot; down→bootstrap→reap→cleanup; unknown⇒skip; default + dedicated handled independently in one pass; kill-switch precedes probing; the uid<0 guard case now computes its fixture path with the implementation (defaultSocketPath(env,-1)) so deleting the guard turns it red; fingerprint cases cover `-L botmux new-session`; SOCK/uid fixtures all derive paths from process.env (CI tmux fence redirects TMUX_TMPDIR). tsc --noEmit clean; related files 165/165; live macOS end-to-end on an isolated TMUX_TMPDIR: displaced -L botmux server detected and terminated with its panes, default socket dir empty afterwards (no probe boot). The 72 failures in the full local unit run are pre-existing macOS-only breakage (Linux paths like /bin/touch, orca token, companion dist) — identical on this branch's parent commit e3f5a3a when checked out alone; CI (Linux) is green for the prior head. Co-Authored-By: Claude Code <[email protected]>
|
P1 已完成并推送(b1559fa48):reaper 对齐 #2107 的独立 socket。 多 socket 覆盖
普查优先、不为探测白 boot default(评审第 2 点)
判定与复核
macOS unlink 形态(评审第 4 点)
复审 3 个测试点
验证
请复审增量;CI 首跑我会盯。 |
|
感谢 @Lotu527 的贡献,PR 已合入 master( 这个修复解决的问题在生产上真实造成过会话批量卡在文件锁,多 socket 普查 + 「拿不准不杀」的保守设计做得很好;#2107 socket 隔离落地后的多 socket 对齐(default ∪ 专用 socket ∪ 会话冻结名)也补得干净。 几点非阻塞的后续(不影响本次合入,欢迎你继续参与,也可以由我们跟进):
功能默认关闭,需要在 Dashboard「全局设置 → 实验性配置」里显式开启;紧急停用可用 |
|
🚀 Released in v3.51.0 |
Problem
A daemon that is hard-killed / upgraded leaves the shared tmux server running. If the new daemon's tmux traffic cannot reach it (probe timeout under load, socket path cleaned from
/tmp, …), a second tmux server is born and rebinds the default socket path. The old server keeps its unlinked socket and becomes unreachable —tmux list-sessionscan never see it again, so botmux can neither manage nor kill it or its panes.During session restore the new daemon cold-resumes every restored CLI in the fresh server. The previous generation's long-lived CLIs (codex, …) are still alive inside the displaced server, each holding its per-session file lock (
~/.codex/thread-writer-locks/<id>.lock). The resumed CLI cannot acquire it and parks on:Measured in production (2026-10-22, botmux 3.48 / tmux 3.6a): 19/48 restored sessions blocked after one restart; the orphan process tree leaked ~13 GB RSS, and a few CLIs kept burning tokens on forked sub-threads.
reapOrphanWorkers()only reaps__workerprocesses and deliberately never touches the tmux server they used — so this gap was unhandled.Fix
Adds
ensureServerThenReapOrphanTmuxServers()(src/core/orphan-tmux-reaper.ts), invoked indaemon.tsright afterreapOrphanWorkers()and before session restore:tmux start-servercannot pin a server durably — with the defaultexit-empty onit exits as soon as the client disconnects (verified).$TMUX_TMPDIR/tmux-<uid>/default):/proc/net/unix+/proc/<pid>/fdon Linux,lsof -Uon macOS.SIGTERMit. The orderly shutdown closes its panes and releases the managed CLIs' file locks, so the cold-resumed sessions recover immediately.Safety / conservatism
new-session -s bmx-<id>/bmx-diag-<id>, or a descendant pane carriesBOTMUX_MANAGED_SHELL=1. A user's own interactive tmux is never touched.ESRCH./tmpsymlink canonicalization (/tmp→/private/tmpon macOS;realpathon the socket file itself fails withEOPNOTSUPP, so the parent directory is canonicalized).BOTMUX_DISABLE_ORPHAN_TMUX_REAP=1.Tests
test/orphan-tmux-reaper.test.ts— 13 unit cases: displaced-server reap, managed-descendant fingerprint, no-reachable-server, healthy single server, user-tmux spared, non-default socket paths, non-tmux holders, opt-out,ESRCHrace, platform/uid guards, plus parser/tree/path helpers.tsc --noEmitclean.TMUX_TMPDIR(server booted with abmx-session, socket unlinked, fresh server rebound) → displaced server pid detected and terminated, child panes cleaned, reachable bootstrap removed.维护者追加说明(评审过程中补入)
默认关闭(自动杀进程的安全口径):daemon 启动自动 SIGTERM tmux server 属自动杀进程,本功能默认关闭,不会在升级后自行生效。开关位于 Dashboard → 全局设置 → 实验性配置 →「回收上一代遗留的 tmux 服务进程」(机器级,实时读取,下次 daemon 重启生效)。紧急停用可设环境变量
BOTMUX_DISABLE_ORPHAN_TMUX_REAP=1。覆盖 #2107 之后的全部托管 socket:#2107 起新建托管会话使用独立 socket
tmux -L botmux(socket 名冻结在每条会话的persistentBackendTarget.socketName,未冻结的老记录仍在 default)。reaper 在 daemon 启动时遍历「default ∪ 当前首选 socket ∪ 所有活跃会话冻结的 socket 名」,逐个做内核普查与回收;普查不到监听者的 socket 直接跳过,不会为探测在 default 上白 boot 一个 server(只读探针在 socket 文件缺失时返回 ENOENT,不会拉起 server;仅当某路径上有监听者但无可达 server 时,才用一次性 bootstrap 会话钉住新 owner)。🤖 Generated with Claude Code