Skip to content

fix(executor): adopt respawn provenance via fd/pipe proof for subreaper orphans (Refs #3300) - #3652

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/3300-subreaper-orphan-proof
Nov 17, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/3300-subreaper-orphan-proof

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

UNVERIFIED-BY-CAPTAIN

摘要

rubash #3300:后台函数体重解析行戳取决父进程退出时序。实现 DESIGN.md 的 fd/pipe 握手(option 2):每次 fork 形 respawn 造全新匿名管道,写 16 字节 nonce,spawn 前关写端,读端以 ≥100 的号交给子进程,配两个新 env 键 __RUBASH_RESPAWN_PROOF_FD / __RUBASH_RESPAWN_PROOF;入口恰一次校验(fstat 须 FIFO、O_NONBLOCK 防挂、读恰 16 字节等于 nonce、再读须 EOF),成功后关闭;respawn_provenance_validated 新增一臂(有效 proof + spawner 已死 → 开孤儿分支),旧两臂与 __RUBASH_SPAWNER_PID 戳不动。

Refs #3300

本 PR 删除了什么

什么也没删除——纯新增机制 + 钉,无行为删除、无文件删除、无公开 API 删除。唯一的行为变化:被 subreaper 收养的孤儿 respawn 子进程现在采信来源(此前回退到重解析行号)。

腿况(车道本地)

需队长裁

完整报告:~/workspace/fix-3300/REPORT.md(Part II)

…er orphans (Refs #3300)

Every fork-shaped respawn (async, coproc, both process-substitution
forms) mints a fresh anonymous pipe holding a fresh 16-byte nonce,
closes the write end before spawn, and hands the child the read end
at a spawner-chosen number >= 100 plus two per-spawn keys
(__RUBASH_RESPAWN_PROOF_FD / __RUBASH_RESPAWN_PROOF). The child's
entry validates the pipe exactly once (FIFO + O_NONBLOCK + 16 bytes
== nonce + EOF) and the orphan branch opens when the proof is valid
and the stamped spawner is gone. A child orphaned to a live unrelated
parent (a subreaper) now adopts its spawner provenance instead of
falling back to a re-parsed body line.

GNU: execute_cmd.c:1576 execute_in_subshell, subst.c:6362
process_substitute — the forked child keeps the definition line
regardless of when its parent exits.

Pins: tests/issue3300_subreaper_orphan_regressions.rs (5 cells, all
assert the reparenting shape: PPID == harness pid, SUB == 1,
PID == spawner pid).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3652 第1轮 — 建议:FAIL
head: f627f55 base(merge-base): 6171293 文件: 8 个 +710/-6
关联 issue: #3300(状态:open,我核过的时间:2026-11-16 21:05 本地)

机制核查:

腿(本轮在 WSL Linux(x86_64, rustc 1.98.1) 上补跑,Unix-only 形状只有 Linux 可复现;Windows 侧只做编译/警告腿):

  • 钉自跑:issue3300_subreaper_orphan_regressions(Linux)5 passed / 0 failed(async/background/coproc/in-output-procsub/in-input-procsub)
  • fail-first:base 6171293(Linux,同一钉文件)1 passed / 4 failed,红名 = {async_child_adopts_provenance, background_reports_definition_line, coproc_child_adopts_provenance, output_procsub_child_adopts_provenance}(input_procsub 本轮到绿);PR 自述 base 每轮 3–4/5 红(竞态调制),本轮 4/5,一致。
  • 邻钉:[bug] respawn 来源链可伪造:__RUBASH_SHELL_PID 冒充 $$ 并开门放行伪造位置参数 #2136 issue2136_respawn_provenance 10 passed / 0 failed;函数体首命令带重定向时报错行指向命令自身而非函数定义行(行戳捕获面) #3209 issue3209_function_body_first_redirect_stamp head 23 passed / 1 failed(coproc_first_reports_definition_line),base 同钉 22 passed / 2 failed(background_first + coproc_first),即 head 修好 background 一格、coproc 那格 base 即红 → 非本 PR 的账。
  • lib 双边(Linux):head 1249 passed / 0 failed / 4 ignored;base 1249 passed / 0 failed / 4 ignored;差集 ∅。(Windows 侧 lib 亦跑过:head/base 同为一个环境基线失败 issue2084,差集 ∅。)
  • -D warnings:Linux RUSTFLAGS="-D warnings" cargo check --locked --lib --bins 0 警告 0 error;Windows 侧唯一 error 仍是既存 src/executor/command_substitution_values.rs:4 unused import: std::io::Read(本 PR 未触碰)。
  • 卫生:rustfmt --check 8 个文件 clean;marker-lint OK (117 consts, 268 allowlisted);utf8 clean
  • CI(run 43630876272,head f627f55):红 5 项 —— Cross-target check (darwin) = 本 PR 的账(E0425 cannot find function prctl / cannot find value PR_SET_CHILD_SUBREAPER,测试文件 #![cfg(unix)] 但 prctl 是 Linux 专属);Cross-target check (linux/android) = 本 PR 的账(E0308/E0277 src/executor/init.rs:181 st.st_mode & libc::S_IFMT,android 上 mode_t=u32 vs S_IFMT=u16);Rust tests / Rust tests (macOS) / GNU regression goldens and canaries = 唯一红均为 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(基线)。
  • GNU 对照:GNU bash 5.3.20(1)-release(WSL)。形状 f() { >x\ + y foooo & } + f:GNU stderr = case.sh: line 1: foooo: command not found(用 2>err.txt; sleep 1 收尾捕获;target/p3652/case.sh),与钉的期望、与 head 一致。

F 点:

  • F1(阻断,本 PR 的账):src/executor/init.rs:181 if (st.st_mode & libc::S_IFMT) != libc::S_IFIFO 在 linux/android 目标上 mode_t=u32、S_IFMT=u16 → E0308/E0277,Cross-target check (linux/android) 红。修法:libc::S_IFMT as libc::mode_t(或 st.st_mode & (libc::S_IFMT as libc::mode_t) 再与 S_IFIFO as libc::mode_t 比较)。证据:job 132556953047 日志 + 本机 cargo check --target aarch64-linux-android 等价报错。
  • F2(阻断,本 PR 的账):tests/issue3300_subreaper_orphan_regressions.rs 用 #![cfg(unix)] 包住 libc::prctl / PR_SET_CHILD_SUBREAPER(第 79-84 行附近),macOS 没有这两个符号 → E0425,Cross-target check (darwin) 红。修法:subreaper 相关 cell 用 #[cfg(target_os = "linux")](或 target_os = "android"),文件级保留 unix 只为 Permissions/CommandExt。
  • F3(缺腿/待裁,不阻断):markers census 取舍(PR 自述留队长裁)与合入定序(fix(executor): coproc respawn seed takes first leaf's GNU stamp (Refs #3301) #3615 同函数)需队长判;本报告的 Linux 腿已把技术面补齐。

续作记录:本轮未动代码(按分工,FAIL 只发报告)。
未跑/VOID 的腿:Windows 侧无法跑 Unix-only 钉(已用 WSL Linux 补跑,非 VOID)。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3652 第2轮 — 建议:PENDING(缺腿:新 head CI 未触发)
head: 2c66f4a(= f450fa2 + 一个空 retrigger commit;实质内容 head 为 f450fa2)
base(merge-base): 6171293 文件: 8 个 +713/-6
关联 issue: #3300(状态:open,我核过的时间:2026-11-17 01:57 本地)

机制核查:

  • 设计成立:每个 fork 形 spawn 点铸独立匿名管道+16 字节 nonce,写端 spawn 前关闭(fd/unix.rs:459 mint_respawn_proof),子进程入口一次性验证 FIFO+O_NONBLOCK+16B==nonce+EOF(init.rs:173 validate_respawn_proof_fd),验证成功才 close;respawn_provenance_validated 新增第三臂 _ => proof_valid && stamped_spawner_gone(init.rs:423)——子reaper 收养的孤儿 ppid 既非 spawner 也非 1,只有管道证明+spawner 已死才采纳,与 GNU"fork 子进程即 subshell"语义一致(execute_cmd.c:1576 execute_in_subshell、subst.c:6362 process_substitute、variables.c $$)。
  • 4 个 spawn 点均已接 proof:async &(compound_exec.rs:1386/1547/1930 走 spawn_whitelisted extra_handles dup2 自身编号)、coproc(:5422/5562/5755)、输出 procsub(external_setup.rs:3789/3832/4126)、输入 procsub(trap_exec.rs:4832/4896/5078+5113 独立 pre_exec 清 CLOEXEC)。proof 键不进 FORK_TRANSPORT_KEYS、不转发孙代、entry 即消费——与注释声明一致。
  • 上轮 F 点(跨目标编译)已修:add92dcca init.rs:181 改 u32::from 归一(android 上 st_mode=u32、S_IFMT/S_IFIFO=u16,原 u32&u16 不匹配——已用 aarch64-linux-android target 对 libc 表达式做编译探针验证通过);f450fa26b 钉文件 gate 收窄到 linux/android(darwin 无 prctl/PR_SET_CHILD_SUBREAPER,探针验证该目标下表达式编译通过、文件整体 cfg 空)。
  • diff 无无关改动、无桥接层。

腿(逐项给数字与名字,均为 Linux/WSL 实测):

  • 钉自跑(head f450fa2,WSL cargo test --test issue3300_subreaper_orphan_regressions):5 passed / 0 failed,连续 10 次全绿
  • fail-first(merge-base 6171293 独立 worktree 原样放钉):1 passed / 4 failed,红名 = {subreaper_orphan_async_child_adopts_provenance(SUB=0), subreaper_orphan_background_reports_definition_line(line2≠line1), subreaper_orphan_coproc_child_adopts_provenance(SUB=0), subreaper_orphan_output_procsub_child_adopts_provenance(SUB=0)};input_procsub 在 base 经"父仍存活"arm1 侥幸绿(钉注释已声明此竞态),与缺陷面一致
  • 相邻面抽查(同一 WSL target,head):issue2136_respawn_provenance 10/10、issue2134_async_child_forgery 7/7([bug] respawn 来源链可伪造:__RUBASH_SHELL_PID 冒充 $$ 并开门放行伪造位置参数 #2136 防伪造臂未被 proof 放松)、issue1074_bg_subshell_depth 4/4、issue1461_bashpid_subshell_regressions 1/1、issue1630_async_trap_table_regressions 4/4、issue1012_exec_exit_trap_regressions 8/8、issue1583_input_procsub_eager_start 3/3、issue1624_coproc_family_retire_regressions 4/4、issue1220_procsub_write_async 2/2、issue2332_respawn_text_body_lineno_regressions 12/12、issue2410_procsub_value_leak_regressions 6/6、issue3291_read_d_fd_table_mb_serve 7/7、issue1023_coproc_unbind 0/0(该 stem 在 Linux 下无用例)——合计 68 例全绿
  • GNU 对照:WSL /usr/local/bin/bash = GNU bash 5.3.20(1)-release;python subreaper harness(PR_SET_CHILD_SUBREAPER)跑 ( sleep 0.3; echo "PPID=$PPID SUB=$BASH_SUBSHELL PID=$$ BASHPID=$BASHPID" > ppid.txt ) & exit → GNU: PPID= SUB=1 PID= BASHPID=;rubash head 同形:PPID= SUB=1 PID= BASHPID= → 逐字段一致
  • -D warnings:Windows cargo check --all-targets 通过;警告全部位于无关测试文件与已知基线 src/executor/command_substitution_values.rs:4,本 PR 8 文件 0 警告
  • 卫生:8 个 PR 文件 rustfmt --edition 2021 --check 全 clean;marker-lint OK;utf8-boundary clean
  • CI:f627f55b8 旧 CI 红 = {Cross-target linux/android(init.rs:181 u32&u16)、Cross-target darwin(tests prctl)、Rust tests(issue2875 基线)、Rust tests macOS(issue2875 基线)、goldens(基线)};新 head f450fa2 与 2c66f4a 的 pull_request synchronize 事件均未派发任何 check-run(同期其他 PR 正常出 run,已用空 commit 重推仍无)→ 新 head CI 腿 VOID

F 点:无(上轮两条跨目标编译 F 点已消除,见续作记录)

续作记录:

未跑/VOID 的腿:

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3652 第3轮(Z3 波,CONFLICTING 机械解尝试)— 建议:PENDING(语义冲突,已 abort;需队长裁定合并方式)

head: 2c66f4a(未变)base(merge-base): 6171293
关联 issue: #3300(状态:open,本轮复核时间:2026-11-17 05:0x UTC)
master 尖(本轮 fetch):a0a6d78ad

机械解尝试记录:

  • git merge origin/master(a0a6d78a)→ CONFLICT ×2:src/executor/init.rs、src/executor/compound_exec.rs;external_setup.rs/markers.rs/mod.rs/trap_exec.rs 自动合净。
  • init.rs = 语义冲突(abort 依据):master bats.bats #71 (Don't hang on CTRL-C, issue #353) hangs forever: inner test registration hits "BATS_TEST_TAGS: readonly variable", latch never fires #3049(0a405d8a0,Refs bats.bats #71 (Don't hang on CTRL-C, issue #353) hangs forever: inner test registration hits "BATS_TEST_TAGS: readonly variable", latch never fires #3049)把 provenance 判定前移到 RAW env、VarTable::from_values 与 transport strip 之前(master init.rs ~L384-389:internal_respawn 决定后 if !internal_respawn { fork_respawn_transport_key 全家剥离 })。本 PR 的 proof 第三臂必须喂给这个前移后的判定——conflict 区内 master 侧删掉的是旧的迟判定块,但 master 的早判定调用点(合并树 ~L531,2 参)在冲突标记之外、不自动改成 3 参 → 任何解法都要手工改 master 新增的早判定代码。若按「保留两处判定」的天真解,早 strip 会用无 proof 的判定跑:subreaper 孤儿被判 !internal_respawn → transport 先剥 → 本 PR 修复失效。唯一正确解是把 respawn_proof_validated() 接到早判定点——这是对 bats.bats #71 (Don't hang on CTRL-C, issue #353) hangs forever: inner test registration hits "BATS_TEST_TAGS: readonly variable", latch never fires #3049 不变量的语义再锚定,超出机械解范畴,按 Z 波口径 abort(git merge --abort 已执行,工作树干净,head 未动)。
  • compound_exec.rs = 机械冲突(记录备查):HEAD 在 let exe 前插 proof-mint 块,master 把 std::env::var_os("CARGO_BIN_EXE_rubash").map(...) 抽成 test_rubash_binary_from_env();正确解 = 保留 mint 块 + 用新 helper,moot(因 init.rs 语义冲突整体 abort)。
  • 语义正确性提示(供队长裁定时参考):proof 验证读 live std::env::var + fd,与 raw_env/snapshot 的构建时序无耦合;OnceLock 保证 main.rs 入口(respawn_provenance_validated_from_process_env)与 new_inner 早判定至多验一次;proof 键不在 fork_respawn_transport_key 表,非 respawn 时由 PR 已合入的 env_vars.remove(RESPAWN_PROOF_*) 块(init.rs ~L1196 区)兜底不外泄。若队长认可「proof 臂进早判定」即唯一正确解,可直接照此 rebase。

腿:本轮为冲突裁定轮,未重跑构建腿(head 未变;第 2 轮 Linux 全腿绿结论存档:钉 5/5、fail-first 4 红名一致、邻钉 68 例、GNU subreaper harness 逐字段一致)。
CI:head 2c66f4a 的 pull_request synchronize 事件前轮已记未派发 check-run(VOID);本轮未推新 commit,无新 CI。
未跑/VOID 的腿:CI(事件未派发);跨目标全量 check(本地缺 NDK/SDK)。

Conflict resolution against master's #3049 provenance refactor:

- src/executor/init.rs: master decides `internal_respawn` on the RAW env
  before import (the transport family must be stripped before
  from_values). The local recomputation the PR carried at the later site
  is dropped (master's comment stands); the proof verdict joins the
  early call as its third argument, `respawn_proof_validated()`
  (OnceLock-cached, so both entry paths see one verdict).
- src/executor/compound_exec.rs: keep the PR's proof mint (first, before
  any carrier dup) and take master's `test_rubash_binary_from_env()`
  exe resolution, which replaced the inline CARGO_BIN_EXE lookup.

Semantics unchanged: `respawn_provenance_validated` keeps master's two
ppid arms verbatim and only the live-unrelated-parent arm consults the
per-spawn fd/pipe proof, still together with `stamped_spawner_gone`.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3652 第4轮(e2:合并解 96dcd34 复核)— 建议:PASS(语义冲突解法实测成立;CI 红逐项归因完毕)
head: 96dcd34(merge origin/master@c8109c585,17:12 +08 推送,gh 实时)base(merge-base): c8109c5 文件: 8 个 +713/-6(对 master 侧)
关联 issue: #3300(状态:open,核于 2026-11-17 18:4x +08)

机制核查(解法与第3轮 abort 时给出的唯一正确解一致):

  • src/executor/init.rs:master bats.bats #71 (Don't hang on CTRL-C, issue #353) hangs forever: inner test registration hits "BATS_TEST_TAGS: readonly variable", latch never fires #3049 的 RAW env 前置判定保留(internal_respawn 在 VarTable::from_values 与 transport strip 之前,init.rs:~531);proof 判定以第三参并入 respawn_provenance_validated(claim, stamp, respawn_proof_validated())(init.rs:403-424),第三臂 _ => proof_valid && stamped_spawner_gone(...) 未放宽 [bug] respawn 来源链可伪造:__RUBASH_SHELL_PID 冒充 $$ 并开门放行伪造位置参数 #2136 stranger 洞。respawn_proof_validated() OnceLock 单次求值(init.rs:126-158 unix / :159-161 not(unix) stub),respawn_provenance_validated_from_process_env(main.rs 入口)与 new_inner 共用同一 verdict。proof 键不进 FORK_TRANSPORT_KEYS、不进 env_vars_snapshot 过滤(!starts_with("__RUBASH_") || rubash_spawn_inherited_state)、entry 后从 env_vars/mirror 双双摘除(init.rs:1272-1275)。
  • src/executor/compound_exec.rs:保留 PR 的 proof mint(first、carrier dup 之前、fail-open、#[cfg(unix)],:1544 async / :5716 coproc);exe 解析取 master 的 test_rubash_binary_from_env()(:1545/:5579/:8157)。
  • external_setup.rs:3907(输出 procsub)、trap_exec.rs:5470(输入 procsub)自动合净,mint+env_entries 接线完整(:3951/:5535)。
  • markers.rs:694-695 两键 EXACT 登记在;diff 对 master 侧恰为 PR 全部 8 文件 +713/-6,无混入。

腿(WSL Linux x86_64, cargo 1.98.1, 树 /root/rubash-e2 @ 96dcd34):

  • 钉自跑:issue3300_subreaper_orphan_regressions 5 passed / 0 failed(async/background/coproc/input-procsub/output-procsub 五 spawn 形全部收养 provenance)。
  • fail-first:同一钉文件原样放到 master ccb86141 → 2 passed / 3 failed,红名 = {background_reports_definition_line, coproc_child_adopts_provenance, output_procsub_child_adopts_provenance}(未修二进制上红格集合随父死时序浮动 3-4/5,符合钉注释声明;async/input-procsub 两格本轮到绿,与第1轮 base 6171293 的 1p/4f 同族——master 侧已有部分相邻修复)。
  • lib:merged head 全量 cargo test --locked --lib → 1273 passed / 0 failed / 4 ignored(171s)。
  • fmt:cargo fmt --check 干净。
  • CI(head 96dcd34, run 44053699520/982 按名核):
    • Rust tests 红 = check-utf8-boundary-hygiene 5 处 strmatch.rs byte→char —— stale-base:merge 的 master 父 c8109c585 不含其后落地的 6bb77bb4e(strmatch 五点 allowlist,Refs [ci] UTF-8 boundary hygiene gate red on master since #3630 (strmatch.rs 5 unreviewed widenings) #3839);下次 update-branch 到现尖即消。
    • Compatibility CLI tests 红 = c_command_keeps_named_coproc_output_fds_distinct(left first:\n right first:second\n)—— flake:本 lane 在 WSL 同 head 单测 8/8 连过,master CI 同任务绿;coproc 时序敏感格,非本 PR 账(存疑待队长复跑一次 CI 确认)。
    • GNU regression goldens and canaries 红 = golden_issue1230_exec_redirect_undo、issue_753::case_9_declare_indexed_operand_binds_nothing、upstream_suite_slice_snapshots —— 已知基线红族。
    • Regression pin stem census 红 = issue_1253_errexit_arith_error_discards_{assignment,echo}_and_continues 等 —— 已知基线 census 红族。
    • niubash builds against this tree 红 = stale-base session-method 漂移(同前轮记录)。
    • 绿:Rust tests (macOS)、Cross-target (linux/android)、Cross-target (darwin)、Windows tests compile check、Shell functionality tests、API ledger tripwire。
  • GNU 对照:后台函数体重解析行戳取决父进程退出时序(background_first box 报 line 2、本地/GNU 报 line 1) #3300 面为 Linux-only(PR_SET_CHILD_SUBREAPER/ppid 收养),Windows 无对应形状;WSL 即 Linux 原生复核,结论不依赖 Windows 对照。

F 点:无。

说明:本 merge commit 由 caomengxuan666 账号于 17:12 +08 推送(解法与第3轮给出的唯一正确解逐点吻合);本 lane 只做只读复核+WSL 实测,未动分支。若队长侧另有在途验证,以其为准。

未跑/VOID 的腿:无。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant