Skip to content

WIP: fix #2368 foreign-spawn post-wait truncation of dup targets - #2377

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/2368-spawn-trunc
Oct 31, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/2368-spawn-trunc

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2368 (D1b residual of the #2322 wave; sibling faces fixed by #2367).

Defect: a redirect list with both a {var} dup and a pending close of the dup's source (9>o1.txt {w}>&9 {v}>&-, v=9) made the stage spawner's post-wait finish path re-open the dup's target with O_TRUNC after wait4, destroying the foreign child's bytes.

Isolated call site (temporary backtrace instrumentation in create_redirect_output / FileFd::open_write, removed before these commits):
create_redirect_output ← open_command_output_target ← apply_ordered_output_redirects ← route_builtin_buffered_output ← write_external_captured_streams ← spawn_external_process ← execute_external_inner. All four O_TRUNC opens of the target went through create_redirect_output; the fourth fired after wait4.

Mechanism: the output close routes the spawn through the parent-drained capture path; its post-wait routing walk re-ran the full ordered redirect walk with plain apply semantics, so the numbered 9>o1.txt entry performed its create/truncate validation open a second time on the file the child had just filled. Same disease as #2255, whose cure (OutputFdState::resolve_only_numbered) the pipeline routing walk already uses; this finish route never got it. Command-substitution capture enters the same route and had the same bug without any close entry.

Fix (category-shaped, not spelling-shaped): thread resolve_only_numbered through route_builtin_buffered_output_ordered and engage it from write_external_captured_streams via route_builtin_buffered_output_resolve_only_numbered. The child's spawn delivery realized every numbered binding as a real descriptor, so the walk resolves numbered entries purely as dup sources and only routes the drained fd-1/fd-2 bytes. Builtin callers keep the plain walk. GNU contract: one ordered walk in the child (redir.c do_redirections, redir.c:246); nothing re-opens a dup target post-child.

Verification (script-file probes vs GNU bash 5.3.20 oracle): issue shape o1=[W10] (was []); strace: target O_TRUNC opens 4 → 3, all before wait4; dup-only and unrelated-close controls byte-identical to GNU; comsub variants byte-identical; drained stderr through the dup still routes into the target file. Pins: tests/issue2368_foreign_spawn_post_wait_trunc_regressions.rs 6/6 under the scrubbed-fd runner; neighbor families issue2310 8/8, issue2314 12/12, issue2281 6/6, issue2308 8/8 re-run green scrubbed. The #2322 pin file (from #2367's branch) run against this master-based tree gives identical pass/fail sets with and without this fix (7 pass / 6 fail — the 6 failures are #2367's own D1a/D2 pins, absent from master).

WIP draft for collector harvest; do not merge from the cluster side.

caomengxuan666 and others added 3 commits October 31, 2026 15:24
…argets

A redirect list holding both a {var} dup and a pending close of the
dup's source (9>o1.txt {w}>&9 {v}>&-, v=9) routed the foreign spawn
through the parent-drained capture path. Its post-wait routing walk
re-ran the full ordered redirect walk with plain apply semantics, so
the numbered 9>o1.txt entry performed its create/truncate validation
open a second time — after wait4 — destroying the bytes the child
had written through the dup descriptor.

GNU applies a command's redirections as one ordered walk in the
child (redir.c do_redirections, redir.c:246); nothing re-opens a
dup's target after the child ran.

Thread the OutputFdState::resolve_only_numbered mode that the
pipeline routing walk already uses (rubash#2255) through
route_builtin_buffered_output_ordered, and engage it from
write_external_captured_streams via a new
route_builtin_buffered_output_resolve_only_numbered wrapper: the
child's spawn delivery realized every numbered binding as a real
descriptor, so the post-wait walk only resolves numbered entries as
dup sources and routes the drained fd-1/fd-2 bytes. Builtin callers
keep the plain walk (flag off). Command-substitution capture takes
the same route and is fixed by the same change.

Refs #2368
Issue shape (dup + pending close of the dup's source, foreign
/usr/bin/bash child), the two bisection controls (dup-only,
unrelated close), a two-command sequence, the command-substitution
capture variant, and a drained-stderr-through-dup routing guard.
Expectations byte-verified against GNU bash 5.3.20.

Refs #2368
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