Skip to content

WIP: stage spawner faces — stdio-dup channel on rebound slots (#2293) + shell-writer producer stderr routing (#2294) - #2299

Merged
caomengxuan666 merged 6 commits into
masterfrom
fix/stage-spawner-faces
Oct 31, 2026
Merged

caomengxuan666 merged 6 commits into
masterfrom
fix/stage-spawner-faces

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

WIP — fix cluster for the two pipeline-spawner faces filed from the #2289 residuals. Two commits, one per issue; mechanisms are distinct.

#2293 commit (d306fa0) — stage stdio dups copy the stage channel on rebound slots

GNU binds every pipeline element's fds 0/1/2 to the inter-element pipes before the element's own do_redirections walk runs (execute_cmd.c:2620 execute_pipeline; redir.c:237 do_redirections, dup2 at redir.c:1193), so a numbered dup of a stdio slot copies the descriptor as bound at the dup's position.

Diagnosis note: the issue's headline shapes (mid/last-stage 3>&1/3>&2 EBADF, combined producer) were already delivered by #2278's shared delivery — verified by oracle-differential probes against a #2278-branch build. The live divergence is the REBOUND face: when a later entry rebinds the dup's source slot, deliver_numbered_dup captured the shell's ambient descriptor parent-side (the simple-command model). On a stage that is wrong in both directions:

  • printf 'x\n' | sh -c 'echo o >&3; echo s' 3>&1 1>f.txt | wc -c — GNU: count 2, f.txt=[s]; rubash: o escaped to the shell's real stdout, count 0.
  • printf 'PAY\n' | sh -c 'cat <&3; cat' 3<&0 <seed2.txt | wc -c (shell stdin = a distinct file) — GNU: 15; rubash: 21 (ambient bytes instead of the stage payload).

Fix: apply_external_fd_delivery/deliver_numbered_dup take an optional StageStdioContext (simple-command face passes None, byte-unchanged). Stage + source 1/2 rebound → child-local pin (the wired slot IS the capture pipe). Stage + source 0 rebound → the spawner materializes the payload into a carrier pipe when a dup sourcing fd 0 precedes the last fd-0 entry, and the delivery pins the carrier read end; a live-inherited stage fd 0 keeps the ambient capture.

Pins: tests/issue2293_stage_stdio_dup_regressions.rs — 9 pins (5 guards incl. combined producer, 3 rebound fix pins, 1 no-rebind fd-0 guard), GNU bash 5.3.20 expectations.

#2294 commit (7e6e55f) — shell-writer producer's streams routed through its own redirects

The stage spawner wires a 2>file stage's stderr as a capture pipe precisely so the driver's routing walk places the payload; the sequential loop runs that walk per stage, but run_shell_writer_pipeline wrote the producer's captured stderr straight to the ambient fd 2 (its comment's premise — "the element's body applies it internally" — holds only for compound producers). Fix: route the producer's captured streams in the writer driver with the loop's exact gate (consume the pre-wired / restricted-refused latches, skip compound), folding a routing error into producer_error since the consumers are already spawned.

Pins: tests/issue2294_stage_stderr_redirect_regressions.rs — 6 pins (stage-0 2>file, 2>>file append, three-stage producer, mid/last guards, no-redirect control), GNU bash 5.3.20 expectations.

Local verification per compile rationing: cargo check and cargo check --test for both pin targets clean; pins NOT run locally (fd-sensitive — collector batch under scrubbed fds). Cluster REPORT.md in the workdir carries the full diagnosis log and two adjacent discoveries (own-list stdio-binding replay gap, both faces) that were deliberately NOT folded into these commits.

Refs #2293
Refs #2294

GNU binds every pipeline element's fds 0/1/2 to the inter-element
pipes before the element's own do_redirections walk runs
(execute_cmd.c:2620 execute_pipeline; redir.c:237 do_redirections,
dup2 at redir.c:1193), so a numbered dup of a stdio slot copies
the descriptor as bound at the dup's position in the element's
list. On the stage face that at-position binding is the stage
channel, but deliver_numbered_dup's rebound arm (a later entry
rebinds the source slot) captured the shell's AMBIENT descriptor
parent-side — the right model for a simple command, wrong for a
stage: `3>&1 1>f.txt` mid-pipeline sent the dup payload to the
shell's real stdout instead of the stage pipe, and
`3<&0 <seed2` read the shell's ambient stdin instead of the
stage payload.

