Repository navigation
fix(scope): 隔离 PID 命名空间里不再把进程号交给宿主 user systemd - #3088
Merged
Merged
Conversation
症状:在 --unshare-pid 的隔离环境里跑 worker 或测试时,宿主上无关进程被挪进 botmux-scope-probe-*.scope / botmux-session-*.scope,随后被当成会话 scope 一并停掉。 根因:隔离环境共享宿主的 user bus。systemd-run --scope 和 StartTransientUnit 交过去的是本命名空间里的 PID,user systemd 在自己的 命名空间里解析,于是挪动的是宿主上同号的进程。 修法: - 新增 probeSystemdPidNamespace:本进程在初始 PID 命名空间里即视为同一 命名空间,不起子进程;否则要求 /proc 编号的就是本命名空间、bus 对端 PID 在本命名空间可见且命名空间相同、并且就是 bus 报给 org.freedesktop.systemd1 的 PID(user systemd 自己监听的 socket 激活 bus)。任何一环失败或无法判断 都按不同命名空间处理。 - 四处交 PID 的地方先过这道检查:能力探测、MemoryMax 探测、会话启动命令、 supervisor 移入 fleet scope;不同命名空间时不建 scope、不调 StartTransientUnit,会话走原有的无 scope 路径,并记录原因。 - pane 由 tmux server 拉起,可能和 worker 不在同一命名空间:启动命令里再由 pane 内的 shell 比对一次命名空间,不一致就跳过 systemd-run 直接运行。 - 停 scope 前检查:scope 里有比 scope 早 5 分钟以上启动、且不是本 unit supervisor 子进程的进程时,拒绝停止并告警;无法判断时保持原有行为。 - 单测加 systemd 围栏:测试进程(vitest 和 bun)调用真实 systemd-run、 busctl、systemctl --user 时让测试失败,吞掉异常也会在 afterEach 报出。 验证:相关单测(vitest 与 bun)、tsc、build 通过;变异验证中去掉检查或 围栏的各项改动都会让测试变红;构建产物在隔离环境里四处均拒绝且不调用 systemd-run,宿主上走初始命名空间快路径、行为不变。
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
November 7, 2026 17:08
- systemd 围栏 shim 的拒绝语句改成单行 `echo ... >&2; exit 1`,不再用只替换首个换行的 replace 去缩进。 - 监管链路测试的载荷改为临时目录里的脚本文件,不再把 /bin/sh 当作被包装的程序传入;spawnSync 的命令不会再被识别成 sh -c 拼接。
Owner
Author
|
对照 #3000 验证时遇到的场景看了一遍:isolate-run 里跑拉起 worker 的测试,宿主的 dockerd、containerd、mihomo、nginx worker 等被挪进 有一点没覆盖:宿主上用
不放进本 PR、另开 follow-up 也可以。 |
This was referenced Nov 8, 2026
|
🚀 Released in v3.59.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.
症状
在有独立 PID 命名空间、但与宿主共用 user systemd 总线(
/run/user/0/bus)的沙箱里运行 botmux 或它的测试时,宿主上与沙箱内进程同号的无关进程(chronyd、dbus-daemon、systemd-logind、containerd、dockerd、nginx、postgres 等)被挪进botmux-scope-probe-*.scope。一次排查里发现 36 个宿主进程分布在 34 个探测 scope 里,其中约三分之一来自碰到真实systemd-run的回归测试。scope 被 stop 时,这些进程会被一起杀掉。根因
四处代码把 PID 以普通数字交给 user systemd:
probeSessionScopeCapabilities(botmux worker-budget status也走这里):systemd-run --user --scope … truememoryControllerPlacementVerified:MemoryMax=16 MB的探测 scope,finally 里再 stopwrapCommandInSessionScope:pane 里的systemd-run --user --scopemoveSupervisorIntoFleetScope:busctl … StartTransientUnit,PIDs=[process.pid]systemd 252 的 StartTransientUnit 不收 pidfd,
systemd-run --scope发的是自己的getpid(),manager 在它自己的 PID 命名空间里解释这个数字。代码从没检查调用方和 manager 是否在同一个 PID 命名空间,沙箱里的数字就落在了宿主的同号进程上。沙箱重挂了/proc,NSpid只有一列、/proc/1是 bwrap,所以单看NSpid或和/proc/1比都判断不了。修法
1. 确定性判定
probeSystemdPidNamespace()/proc/self/ns/pid等于pid:[4026531836](内核初始 PID 命名空间,inode 固定为 PROC_PID_INIT_INO)时判为共享,不起任何子进程。宿主的 user manager([email protected])由 PID 1 派生,必然在初始命名空间。/proc给本命名空间编号:/proc/self/status的NSpid恰好一列;busctl --user --augment-creds=no status给出总线对端 PID,且该 PID 的ns/pid与本进程相同。内核按读取方的命名空间翻译 SO_PEERCRED,对端在外层命名空间时读到 0,busctl 不输出PID=;GetConnectionUnixProcessID org.freedesktop.systemd1给出一个 PID;两个候选方案怎么选
GetConnectionUnixProcessID+ 比较/proc/<pid>/ns/pid)单独用不确定:总线报的是它那一侧编号的 PID。在重挂了/proc的沙箱里,跑得久的会话可能恰好有一个本地进程占着同一个号,比较 ns 就会误判为共享。上面第 2、4 步用内核对 SO_PEERCRED 的翻译做锚:只有总线对端在本命名空间可见、在本命名空间里,并且就是 user systemd 时,总线报出的 PID 才和本地/proc是同一套编号。沙箱里的总线中继或代理(对端是中继自己)、独立运行的 dbus-daemon(对端是 daemon)都判为不共享,属于保守方向。$XDG_RUNTIME_DIR/systemd/private)、没有 user 总线的,判定为不共享、不建 scope。这是保守方向:会话照常运行,只是没有 scope 清理和 MemoryMax。宿主上没有 user 总线的情况不受影响:初始命名空间不问总线,systemd-run照旧走私有 socket。2. 交 PID 前都先过判定
probeSessionScopeCapabilities:不共享时直接返回cleanupSupported: false,reason 写明原因,不起systemd-run。memoryControllerPlacementVerified:不共享时不起 16 MB 探测 scope,也不 stop。wrapCommandInSessionScope/prepareSessionScopedLaunch:不共享时命令原样返回,走现有的无 scope 路径。moveSupervisorIntoFleetScope:不共享时返回unsupported,不调 StartTransientUnit。systemd-run跑在 pane 里,而 tmux server 可能和 worker 不在同一个命名空间。启动命令在systemd-run前插一段/bin/sh检查,比较 pane 的/proc/self/ns/pid与 worker 判定时的命名空间。不一致就丢掉systemd-run及其选项,直接 exec payload,stdin、argv、退出码照样透传。识别 scope supervisor 的 argv(sessionScopePayloadPid、stop 前的豁免)同时认新旧两种格式,升级前已开的 pane 不受影响。stopSessionScope/stopAllSessionScopes/releaseSessionScopeForSpawn受同一判定约束:不共享的进程从没建过 scope,也不去停任何 scope。Session scope unavailable; using backend lifecycle cleanup: …会带上user systemd scopes are off: <原因>;supervisor 新增[supervisor] no own systemd scope: <原因>;botmux worker-budget status显示同一原因。3. 停 scope 前的兜底(评估后采用)
stop botmux 自己的 scope 前,先用一次
systemctl --user show <unit> --property=LoadState --property=ActiveEnterTimestampMonotonic --property=ControlGroup拿到 unit 状态、激活时间和 cgroup,再读 cgroup 里每个进程的/proc/<pid>/stat启动时间。有进程比 scope 早启动 5 分钟以上、且不是本 unit 的 scope supervisor 的子进程时,不 stop、不 SIGKILL,告警并列出这些进程(PID 和命令行)。之后重新启动会话改用 spawn-generation 名,close/restart 跳过这个 scope。systemd-run,它在 scope 启动时把自己挂进去,然后就地 exec AI CLI;负载高的宿主上观测到 CLI 比它的 scope 早 2–9 秒。被 PID 错配挪进来的宿主守护进程通常从开机就在跑。5 分钟给正常情况留了约 30 倍余量,又远小于宿主守护进程的年龄。/proc/<pid>/stat第 22 字段(USER_HZ = 100)和 systemd 的ActiveEnterTimestampMonotonic都以开机为原点。跨挂起时/proc用的 boottime 只会更大,进程只会显得更年轻,不会把 botmux 自己的进程判成外来进程。LoadState=not-found)时不 stop、不 SIGKILL,直接复用确定性名字。其他判断不了的情况(没有激活时间、cgroup 或/proc读不到)照旧 stop,不改变现有行为。name=systemd)三种布局都能读。4. 测试护栏
test/helpers/systemd-fence.ts挂在 vitest unit 项目(
test/unit-setup.ts)和 bun 腿(test/bun-test-fence.ts):spawn/spawnSync/execFile/execFileSync/exec/execSync(保留util.promisify.custom),bun 下另包装Bun.spawn/Bun.spawnSync,并通过mock.module覆盖 ESM import 和require。处在命令位置的程序解析到真实的systemd-run、busctl,或者带--user的systemctl时就抛错并记录。能展开的写法:env(含-S、-u)、timeout/nice/ionice/stdbuf/setsid/nohup/chrt/taskset/sudo/exec/command/time前缀、sh -c脚本及其位置参数和shift、sh <脚本文件>、命令替换、shell选项、绝对路径和手写的不含 shim 的 PATH。只是提到这些名字(echo、grep、command -v)不算调用。afterEach/afterAll里让测试失败,被测代码吞掉异常也一样。有夹具测试:在子 vitest 和子bun test里各跑一个吞掉拒绝的用例,断言子运行失败。systemd-run和busctl一律以退出码 1 拒绝;systemctl带--user时拒绝,其余(如--version)交给真实程序。Bun.spawn不带env时 Bun 用的是启动时的环境快照,护栏补成当前process.env,子进程同样拿到 shim。集成测试拉起的真实 worker 探测 scope 能力时会被拒绝,和 CI 上没有 user manager 时一样,走无 scope 路径。子进程被 shim 拒绝不判测试失败,因为这是生产代码自己在探测。systemd-run不是真实程序,照常通过。test/index-supervisor-env-boundary.test.ts在测试进程里 import supervisor 入口,从 bot 会话里跑测试时会真的发 StartTransientUnit,把 vitest worker 挪进宿主的 fleet scope(该测试把 PATH 换成了/usr/bin,shim 不在 PATH 上,是进程内那一层拦下的)。现在它 mock 掉 fleet-scope。CLAUDE.md补了一段「单测里不碰真实 systemd」的约定。删掉的测试及原因
test/session-scope-lifecycle.integration.test.ts:在测试进程里用真实systemd-runscope + tmux 验证生命周期,按「测试不碰真实 systemd」的规则不能再跑。scope 包装的外形(supervisor 留在 scope 外、stdin/argv/退出码透传、pane 命名空间不一致时不进 scope)由session-scope.test.ts用假systemd-run真实执行 shell 的用例和launch-layers测试覆盖。test/fleet-scope.test.ts里「against the real user systemd manager」两例:真实systemd-run+ StartTransientUnit,同理删除;移动与不移动的判断由注入 runtime 的用例覆盖。test/worker-codex-actor-launcher.integration.test.ts收尾里的stopSessionScope(sessionId):护栏下 worker 不会建出 scope,这一步只会去 stop 一个不存在的真实 unit。已知边界
systemd-pm2-migration.e2e.ts有意驱动真实 user manager(只管 unit 文件,不交 PID),e2e 不在 PR CI 里跑。影响面
systemctl --user show;unit 不存在时反而少掉原来的 stop 和名字检查。botmux worker-budget status写明原因。独立对抗复核
合入前做了一轮独立对抗复核,用真实组件(
unshare+ 私有 tmux server、isolate-run、新旧代码调用序列对比)攻击修复。结论:观测到的环境里修复有效,宿主侧四处调用点结果与改动前一致;另有下列问题,均已处理或列为已知边界。systemd-run;tmux server 在另一个命名空间时会发出沙箱 PID(用 unshare + 私有 tmux server 复现)systemctl show,release 更久超出预算LoadState;unit 不存在时跳过 stop 和 SIGKILL。存在的 unit 仍比改动前多一次只读 showtimeout/nice/setsid/env -S/sh <文件>包装、bun 的require和Bun.spawnSyncBun.spawn*并补当前 env,mock.module覆盖require"$@"当命令执行时才算验证
只跑了相关测试、tsc 和 build,没有跑全量。
session-scope、fleet-scope、systemd-fence、launch-layers、launch-layers-real-proc、index-supervisor-env-boundary、find-launched-cli-pid,205 通过、1 跳过(仅 bun 下运行的Bun.spawn用例)。session-scope64、fleet-scope16、systemd-fence33、launch-layers8,全部通过。worker-codex-actor-launcher.integration、fleet-supervisor.integration,37 通过。tsc --noEmit、bun run build通过。dist/core)实测,子进程全部记录,会交 PID 或动 unit 的调用一律拦下不执行:initial-namespace,不起子进程,启动命令照常带 scope。this process runs in PID namespace pid:[…], not the initial one, and the user bus is served from outside it (no peer PID visible here); …。能力探测、启动命令、fleet 迁移、stopAll 四条路径都拒绝并带上原因;唯一的子进程是只读的busctl --user --augment-creds=no status,没有systemd-run、systemctl,也没有 StartTransientUnit。变异清单
--sh -cshiftBun.spawnSyncBun.spawnSync子进程用启动时环境