Skip to content

WIP: fix die-trap serialization corruption + silent die path (Refs #2493 #2480) - #2545

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2493-die-trap-redirect-bg-parse
Nov 2, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2493-die-trap-redirect-bg-parse

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: fix #2493 (follow-up of #2480). Two faces, one root cause per face.

Face 1 — parse: ... 0<&8 & ;; / { ...; } & } 8<&0 failed to re-parse

Not a parser defect. Direct parse of case x in ( p ) cmd 0<&8 & ;; esac and the whole while ... do cmd 0<&8 & done ;; family passes in rubash byte-for-byte like GNU (probed, target/p2493/probe-parse.sh..probe3a.sh under WSL GNU Bash 5.3.20 = rubash master). The failing string was rubash's OWN serialization: respawned async & children re-parse a -c source built by background_command_source = background_function_prelude + bash_command_source_text(body), and three closer arms in src/executor/command_text.rs emitted ; } / ; unconditionally, so a body whose last command printed ... & serialized as:

f() { { { echo inner; } &; } 8<&0; }; { sleep 0; }   ← child's -c source, verbatim from the old diagnostic

& ; is illegal (GNU list grammar: parse.y:1275/1290 list1 '&' newline_list — only newline_list may follow &; a ; at list start hits the parse.y:1326 error arm). Because the corrupted text rode inside a function definition in the prelude, one such function (modernish die()) corrupted every async respawn afterwards — the die trap's own message block included. Minimal repro (old build, syntax error byte-visible; fixed build: done, rc 0):

f() { { { echo inner; } & } 8<&0; }
{ sleep 0; } &
wait; echo done

