Skip to content

WIP: coproc $! 进程保真:简单外部命令直 spawn (#1584);#1582 裁定材料+契约钉 - #2019

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/proc-identity-1584-1582
Oct 29, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/proc-identity-1584-1582

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

进程身份面双单:#1584 已修;#1582 交付裁定材料(形状表 + 方案代价),保持 open。

#1584 — coproc $! 进程保真(已修)

GNU execute_coproc 只 fork 一次、子进程直接 exec 简单外部命令(execute_cmd.c:2525 make_child(FORK_ASYNC)),所以 $! 就是程序本体的 pid。rubash 的 respawn 路线把 rubash -c 包装进程放在 $!:ps -o comm= -p $! 读到 rubash、kill $! 只杀包装(被杀的 sleep 本体无人发信号、成孤儿),进程树保真度失真(wait 状态 143 数字相同、树不同,双跑实测见 REPORT)。

修法(plan_direct_coproc_command,unix;Windows 维持 respawn):白名单准入直 spawn,任一条件落空回既有 respawn 路线,miss 只损失保真度、永不损失语义:

  • 简单命令体,无 tempenv 前缀赋值(绑定归子 shell),无 body;
  • 词与展开上下文无关:无命令/进程替换/算术展开,无 BASHPID/PPID/BASH_SUBSHELL(子进程里指 coproc 本身),无 RANDOM/SRANDOM(fork 副本序列);set -u 下任一展开、failglob 下任一 glob 词回退;
  • 重定向目标含进程替换或 {fd} 分配回退;
  • 展开后 head 解析与子进程 dispatch 同判:函数/别名/builtin/keyword 回退(GNU 子进程以 shell 本体跑它们,包装即保真形);未解析名回退(127 归子进程);
  • 目标必须是内核可 exec 镜像(ELF/Mach-O/#!)且有执行位——ENOEXEC 在 GNU 是子进程内按 shell 脚本重跑,只有包装路线能复现。

直 spawn 的环境走普通外部命令的 apply_external_environment,不带 shell 状态快照与 __RUBASH_* 键;管道/stderr 计划/重定向/job 注册/COPROC 绑定与修前共用同一尾段。残差:展开在父进程同步发生(准入类已限定为等值展开,仅剩超大目录 glob 的时序差)。

钉(tests/regression.rs mod issue_1584,CI regression 目标覆盖):种子形 comm=cat + fds 63,60 + 回环 + rc 0;kill 达本体(comm=sleep/143);/bin/cat;带参 grep;具名 coproc 的 NC_PID;负对照 builtin read 保持 shell 镜像(comm=rubash)。

Fixes #1584

#1582 — 平坦子壳 $BASHPID 合成值(待裁定,不在本 PR 修)

双跑形状表(节选;全表在工区 REPORT):

形状 判定
顶层 / 异步 ( ) & / procsub 生产者 一致(realized 层 $BASHPID = 子进程眼中的 PPID)
平坦 ( ) 深度 1/2、命令替换、管道 brace 阶段 分歧:BASHPID=host+depth,子进程 PPID=host(同根机制)
平坦层 $PPID 一致:双边都冻结于启动值(GNU set_ppid 仅 init 调一次,variables.c:606)——原疑「错位一代」实测推翻
平坦层 kill -TERM $BASHPID 一致:层死 143、父存活、Terminated 通知(#1461 拦截)
顺序兄弟子壳 投影复用同值;生命周期不重叠,与真实 pid 复用同性质

裁定要点:no-fork 前提下 $BASHPID 至多满足 {=真实子进程 PPID、每层互异、kill 只作用本层} 中的两条,GNU 靠真实进程三条全得。方案 A 维持现投影(选互异+kill 作用域);方案 B 取最近 realized 祖先真实 pid(得交叉核对、失互异,且 kill 拦截须特判——会吞掉子壳内合法的「杀主 shell 真实 pid」,风险高);方案 C 每层真进程与架构前提冲突。issue 原立案口径即「与状态视图线一并裁定、不做点补」。建议维持 A 并把投影规则写进状态视图文档;本 PR 只落三条与裁定无关的既有契约钉(realized 层一致性 / PPID 冻结 / kill 作用域——方案 B 若被采纳,第三条是其必须守住的安全线)。

Refs #1582

门

  • 本地:cargo check --locked --lib with RUSTFLAGS="-C debuginfo=0 -D warnings" 干净通过(单次不间断运行、产物核验);per-file rustfmt 已过;CR 计数 0。
  • lib/regression 全量名集与 diffcorpus:走本 PR 的 CI 裁定(2026-10-29 新规:CI 覆盖面甩 CI)。diffcorpus 本地未跑:本工区全量构建产物两次未落地(槽位 phantom),无修后二进制可跑——如需在合并前补跑,请主会话在有构建的工区代跑 cg 族。

GNU execute_coproc forks once and the child execs a simple external
command itself (execute_cmd.c:2525 make_child), so the coproc pid IS
the program's pid. rubash's respawn route put a rubash -c wrapper at
$!, running the command as a grandchild: comm($!) read rubash and
kill $! terminated the wrapper, never the command.

plan_direct_coproc_command admits a coproc to a direct spawn only
when parent-side evaluation is provably identical to the child's
fork-context evaluation: simple command, no tempenv prefix, no
command/process substitution or arithmetic expansion, no
process-identity dynamic variables (BASHPID/PPID/BASH_SUBSHELL), no
RANDOM draws, no procsub or {fd} redirect targets, nounset/failglob
decline on any expansion or glob word, the expanded head must not
resolve to a function/alias/builtin/keyword, and the target must be
a kernel-exec'able image (ELF/Mach-O/#!) so the ENOEXEC
run-as-shell-script fallback stays with the wrapper. Every miss
falls back to the unchanged respawn route. Direct children get the
ordinary external environment, not the shell-state snapshot; pipes,
redirects, job registration and COPROC endpoint binding share the
existing tail. Windows keeps the respawn route.
Seed shape plus the fidelity surface: $! names the program for
coproc cat, /bin/cat, grep with args and a named coproc (NC_PID);
kill -TERM $! reaches the command itself (comm=sleep, wait 143);
the fds stay 63,60 and the data round-trip is unchanged. Negative
control: a builtin coproc (read) keeps a shell-image child, the
GNU-faithful shape for shell-image coprocs.
Three invariants that hold on current master and that any
resolution of the flat-level BASHPID projection must preserve:
a spawn-realized (async) subshell's BASHPID equals the PPID its
children observe; $PPID stays frozen at its startup value inside
a subshell (GNU set_ppid runs once at init); kill $BASHPID inside
a flat subshell stays scoped to that layer (dies 143, host lives).
No behavior change; the projection question itself is left open
for adjudication with the shape table in the PR.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 29, 2026 01:42
@caomengxuan666
caomengxuan666 merged commit 1f007cc into master Oct 29, 2026
8 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/proc-identity-1584-1582 branch October 30, 2026 01:41
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.

[bug] coproc $! is a rubash wrapper process, not the command itself (process-tree fidelity)

1 participant