Repository navigation
WIP: stream single-shell-stage pipelines on real pipes (rubash#2483) - #2516
Conversation
…ash#2483)
GNU forks every pipeline element onto real pipes (execute_cmd.c:2620
execute_pipeline; do_piping at execute_cmd.c:2741 binds the element's
fd 1 to the pipe), so element output flows as produced and an
element's direct fd-2 writes land on the shared stream in real time;
a builtin/function element flushes its stdout into the bound fd at
every simple-command boundary (execute_cmd.c:5550 fflush in
execute_subshell_builtin_or_function; execute_cmd.c:5648 fflush +
fpurge in execute_builtin_or_function). rubash's sequential stage
loop instead captured each stage's output and emitted it at stage
completion, so in `bats-exec-suite | validator_fn | bats-format-tap`
the suite's late direct stderr line ("Marking test run as failed...")
overtook the TAP plan line (bats cells 115/116).
- New execute_streaming_sandwich_pipeline: a pipeline with exactly
one shell-side stage (function/compound) amid external members
spawns every external member on real OS pipes first, then runs the
shell stage on the main executor between the pipe ends — fd 0
bound to the upstream pipe like the #761 streaming tail, fd 1
armed to the downstream pipe like the #1605 shell-writer — so all
members produce and consume concurrently.
- Live stage stderr: the function/compound stage funnels leave the
stage clone's stderr uncaptured when a concurrent driver arms the
new Executor::pipeline_stage_stderr_live (parent uncaptured, the
stage claims no fd-2 redirect of its own, the pipeline's fd 2 is
the ambient Stderr endpoint), so stage diagnostics interleave in
production order; the shell-writer path arms it for its producer.
- The function stage funnel now keeps FUNCTION_STDIN absent when
pipeline_stdin_stream is armed — the compound funnel already keyed
on that streaming state; a streamed function stage used to read
the empty staged mirror and see EOF while the producer wrote.
- An element spelled `exec prog args...` is classified by its
operand for spec purposes in the sandwich: GNU's element is
forked first, and its exec_builtin (builtins/exec.def) replaces
the child image via shell_execve, so the element IS the operand
program with the pipe bound. The bats pipeline's first element is
`exec bats-exec-suite ...`. Exec options and operand-less exec
keep the sequential path.
Pins: tests/issue2483_stream_drain.rs — six tests; order
expectations measured against GNU bash 5.3.20 from script files
with stdout/stderr merged onto one file and 0.4s event spacing.
mini2212_t115/t116 merged output is now byte-identical to GNU.
Residual: pipelines with two or more shell-side stages (fn | fn)
keep the sequential loop and its completion order — the executor
cannot move to a worker thread (its fd endpoints are Rc-based), so
true concurrency there needs a detached-executor design.
Refs #2483
12a19c7 to
cb9040b
Compare
|
[win-acceptance] #2516 PASS — pin Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 9348b8d (contains this merge), oracle |
Refs #2483
Problem
In
bats-exec-suite | bats_test_count_validator | bats-format-tap(bats cells 115/116), rubash emitted the suite's late direct stderr line (Marking test run as failed due to `bats:focus` tag...) before the TAP plan line1..1; GNU emits it after all TAP output. Byte set identical, order different.Root cause: the sequential stage loop is capture-then-emit — each stage's output is assembled at stage completion. GNU has no such step:
execute_pipeline(execute_cmd.c:2620) forks every element onto real pipes (do_piping, execute_cmd.c:2741), and a builtin/function element flushes stdout into the bound fd at every simple-command boundary (execute_cmd.c:5550, :5648).Fix (mechanism, not a bats special case)
execute_streaming_sandwich_pipeline(new fast path inpipeline_exec.rs): a pipeline with exactly one shell-side stage (function or compound) amid external members spawns every external member on real OS pipes first, then runs the shell stage on the main executor between the pipe ends — fd 0 bound to the upstream pipe like the pipeline: unbounded external producer + shell-side tail never returns —yes | truedrains into RAM at ~250 MB/s (hangs on HEAD too) #761 streaming tail, fd 1 armed to the downstream pipe like the [bug] Pipeline last stage gets a fresh pipe as fd 1 instead of inheriting the shell stdout (residual of #1454) #1605 shell-writer. All members produce/consume concurrently; streams interleave in production order. Admission is narrow (one plain shell stage at index ≥ 1, no enclosing capture, no ambient2>&1merge, no DEBUG trap); anything else keeps the existing paths.Executor::pipeline_stage_stderr_live. When a concurrent driver arms it (parent uncaptured, stage claims no fd-2 redirect, pipeline fd 2 is the ambientStderrendpoint), the function/compound stage funnels leave the stage clone's stderr uncaptured so diagnostics write the live fd 2 as produced. The shell-writer path arms it for its producer too (its producer stderr used to be deferred to pipeline end).FUNCTION_STDINabsent whenpipeline_stdin_streamis armed (the compound funnel already keyed on that state); a streamed function stage previously read the empty staged mirror and saw EOF.exec prog …element classification: GNU forks the element first and itsexec_builtin(builtins/exec.def) replaces the child image viashell_execve, so the element IS the operand program with the pipe bound — the sandwich specs an exec-headed stage by its operand. This is the bats pipeline's actual first element (exec bats-exec-suite …). Exec options / operand-less exec keep the sequential path.Untouched: the #2475
route_builtin_buffered_outputrouting (this change is drain timing, not routing),src/executor/continuation.rs,src/lexer/continuation.rs.Verification (local, scrubbed fd environment)
tests/issue2483_stream_drain.rs: 6/6 (ext|fn|ext, ext|fn tail, fn|ext live stderr, ext|compound|ext, fn|fn content control, exec|fn|ext + PIPESTATUS). Expectations measured against GNU bash 5.3.20 from script files, merged stdout+stderr on one file, 0.4 s event spacing.issue2212_bats_remainder_regressions15/15,issue2475_tempenv_invalid_identifier_env_redirect6/6,issue2385_complementary_end19/19 (all viascrubbed_cargo.py cargo test).mini2212_t115.bats/mini2212_t116.batsunder the h-bats harness — TAP output byte-identical to GNU 5.3.20 on both (before: Marking line overtook1..1; an earlier run also appeared to hang at 120 s under load ≈ 18 — that was machine contention, the inner run completes in ~14 s unloaded).Residual (explicit)
Pipelines with two or more shell-side stages (fn|fn, cluster C's mini) keep the sequential loop and its completion order: the executor cannot move to a worker thread (fd endpoints are
Rc-based; background jobs are processes, and there is no executor-on-thread precedent), and respawn-per-stage is a product-level change. Content and PIPESTATUS stay correct there (pinned as a control). This wants a detached-executor design decision.Harvest note (overlap)
Touches
src/executor/pipeline_exec.rs(+498, pure insertions),pipeline_stages.rs,mod.rs,init.rs,command_substitution.rs— the pipeline/fd area cluster H is also working in (external_setupneighbourhood). Base is 15b457f; rebase at harvest if H lands first.