GNU ownership: print_cmd.c:693-728 print_group_command — brace body, then semicolon(), then }. print_cmd.c:1520-1530 semicolon() prints nothing when the accumulated output ends with \n or " &" (so GNU spells { ...cmd & }). print_cmd.c:357-363 cm_coproc delegates the body to the same group printer; print_cmd.c:1323+ print_function_def closes with newline("}"). This is the same rule the repo already ports as push_command_separator (rubash#294 sequence joiner, loop/if closers, prelude tail; #2388 heredoc delimiter lines) — these three arms had just been missed.

Fix (class fix, not symptom): all three closers — bash_command_source_text_render's brace_group arm, coproc_command_source_text's body, function_command_source_text's BraceGroup arm — now route through push_command_separator (\n after &/heredoc-delimiter, ; otherwise; also nothing after a trailing \n, matching semicolon()'s second suppression arm). No admission-list guards added or extended (rubash#117 discipline).

Face 2 — silent: die path rc 128 with no diagnostic

Consequence of face 1: the respawned child died at parse of the corrupted prelude before its body (the die message block: put ... >| /dev/tty; put ... >&2) could run, so the modernish die path exited 128 printing nothing where GNU prints <prog> died: <msg> from the forked trap body. No swallow-point to fix beyond the corruption itself — the diagnostic the old child did emit (... syntax error near unexpected token ';', -c: line N) was the parse error of the corrupted string (visible under set -x; lost in plain runs to the die path's own kill sweep racing the slow wrapper spawn+parse).

After the fix, the issue repros deliver byte-parity with the GNU 5.3.20 probes (script-file, /usr/local/bin/bash):

probe GNU 5.3.20 rubash (this PR)
. bin/modernish; die "direct die call" stderr <script> died: direct die call, rc 128 same
use sys/base; PATH=/dev/null _Msh_seq_bc x stderr died: seq: 'bc' wrote error: ..., rc 128 same message shape (bc: command not found — bc absent on WinuxCmd PATH, an environment gap per the issue)

Measurement note: probes must run without an MSYS timeout wrapper — coreutils timeout kills the detached wrapper child on exit (file-signal probes m1..m5 under target/p2493/), which masqueraded as a "children die at shell exit" divergence; MSYS bash and rubash both survive once the wrapper is removed.

Residual (out of scope, filed as #2543)

The respawn prelude re-defines functions that the exported-function env transport already carried, so readonly -f functions (all of modernish's) produce a 40-line readonly function stderr noise wall on every compound & after sourcing modernish. GNU forks and defines nothing (silent). The die message itself is unaffected.

Tests

  • tests/issue2493_die_trap_redirect_bg_parse.rs — (1) minimal parse shape (redirect-carrying brace ending in & + compound async to transport the prelude): no syntax error, rc 0, done; (2) die-shaped chain (both case arms with 0<&8 & ;;, nested braces, fd-8 copy): stderr carries died: hard error, rc 128, byte-baseline from the GNU probe (with the ps/awk-equivalent pre-exit delay; rubash delivers without it — the respawned child survives the parent's exit); (3) modernish real state when D:/repo/pre15-modernish exists (self-skips elsewhere).
  • src/executor/command_text.rs unit test closers_after_trailing_background_take_newline_not_semicolon pins the \n closers (brace/copro/function-def) and the unchanged ; } plain-body spelling.

Verification

  • WSL GNU Bash 5.3.20 script-file probes for every shape above (target/p2493/).
  • Respawn family green: issue294 / issue1704 / issue2332 / issue2429 (+2058/2136 targets, 0 tests on this host, same as master).
  • Trap/parse family: issue0085_..._trap_diag 3 failures are pre-existing host artifacts (temp-path normalization /c/Users/... vs $S; identical on clean master 9348b8d, verified in a baseline worktree).
  • cargo test --lib: parity with clean master — same 2 pre-existing environment failures (api::consumer_tests::issue2084_session_end_restores_host_process_state, executor::tests::unit_tests::shell_identity_is_used_for_default_dollar_zero "niu" binary-name), 1127 passed vs 1126 (+1 = the new unit test), 3 ignored both sides.
  • rustfmt disclosure: cargo fmt fails with os error 206 (filename-too-long) on this host in every checkout including clean master and the main repo, so the pre-commit hook cannot pass here; committed with --no-verify for that environment failure only, and both changed files pass rustfmt --check --edition 2021 individually.

Refs #2493, #2480. Draft — not to merge as-is.

…ewline, not `;` (Refs #2493)

The modernish die trap's `{ { ...; } & } 8<&0` body killed every
respawned async child at parse and the die path died silently
(rc 128, no diagnostic). Root cause: three closer arms in
executor/command_text.rs emitted `; }` / `; ` unconditionally, so a
body whose last command printed `... &` serialized as `{ ... &; }` —
unparseable under GNU's list grammar (parse.y:1275/1290 admit only
newline_list after `&`). Any defined function containing the shape
corrupted EVERY later async respawn through
background_function_prelude (the `-c` child died at parse before
running the die message block — the silent face).

GNU ownership: print_cmd.c:693-728 print_group_command closes a
brace group through `semicolon()` (print_cmd.c:1520-1530), which
prints NOTHING after `... &' — the exact rule the sequence joiner,
loop/if closers and the prelude already port (rubash#294). Fixed
arms: bash_command_source_text_render's brace_group arm,
coproc_command_source_text's body, and
function_command_source_text's BraceGroup closer — all now route
through push_command_separator (#294/#2388 spelling: newline after
`&` or a heredoc delimiter line, `; ` otherwise).

Verified against WSL GNU Bash 5.3.20 (/usr/local/bin/bash, script
files): the issue repro (`. bin/modernish`; `die "direct die call"`)
now prints `<script> died: direct die call` on stderr and exits 128,
byte-matching the GNU probe; the `_Msh_seq_bc` hardened-error repro
prints `repro.sh died: seq: 'bc' wrote error: ...`. Residual (out of
scope, filed as #2543): the respawn prelude double-defines readonly
functions (40-line stderr noise wall).

Tests: tests/issue2493_die_trap_redirect_bg_parse.rs (minimal parse
shape, die-chain diagnostic delivery, modernish real state when the
fixture exists) + a command_text unit test pinning the newline
closers. cargo test --lib parity with clean master 9348b8d (same 2
pre-existing env failures, +1 new pass); respawn/trap/parse families
green. rustfmt run per-file: cargo fmt itself fails with os error
206 on this host for every checkout including clean master (hook
bypassed with --no-verify for that environment failure only; both
changed files pass rustfmt --check individually).
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 04:00
@caomengxuan666
caomengxuan666 merged commit 9c8e211 into master Nov 2, 2026
13 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2545 PASS — new pin issue2493_die_trap_redirect_bg_parse 3/3 ×2 on Windows. No unix-gated faces in this file. No BLOCKER.

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 2ce27e6 (contains this merge), oracle wsl /usr/local/bin/bash 5.3.20. Logs: D:/repo/win-accept/wave3/logs/issue2493_die_trap_redirect_bg_parse.r{1,2}.log.

@caomengxuan666
caomengxuan666 deleted the fix/2493-die-trap-redirect-bg-parse branch November 3, 2026 09:05
SpookyWaste pushed a commit to SpookyWaste/rubash that referenced this pull request Nov 12, 2026
…y inheritance no longer errors (Refs unixwin#2543 unixwin#2493)

A respawned async/procsub child re-defined every function in the
parent's table through background_function_prelude while the
__RUBASH_READONLY_FUNCTIONS attribute transport had already landed in
the child's env at startup (init.rs), so every readonly-marked function
tripped the `readonly function' guard at its OWN inheritance
definition — a 40-line `name: readonly function' noise wall per async
respawn under modernish, and the define was refused, dropping the
function from the child entirely (`h: command not found').

Root cause 1 — wrong guard invariant. GNU execute_cmd.c:6325-6331
(execute_intern_function) raises `readonly function' only when
find_function() returns an EXISTING function that is readonly: the
guard protects a redefinition, never a first definition. rubash's
execute_function_def checked the READONLY_FUNCTIONS mark alone, so a
transported mark blocked the inheritance define. Fix: gate on
self.shell_state.functions containing the name, as GNU gates on
find_function(). The prelude's first definition is admitted; a
redefinition of the now-existing readonly function by the child's body
still errors once (GNU-verified).

Root cause 2 — double transport. GNU variables.c:397-446
(initialize_shell_variables) defines each env-imported exported
function exactly once (parse_and_execute SEVAL_FUNCDEF at :418); a
forked async child (execute_cmd.c:1761-1763 execute_in_subshell ->
make_child) copies the table by memory and defines nothing. Where a
BASH_FUNC_<name>%% carrier rides this process's environment into the
child, the startup import already defines the function, so
background_function_prelude re-rendering it is a second definition —
a redefinition once the function exists. Fix: the prelude (and the
FORK_FN_SPANS transport and the prelude-def count that feed the
child's line-offset math) now iterate background_prelude_function_entries,
which excludes exactly the names whose carrier parses to the same
definition as the current table (both sides rendered through the same
canonical exportstr printer; a stale carrier after a parent redefine
does not skip — the fresh rendering wins, as GNU's fork would carry).

Not the double-define the issue suspected: modernish functions are
NOT exported (probed: declare -Fx empty, env has no BASH_FUNC_*); the
40-line wall was root cause 1 alone — mark transport vs prelude.

Verified against WSL GNU Bash 5.3.20 script-file probes, byte-for-byte:
- `{ h; } &` with h readonly: child prints H silently (pre-fix: error
  + command not found).
- `{ k(){ NEW; }; k; } &` with k readonly: exactly one `k: readonly
  function', k keeps ORIG, parent unaffected.
- parent-level readonly redefinition still errors (rc=1).
- BASH_FUNC_g%% carrier + `readonly -f g` + `{ g; } &`: ENVG, silent.
- stale carrier + parent redefine: child prints NEWBODY, silent.
- modernish `use safe` + `{ sleep 0; } &` on top of the unixwin#2493 render
  fix (PR unixwin#2545): 0 stderr noise lines (was `assign: readonly
  function'). Patch applies cleanly onto PR unixwin#2545's head; both issue
  suites pass together on the stacked tree.

Tests: tests/issue2543_respawn_readonly_function_noise.rs (7 pins).
Regressions green: issue1704 (9), issue2136 (10), issue2500 (5),
issue1275 (1), procsub families 1021/1220/1221/1223/1460; unix-gated
respawn suites (2058/2072/2332/2429) collect 0 tests on this host as
on clean master. cargo test --lib: 1126 passed / 2 failed / 3 ignored
— identical to pristine master 9348b8d (the 2 failures,
issue2084_session_end_restores_host_process_state and
shell_identity_is_used_for_default_dollar_zero, fail identically on
clean master).

rustfmt: cargo fmt fails on this host with os error 206 for every
checkout including clean master (documented in PR unixwin#2545); the three
changed files pass `rustfmt --edition 2021 --check` individually, and
the pre-commit hook was bypassed with --no-verify for that environment
failure only.
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.

modernish die trap: body with '0<&8 & ;;' fails to parse and the die path is silent (rc 128, no output)

1 participant