Skip to content

fix(executor): comsub reap publishes its status to $? before the enclosing command runs (rubash#485) - #494

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/comsub-status-propagation-2
Oct 10, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/comsub-status-propagation-2

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #485

问题

echo "$(cd nope && pwd)" $? —— GNU bash 在父壳回收命令替换子进程的瞬间就把其退出状态写入 last_command_exit_value(subst.c command_substitute),同一命令行内后置的 $? 随即可见;rubash 只把状态记进 last_command_substitution_status 单元、仅由空词/赋值两个收尾点消费,词展开路径上 $? 读到的仍是展开前的旧值(GNU 1 / rubash 0)。

修复(父侧回收点发布状态)

  • embedded_mutations.rs run_ast_command_substitution_with_context / run_function_command_substitution 尾部:fork 边界处 exit_code = status,不再回退 saved_exit_code(原地执行器就是 GNU waitpid 的父侧)。
  • expand_command_substitution_mut_typed_with_context 三个经过 &self 包装器的回落点(specialized cd/pwd 含 perf(executor): single-handle cd/pwd comsub path resolution (rubash#375) #484 快路径、specialized builtins、最终回落)从状态单元提升——包装器是 &self 写不了字段。
  • command_prepare.rs 整词引号 "$( )" 快路径同一提升。
  • parameter_core.rs 嵌入反引号词改道可变嵌入 walker($() 词的生产路径),脱离无法发布状态的遗留 &self 展开器。

语义边界(oracle: WSL GNU 5.3.0, /usr/local/bin/bash)

保持/对齐均以现场探针为准:

形态 GNU 5.3.0 修复后 rubash
echo "$(cd nope && pwd)" $? 1 1
false; echo "$(true) $?" 0(成功的替换同样覆盖) 0
true; echo $? "$(exit 3)" 0 (左到右,前置 $? 保留旧值) 0
echo "$(exit 3)$?" 3 3
echo a\exit 3`b $?` ab 3 ab 3
v="$(exit 3)$?" v=3、rc=3 v=3、rc=3
false; x=$(true) y=$? y=0、rc=0 y=0、rc=0
f(){ local v="$(exit 3)$?"; } v=3、rc=0(local 屏蔽) v=3、rc=0
D 用例:set -e; x=$(exit 4) 中止 rc=4 中止 rc=4
D 用例:set -e; echo "$(exit 9) x" 不中止 不中止

已知相邻缺口(本 PR 不动,现状与修复前一致)

  1. 管道阶段词展开存在前置 pre-pass:true; echo $? "$(exit 3)" | tr -d ' ' rubash 得 3、GNU 得 0——后一词的替换先于前一词的 $? 执行。该 pre-pass 早于本修复存在(修复只是让它的状态变得可观测),属流水线展开时序子系统,建议另开 issue。
  2. cd/pwd 命令替换快路径失败时 stderr 静默,GNU 会打印 bash: line N: cd: nope: ...(status/stdout 已一致)。

验证

  • 新增 tests/issue485_comsub_status_propagation.rs:13/13(上表形态 + set -e 两用例 + 嵌套替换,全部为 5.3.0 现场实测字节)。
  • 相邻套件全绿:issue70、issue343_345、issue440(fix(executor): broken-pipe writes no longer abort the interpreter (rubash#440) #455 车道)、issue71、issue154、issue305、issue319、issue335、issue354、issue362、issue368、issue376、issue379、issue384、issue416_417、posixexp2、comsub_nonascii(合计 134 用例);conformance runner 22/23(03-cond-unclosed-bracket 为既有环境项,GNU 同形中止 rc=2)。
  • 全量 cargo test --no-fail-fast:失败集与修复前树逐名比对完全一致(68 个 host-env 失败:pwd/path 盘符渲染、tilde-user fixture、bashdb fixtures、prompt/cli 环境类;双向空差),lib 636 passed / 2 failed = master 既有环境对。

…osing command runs (rubash#485)

GNU command_substitute (subst.c:7143) reaps the substitution child in the
parent and the child's exit status becomes last_command_exit_value at that
moment - BEFORE the enclosing command runs - so every later expansion on
the same command line sees it, and a SUCCESSFUL substitution clobbers the
previous status just the same. Probes, WSL GNU bash 5.3.0
(/usr/local/bin/bash, 2026-10-10):

  echo "$(cd nope && pwd)" $?   -> " 1"   (rubash: " 0" - the issue repro)
  echo "$(exit 3)" $?           -> " 3"
  false; echo "$(true) $?"      -> " 0"   (unconditional reap contract)
  true; echo $? "$(exit 3)"     -> "0 "   (left-to-right: $? BEFORE keeps old)
  echo "$(exit 3)$?"            -> "3"    (same-word trailing fragment)
  echo a`exit 3`b $?            -> "ab 3" (embedded backtick word)
  v="$(exit 3)$?"               -> v=3, rc=3 (assignment keeps last comsub)
  false; x=$(true) y=$?         -> y=0, rc=0
  f(){ local v="$(exit 3)$?"; } -> v=3, rc=0 (declaration builtin masks)

rubash recorded the substitution's status only in the
last_command_substitution_status cell and promoted it at the two
consumption sites (vanished-words null command, assignment RHS tail), so
the word-expansion path never published it: every mid-line $? read the
stale pre-expansion value.

Fix - publish the reaped status at each parent-side completion point:
- embedded_mutations.rs run_ast_command_substitution_with_context and
  run_function_command_substitution tails: exit_code = status at the fork
  boundary instead of restoring the pre-comsub value (the in-place
  executor IS the parent side of GNU's waitpid; saved_exit_code dropped
  exactly the status GNU publishes)
- embedded_mutations.rs expand_command_substitution_mut_typed_with_context:
  the three fallbacks that run through the &self
  expand_command_substitution_with_context wrapper (specialized cd/pwd
  path incl. the #484 fast path, specialized builtins, final fallback)
  promote the wrapper's status cell - the wrapper is &self and cannot
  write the field
- command_prepare.rs whole-word quoted "$(...)" fast path: same
  promotion after the wrapper call
- parameter_core.rs expand_word_mut_with_context: embedded-backtick words
  route to the mutable embedded walker (the production path for every
  $( )-bearing word) instead of the legacy &self expand_word fallback,
  whose completions cannot publish status

Unchanged on purpose (GNU-verified): the enclosing command's own status
still overwrites when it runs (echo $(false); $? = 0); set -e aborts on a
failed comsub assignment with its rc (D case, 1 / 4) and does NOT abort
on a word-attached comsub failure; local/declare mask the assignment
status (rc 0); nested comsub reports the outer body's own last status to
the parent.

Known adjacent gap, NOT touched (pre-existing, separate subsystem): in a
pipeline, stage words are expanded in a pre-pass, so `true; echo $?
"$(exit 3)" | tr -d ' '` prints 3 where GNU prints 0 - the later word's
comsub runs before the earlier word's $? expands. The pre-pass predates
this fix (which only made its status observable); filing separately.
Also pre-existing: the cd/pwd comsub fast path stays silent on stderr
where GNU prints the cd diagnostic (status and stdout match).

Verification: tests/issue485_comsub_status_propagation.rs 13/13 (oracle
probes above baked as assertions; set -e D case included). Adjacent
suites green: issue70, issue343_345, issue440 (#455 lane), issue71,
issue154, issue305, issue319, issue335, issue354, issue362, issue368,
issue376, issue379, issue384, issue416_417, posixexp2, comsub_nonascii,
conformance runner 22/23 (03-cond-unclosed-bracket pre-existing, GNU
aborts the same shape with rc 2). Full cargo test --no-fail-fast: failure set byte-identical to the
pre-fix tree (68 host-env failures compared name-by-name with the three
fixed files reverted: pwd/path drive rendering, tilde-user fixture,
bashdb fixtures, prompt/cli env classes; empty both-way diff); lib
636 passed / 2 failed = the master host-env pair.

Closes #485
…, not per admission probe

The #491 merge added the cd diagnostic to command_substitution_cd_pwd_output
- but the mut_typed word path consults that helper TWICE: first as the
admission probe (command_substitution_uses_specialized_path calls it with
.is_some() to admit the specialized path), then for the real expansion
inside the &self wrapper's inner walk. Each failure printed the diagnostic,
so `x="$(cd nope && pwd)"` and `local y="$(cd nope && pwd)"` reported
`cd: nope: No such file or directory` twice where GNU bash 5.3.0 prints it
once (probe vs script-file oracle, /usr/local/bin/bash, byte-compare on
both shapes).

Split the helper: the single-arg form (real expansion) keeps the emission;
the probe goes through command_substitution_cd_pwd_output_quiet_probe,
which suppresses only the diagnostic - status cell, output, and admission
result are unchanged.

Verification: both #485 suites green (tests/issue485_comsub_status_
propagation.rs 13/13, tests/issue485_comsub_status_word_propagation.rs
13/13); 17-form oracle matrix byte-identical to GNU 5.3.0 including the
cd shapes now that the diagnostic prints once; full cargo test
--no-fail-fast failure set identical to the pre-merge baseline (68
host-env failures, empty both-way diff).
@caomengxuan666
caomengxuan666 merged commit 07c67ab into master Oct 10, 2026
5 of 6 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/comsub-status-propagation-2 branch October 10, 2026 06:44
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] comsub 失败状态在词内 $? 的传播时序与 GNU 不一致(rubash 0 / GNU 1)

1 participant