- apply_external_fd_delivery/deliver_numbered_dup take an
  optional StageStdioContext; the simple-command face passes
  None and is byte-unchanged.
- Stage face, source 1/2 rebound: the child's wired slot IS the
  capture pipe, so the child-local pin is the delivery.
- Stage face, source 0 rebound: the stdin face wires the later
  winner into the child's fd 0, so the spawner materializes the
  payload (`input`) into a carrier pipe when a dup sourcing fd 0
  precedes the last fd-0 entry, and the delivery pins the
  carrier's read end; a live-inherited stage fd 0 keeps the
  ambient capture.

Pins (tests/issue2293_stage_stdio_dup_regressions.rs, GNU bash
5.3.20 expectations): mid/last-stage 3>&1 and 3>&2 guards and
the combined producer (already delivered by #2278's shared
delivery), plus the rebound faces — mid/last 3>&1 1>f.txt and
3<&0 <seed2 against a distinct ambient stdin (15 bytes GNU vs
21 for the ambient capture).

Refs #2293
…own redirects

GNU runs every pipeline element's own redirections inside the
element (execute_cmd.c:2620 execute_pipeline; redir.c:237
do_redirections). rubash's stage spawner wires a `2>file`
stage's stderr as a capture pipe precisely so the driver's
routing walk places the payload in the file, and the
sequential stage loop runs that walk for every stage — but
run_shell_writer_pipeline wrote the producer's captured stderr
straight to the ambient fd 2 under a comment whose premise
("the element's body applies it internally") holds only for
compound producers. For a plain external producer the payload
escaped to the shell's real stderr and the file was never
written: `sh -c 'echo e >&2' 2>e.txt | wc -c` left e.txt empty.

Route the producer's captured streams in the writer driver
with the loop's exact gate: consume the pre-wired and
restricted-refused latches, skip compound stages (internal
application) — a stage that wired its fds directly, or a
function stage that applied its redirects to its fd table,
claims the pre-wired latch, so nothing is placed twice. The
consumers are already spawned on this path, so a routing
error folds into producer_error (reaped, then returned)
instead of aborting the driver.

Pins (tests/issue2294_stage_stderr_redirect_regressions.rs,
GNU bash 5.3.20 expectations): stage-0 producer 2>file, the
2>>file append form, the three-stage producer face, mid- and
last-stage guards, and a no-redirect control (stderr still
escapes to the ambient fd 2).

Refs #2294
The #2293 child-local pin delivered the dup onto the child's wired
fd 1, but a sequential pipeline stage shares ONE capture pipe per
stdio slot and route_pipeline_stage_streams places that capture
whole at fd 1's FINAL target — so `3>&1 1>f.txt` still landed the
dup's bytes in f.txt beside fd 1's payload (GNU: count 2, f=[s];
the pinned branch: count 0, f=[o,s]).

- The stage spawner now materializes a carrier pipe for every
  numbered output dup of fd 1/2 that deliver_numbered_dup's
  step (4) serves from the StageStdioContext (the reachability
  predicate is mirrored exactly), a reader thread drains it
  while the stage runs, and the delivery pins the carrier's
  write end at the dup's slot (a later `4>&3` chains onto it).
- The drained payload is placed at the target the routing walk
  resolved for the dup's fd at its own list position
  (route_pipeline_stage_streams consumes the
  stage_dup_channel_payloads stash; the pre-wired merged-file
  branch places source-1/2 payloads as the stage's stdout/stderr
  returns directly). One slot, one binding: the last carrier
  per target wins, like GNU's sequential dup2.
- pin_ambient_fds_in_child's error-channel guard now also
  recognizes the sibling end of a pin source's pipe by (dev,
  inode) identity: a carrier's read end parked on the dup's
  target slot is part of the spawn's own fd arrangement, not
  std's exec-error channel (a pipe of its own, matching neither
  a stdio descriptor nor any pin source). Without this the
  carrier pin was skipped whenever the parent's fd allocation
  put the read end on the target number and the child saw
  EBADF at the dup's slot.

GNU spec: execute_cmd.c:2620 execute_pipeline binds the
inter-element pipes before the element's own walk runs;
redir.c:237 do_redirections, the dup2 at redir.c:1193 copies
the descriptor as bound at the dup's position in the list.

Pins: issue2293 9/9 (both rebound pins now pass), issue2294
6/6; controls issue2255 14/14, issue2278 13/13, issue2272
24/24, issue2270 10/10, issue2279 13/13; lib 985/985. The
2293/2294/2255 targets also pass under the scrubbed-fd runner.

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