Repository navigation
WIP: fix #2368 foreign-spawn post-wait truncation of dup targets - #2377
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 afterwait4, 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 throughcreate_redirect_output; the fourth fired afterwait4.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.txtentry 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_numberedthroughroute_builtin_buffered_output_orderedand engage it fromwrite_external_captured_streamsviaroute_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 beforewait4; 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.rs6/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.