Repository navigation
fix(fence): fenced-run 去掉总线后 scope 代码不再从 /run/user 推导回来 - #3158
Merged
Merged
Conversation
不带 --keep-user-bus 时 fenced-run 删掉 XDG_RUNTIME_DIR / DBUS_SESSION_BUS_ADDRESS, 但 userSystemdBusEnv() 不看环境变量,只要 /run/user/<uid>/bus 是 socket 就把总线地址补回, session-scope 的 defaultRun、pane 上的 systemd-run 参数和 fleet-scope 都会经它连到真实 user manager。 现在 BOTMUX_FENCED_RUN=1 且两个总线变量都不在时 userSystemdBusEnv() 返回空,围栏内走已有的无总线分支; 带 --keep-user-bus 而调用方环境没有总线变量时,fenced-run 按同一规则把推导结果补进子进程环境,行为与原来一致。 Fixes #3152
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
November 8, 2026 04:36
|
🚀 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.
Fixes #3152
现象
scripts/fenced-run.ts不带--keep-user-bus时会删掉XDG_RUNTIME_DIR/DBUS_SESSION_BUS_ADDRESS,承诺围栏内碰不到真实 user manager。但userSystemdBusEnv()不看这两个变量,只要/run/user/<uid>/bus是 socket 就把总线地址补回来。下面三条路径都经它连到真实 user manager:session-scope.ts的defaultRun(能力探测、systemctl --user stop按会话 id 停 scope);wrapCommandInSessionScope放在 pane 参数上的/usr/bin/env XDG_RUNTIME_DIR=… DBUS_SESSION_BUS_ADDRESS=… systemd-run;fleet-scope.ts默认 runtime 的busEnv。在 fenced-run 内只调用
userSystemdBusEnv(),修复前得到{"XDG_RUNTIME_DIR":"/run/user/0","DBUS_SESSION_BUS_ADDRESS":"unix:path=/run/user/0/bus"}。根因
这段推导是给没导出总线变量的生产进程用的(cron、不走 pam 的 ssh 拉起的 daemon),所以它有意不看环境变量。fenced-run 删变量想表达的是「没有总线」,推导却把它当成「没导出」。
修法
userSystemdBusEnv():环境里有BOTMUX_FENCED_RUN=1、且XDG_RUNTIME_DIR和DBUS_SESSION_BUS_ADDRESS都没有时返回undefined。上面三条路径都从这里取总线,一处改动全部覆盖。围栏内因此走产品里已有的无总线分支:能力探测失败,不建 scope;fleet-scope 返回unsupported: no user systemd bus。fenced-run --keep-user-bus:如果调用方环境里两个总线变量都没有,就按userSystemdBusEnv()的规则把推导结果补进子进程环境。原因是带标记后产品只认环境变量,不补的话,这种情况下原来能连上总线的产品代码现在会连不上。读取的是调用方环境,所以如果 fenced-run 嵌套在一个已经去掉总线的围栏里,推导结果为空,内层的--keep-user-bus拿不回总线。userSystemdBusEnv({ env })参数,便于测试。fencedRunEnv新增isBusSocket参数,同样用于测试。不带
BOTMUX_FENCED_RUN=1时代码路径与原来完全相同,宿主和线上 fleet 的行为没有变化。为什么不用 PATH shim
另一个方案是把 systemd 测试围栏里拒绝
systemd-run/busctl/systemctl --user的 PATH shim 放到 fenced-run 子进程 PATH 最前面。没选它,原因有三:run把 PATH 写死成/usr/bin:/bin,它的busctl根本不经过 shim。要拦就得改 fleet-scope 的子进程环境,等于为了测试改产品行为。代价是产品代码读了一个围栏标记。这只是一行判断:只在标记为
1且两个总线变量都缺失时生效;标记只由仓库内的scripts/fenced-run.ts设置;src 里已有NODE_ENV === 'test'、BOTMUX_TEST_*这类先例。也没有把 shim 叠加成纵深防御。推导堵住后,围栏里已经没有总线地址,shim 能多拦的只剩探针自己写死总线地址这一种情况。它和 fenced-run 已声明不覆盖的「命令里写死的绝对路径」属于同一类,收益不抵多维护一套 PATH 改写。
行为变化
--keep-user-bus:围栏内 botmux 的 scope 代码拿不到 user bus(这正是本次修复)。--keep-user-bus,调用方已导出总线变量:原样透传,与原来一致。--keep-user-bus,调用方没导出总线变量:产品代码仍得到原来的推导结果。另外,探针里直接执行的systemctl --user现在也能拿到总线(原来只有产品代码能推导到)。这与该参数的本意一致。验证
test/session-scope.test.ts:标记在、两个变量都缺时返回空;只要有任一变量,或标记不是1,结果与原来相同。原有用例改为显式传env: {},这样整套测试在 fenced-run 内跑时结果也不变。test/fenced-run.test.ts:真跑 fenced-run,探针在子进程里调用userSystemdBusEnv()(socket 检查打桩,没有 user bus 的 CI 上同样有效)。默认模式拿不到总线,带--keep-user-bus照常拿到。fencedRunEnv单测覆盖四种情况:默认去掉总线;透传调用方的总线变量;调用方没有总线变量时补上推导结果;嵌套在已去掉总线的围栏里时拿不回总线。DBUS_SESSION_BUS_ADDRESS;判断漏看XDG_RUNTIME_DIR;标记写成「有值就算」;删掉--keep-user-bus的补全;补全改读 fenced-run 自身进程环境;补全条件写成「缺任一变量」。7 个全部被抓住。derived从推导出的总线变为null;带--keep-user-bus时,无论调用方是否导出总线变量都和原来一致。run注入成只记录、不执行:默认模式下 fleet-scope 返回unsupported,没有调用busctl,pane 参数里也没有总线变量;带--keep-user-bus时与修复前相同。bun run build(含 typecheck)通过。相关测试fenced-run、session-scope、fleet-scope、launch-layers、index-supervisor-env-boundary、systemd-fence、vitest-config-fences全部通过。主机负载高,未跑全量。