Skip to content

WIP: stream single-shell-stage pipelines on real pipes (rubash#2483) - #2516

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2483-stream-drain
Nov 2, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2483-stream-drain

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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 line 1..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 in pipeline_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 | true drains 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 ambient 2>&1 merge, no DEBUG trap); anything else keeps the existing paths.
  • Live stage stderr: new Executor::pipeline_stage_stderr_live. When a concurrent driver arms it (parent uncaptured, stage claims no fd-2 redirect, pipeline fd 2 is the ambient Stderr endpoint), 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 funnel streaming-stdin parity: the function funnel now keeps FUNCTION_STDIN absent when pipeline_stdin_stream is 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 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 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_output routing (this change is drain timing, not routing), src/executor/continuation.rs, src/lexer/continuation.rs.

Verification (local, scrubbed fd environment)

  • New pins 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.
  • No regression: issue2212_bats_remainder_regressions 15/15, issue2475_tempenv_invalid_identifier_env_redirect 6/6, issue2385_complementary_end 19/19 (all via scrubbed_cargo.py cargo test).
  • Real minis: mini2212_t115.bats / mini2212_t116.bats under the h-bats harness — TAP output byte-identical to GNU 5.3.20 on both (before: Marking line overtook 1..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).
  • Probe matrix (0.4 s-separated shapes): ext|ext, ext|fn, ext|fn|ext, ext|compound, compound|ext, fn|ext all byte-identical to GNU. shapes.sh's same-instant write pairs (fn_ext/compound_ext tails) remain scheduler races in both shells — no causal order exists to pin.

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_setup neighbourhood). Base is 15b457f; rebase at harvest if H lands first.

…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
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2516 PASS — pin issue2483_stream_drain is #![cfg(unix)] (0 tests on Windows by design); live stream-ordering probe on Windows is byte-identical to WSL GNU Bash 5.3.20: a shell function stage amid external members (pre | mid | drain & where mid sleeps 0.3s before printing PLAN and a late direct-stderr writer fires mid-drain) produces the same merged-stream ordering (late stderr interleaves in production order, not deferred past the drain) and identical rc — probe D:/repo/win-accept/wave2/probes/p2483.*. Guard families green x2 on this tree in the same session: issue2212_bats_remainder_regressions 14/14, issue2502 6/6.

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 9348b8d (contains this merge), oracle wsl /usr/local/bin/bash 5.3.20 script-file comparison. No BLOCKER.

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