Repository navigation
fix(pipeline): keep merged-stream order in pipeline stages - #3399
Conversation
Pipeline stages lost cross-stream order when their stdout and stderr merged into one sink (trap -p lists vs diagnostics, printf output vs its diagnostics): GNU's execute_pipeline forks each element and the element's own redirections run in the child (execute_cmd.c:2620, do_redirections redir.c:237), so writes to the same descriptor are naturally ordered, and bash line-buffers stdout (shell.c:1921-1922 sh_setlinebuf). Rubash had three instances of the same disease, all in the merge layer rather than in any builtin: (1) a sub-shell stage's chunk routing fell through to pair-block writes (all of stdout, then all of stderr) whenever apply_ordered_output_redirects found nothing to do — but the stage's clone had already had its fd 1/2 entries stripped, so the walk legitimately had nothing to do while the fd table already held a merged binding (unixwin#2689); now it falls through to the chunk walk only when redirect_failed and first_open_error are both empty; (2) the inline Trap/Printf arms collected their ordered writes into two separate Vecs and returned a (stdout, stderr) tuple with the interleave already lost — both arms now go through OrderedOutputLog (unixwin#2699), recorded in the new Executor::pipeline_stage_ordered_chunks field (cleared at execute_pipeline_stage_inner entry; initialized in init.rs and command_substitution.rs), and the driver rebuilds the merged stream from the log through one shared sink when both streams resolve to the same sink (2>&1, 1>&2, >f 2>&1, including the ProcessStderr==Stderr 1>&2 variant), keeping the order-dependence of 2>&1 >f; (3) printf's unixwin#1134 error model was wrong: render() collected all output into one String and diagnostics separately, with consumers writing all diagnostics first, on the claim that GNU's stdio fully buffers stdout — the oracle (printf '%d\n' 5 bad 7 2>&1 | cat and the direct form both give 5, diagnostic, 0, 7) shows bash line-buffers stdout, so RenderedPrintf is now a production-ordered sequence of PrintfSegment::{Text, Error} (with %n counts summed over Text segments and -v still joining Text), execute_printf goes through the log + chunk routing, and the now-dead write_buffered_builtin_output_stderr_first plus the two stderr_first parameters were removed with four comments corrected. unixwin#1134's single-parameter test still passes (its diagnostic precedes its own converted value, which the interleave model also satisfies). Refs unixwin#3324 Regression pin tests/issue3324_pipeline_stage_merged_order.rs (18 tests; fail-first 7 passed / 11 failed on the unpatched tree, 18/18 pass after; supervisor verified both rounds with the patch method). Adjacent: printf/trap/type/help/pipeline/echo/declare families 149 targets, 976 pass / 10 fail — the 10 failures are pre-existing, verified by the supervisor on the unpatched tree with identical counts (issue1100_printf_option_parsing 4/5 and issue3046_exec_pipeline_chardev_stdin 10/5 on both trees; unixwin#1100 expects a bash: prefix where the worktree binary reports its own path, unixwin#3046 is an exec + char-device stdin pipeline read-count issue). lib printf unit tests 56/56; unixwin#1134 1/1, unixwin#2699 7/7 (supervisor re-ran both; the agent's report said 8/8 for unixwin#2699, the supervisor's own count is 7/7, all green). cargo fmt clean.
|
Thanks, @Uarz — received. This is queued for captain review (per the owner's rule, external PRs are reviewed by the captain first); no merge or change request from intake. First pass on the diff: the change matches the body — the three merge-layer defects are addressed where claimed: the subshell-stage chunk routing now falls through to the chunk walk only when CI is fully green here (10 checks passed + 1 skipping). One thing flagged for the captain's review, from your Owed §1: the newly found |
Captain review — PASSReviewed at head Measured
Delimitations attached to this review
Merge conditionsUpdate-branch first (the PR's 9 files intersect master's interval since the |
Fixes #3324
What was fixed
Pipeline stages lost cross-stream order when their stdout and stderr merged into one sink (
trap -plists vs diagnostics, printf output vs its diagnostics): GNU'sexecute_pipelineforks each element and the element's own redirections run in the child (execute_cmd.c:2620,do_redirectionsredir.c:237), so writes to the same descriptor are naturally ordered, and bash line-buffers stdout (shell.c:1921-1922sh_setlinebuf). Rubash had three instances of the same disease, all in the merge layer rather than in any builtin:apply_ordered_output_redirectsfound nothing to do — but the stage's clone had already had its fd 1/2 entries stripped, so the walk legitimately had nothing to do while the fd table already held a merged binding ([A2-02] command <external> as a pipeline stage drops the command's own redirections #2689). Now it falls through to the chunk walk only whenredirect_failedandfirst_open_errorare both empty.Vecs and returned a(stdout, stderr)tuple with the interleave already lost — both arms now go throughOrderedOutputLog([A3-R58A] Multi-argument builtin stdout/stderr interleaving lost in the shared buffered-output path #2699), recorded in the newExecutor::pipeline_stage_ordered_chunksfield (cleared atexecute_pipeline_stage_innerentry; initialized ininit.rsandcommand_substitution.rs), and the driver rebuilds the merged stream from the log through one shared sink when both streams resolve to the same sink (2>&1,1>&2,>f 2>&1, including theProcessStderr≡Stderr1>&2variant), keeping the order-dependence of2>&1 >f.printfinvalid-number diagnostic comes after the output value in a merged 2>&1 stream #1134 error model was wrong:render()collected all output into oneStringand diagnostics separately, with consumers writing all diagnostics first, on the claim that GNU's stdio fully buffers stdout — the oracle (printf '%d\n' 5 bad 7 2>&1 | catand the direct form both give5, diagnostic,0,7) shows bash line-buffers stdout, soRenderedPrintfis now a production-ordered sequence ofPrintfSegment::{Text, Error}(with%ncounts summed over Text segments and-vstill joining Text),execute_printfgoes through the log + chunk routing, and the now-deadwrite_buffered_builtin_output_stderr_firstplus the twostderr_firstparameters were removed with four comments corrected. [bug]printfinvalid-number diagnostic comes after the output value in a merged 2>&1 stream #1134's single-parameter test still passes (its diagnostic precedes its own converted value, which the interleave model also satisfies).What was deleted
write_buffered_builtin_output_stderr_firstand thestderr_firstparameters ofroute_builtin_buffered_output_ordered/write_buffered_builtin_output_ordered(dead after the interleave model replaced the stderr-first model), plus four comments stating the old fully-buffered model. No files, features, or tests were removed.Verification (all run, per-item counts)
tests/issue3324_pipeline_stage_merged_order.rs: 18/18 pass (canon/list-first/sandwich/multi-stage/-Pboth directions/printf mixed/1>&2/subshell control/direct control/type both directions/file merge/non-merge/group/|&/unredirected split/direct-form printf). Fail-first on the unpatched tree (supervisor re-ran with the patch method): 7 passed / 11 failed.issue1100_printf_option_parsing4 passed / 5 failed andissue3046_exec_pipeline_chardev_stdin10 passed / 5 failed on both trees; [bug] Leading-dash printf format: different option-parsing + error text #1100 expects abash:prefix where the worktree binary reports its own path, exec in a pipeline/subshell ignores stdin redirect from a character device (reads EOF) and drops stderr redirects #3046 is anexec+ char-device stdin pipeline read-count issue).printfinvalid-number diagnostic comes after the output value in a merged 2>&1 stream #1134 1/1, [A3-R58A] Multi-argument builtin stdout/stderr interleaving lost in the shared buffered-output path #2699 7/7 (supervisor re-ran both; the agent's report said 8/8 for [A3-R58A] Multi-argument builtin stdout/stderr interleaving lost in the shared buffered-output path #2699, the supervisor's own count is 7/7, all green).5/diagnostic/0/7) matches the oracle; closed-stdout (exec >&-; printf "end\n") diagnostic byte-identical with the oracle.cargo build -j 2clean,cargo fmt --checkclean.src/lexer/continuation.rsuntouched.Owed
declare -pmixed form still reversed (newly found here, needs a separate issue):v=1; declare -p BADNAME v 2>&1 | cat— GNU: diagnostic first, listing after; rubash: listing first, diagnostic after. Different mechanism from this fix: declare's diagnostics go throughDiag::pending, rendered only after the handler returns viaflush_with, and the funnel uses the pair route. Fixing it needs declare's funnel (execute_declareand 2 other sites) to go through the log + flush each Diag in the print loop — a big declare-family change, not folded into this fix.trap "echo bye" EXIT; type ls | catprintsbyeonce when the stage exits and again at script end; GNU prints it once.execute_builtin_pipeline_stage(pipeline_stages.rs:927) lacksreset_for_subshell_child. The pin'stypecases deliberately avoid installing an EXIT trap.write error: Bad file descriptoronce (before the fix, each stream reported once; GNU loses the diagnostic entirely because fd 2 is closed, so it is unobservable). Closer to GNU; no separate issue filed.command_substitution.rs:1042,command_substitution_pipelines.rs:930) take only stdout and already drop diagnostics — the captured value contains stdout only, so cross-stream order is unobservable there; untouched.