Skip to content

WIP: fix(executor): let fd pins win slots held by stdio pipe leftovers (rubash#2255) - #2284

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/2255-stage-regression
Oct 30, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/2255-stage-regression

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: adjudication fix for the two red issue2255 stage pins — do not merge without the main session's harvest.

What was wrong

The two pins were never green on any real build (red at the fix's own commit afc1845, red on the composed tree d6dc304, red on master). The 7/7 recorded at PR #2262's harvest was an environment artifact: the pins' outcome depends on the fd layout the rubash process inherits at launch. With extra fds held open by the launcher they pass; in the canonical cargo-test layout they fail.

Mechanism (strace-verified): in a pipeline stage child, pin_ambient_fds_in_child's exec-error-pipe guard sees an anonymous pipe at the pin's target slot — but that pipe is the stage's own stdout pipe, dup2'd onto fd 1 by the spawn machinery and left open at its original number (3) until exec. The guard drops the pin, so the stage's own 3<seed / 3>file never reaches a foreign child (seed orphaned at fd 8; fd 3 stays the write-only pipe: cat EBADF / output leaks down the pipe). GNU: execute_cmd.c:2620 execute_pipeline forks the element and redir.c:237 do_redirections applies its redirect list in the child — the binding wins its slot unconditionally. The pins encode GNU behavior exactly (oracle-verified byte-for-byte); the product was wrong.

Fix

One mechanism-level change in pin_ambient_fds_in_child (src/executor/external_inner.rs), shared by the simple-command path and both pipeline spawner faces: a FIFO at the target only keeps the guard's protection when it is NOT one of the child's stdio pipes under a second number. Both ends of a pipe share one (dev, inode), so an fstat identity match against fds 0/1/2 identifies the stdio leftover exactly; std's exec-error channel is a pipe of its own, never matches, and stays protected.

Verification (this branch, clean fd layout)

  • tests/issue2255_pipeline_stage_redirects.rs: 5/7 → 7/7
  • Controls unchanged vs master c17160e baselines: issue1963 6/6, issue2235 11/11, issue2250 5/5; issue2249 8/9 and issue2270 2/10 keep their pre-existing master reds (same pins by name).

Full adjudication (bisect table, oracle transcripts, strace decode, and one residual discovery: a stage-0 numbered input in a ≥3-stage pipeline still lacks fd 3 in the child — a different runner face) is in the adjudication REPORT held by the main session.

Refs #2255

…bash#2255)

pin_ambient_fds_in_child refused to dup2 a pin onto a slot holding
an anonymous pipe, to protect std's exec-error channel. In a
pipeline stage child the stage's own stdout pipe is dup2'd onto
fd 1 by the spawn machinery and left open at its original number
until exec; when that number is the pin's target (fd 3 in a clean
fd layout), the guard mistook it for the error channel and dropped
the pin — the stage's own numbered redirect (GNU: execute_cmd.c:2620
execute_pipeline forks the element and redir.c:237 do_redirections
applies its list in the child) never took effect for a foreign
child: `sh -c 'cat <&3' 3<seed | wc -c` printed 0 with cat EBADF,
and `3>o.txt` output leaked down the pipe. The miss was
layout-dependent: with extra fds inherited at launch the pipe
lands elsewhere and the pins pass.

Tell the two pipes apart by identity: both ends of a pipe share
one (dev, inode), so a FIFO matching a stdio descriptor (0/1/2)
is the stdio leftover and the pin wins the slot; the exec-error
channel is a pipe of its own, never matches, and keeps its
protection. Pins: tests/issue2255_pipeline_stage_redirects.rs
goes 5/7 -> 7/7; controls issue1963 6/6, issue2235 11/11,
issue2250 5/5 unchanged, issue2249 8/9 and issue2270 2/10 keep
their pre-existing master reds.

Refs #2255
caomengxuan666 and others added 2 commits October 31, 2026 04:48
The dev_t/ino_t aliases are u32 on armv7 Android while the raw
stat fields are u64, so the tuple construction failed the
cross-target check with E0308. Only equality is ever tested,
so widen both fields to u64.

Refs #2255
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 30, 2026 21:03
@caomengxuan666
caomengxuan666 merged commit 288ee28 into master Oct 30, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 30, 2026
…-opening them; narrow the §8 decline to non-stdio-dup bindings

Collector batch on eecde6d: issue2255 target 9/11 — both numbered
OUTPUT pins failed (the pre-existing 2-stage pin and the new
3-stage pin), each on the o.txt contents poll. Adjudication against
the master-content build (0d253a5, 7/7 there):

(1) >=3-stage stage-0 output WAS already broken on master, by the
SAME §8 mechanism as the input face (shell-writer guard skip: the
child had no fd 3 — `sh: 1: 3: Bad file descriptor`, o.txt created
empty by the delivery open). Not a different mechanism at stage 0,
so the pin stays and the decline already routes the shape here.

(2) The decline exposed a SECOND, distinct mechanism in the
sequential composition: route_pipeline_stage_streams re-runs the
ordered output walk AFTER the stage executed, and the walk's
open_command_output_target performs a create/truncate validation
open for every numbered target. GNU performs ONE open per
redirection, in the element's fork (execute_cmd.c:2620
execute_pipeline; redir.c:237 do_redirections). Slow-writer
probes: the child's bytes land in the file during its run, then
the post-stage open truncates them away. On master this same
mechanism wipes MID- and LAST-stage numbered output (the binding
is delivered there; the content is destroyed at routing time) —
strace shows the shell process opening the target O_TRUNC a
fourth time, after the child exited.

Fix: OutputFdState gains resolve_only_numbered, set only by
route_pipeline_stage_streams. The walk still resolves the full
ordered list (a dup chain like `3>f 2>&3` resolves and its
captured stream flushes by append exactly as before), but for
fd > 2 it records the target with no filesystem open: the plain
arm skips its validation open, and the /dev/fd arm records the
alias resolver's path instead of fresh-opening it again. Fds 1/2
keep full apply semantics — this walk IS their realization, and
their opens stay validated pre-run by
pipeline_stage_stdio_targets. Three new pins cover the
mid-stage, last-stage, and dup-chain output faces.

(3) The original decline also regressed an unpinned 2-stage
shape: `sh -c 'echo o >&3' 3>&1 | wc -c` printed 2 on master (the
writer path delivers a stdio dup there — the producer child's
only foreign pipe is its own stream pipe, the stdio alias the
#2284 guard recognizes) but EBADF after declining, because the
sequential spawner's delivery cannot dup a capture-pipe stdio
endpoint onto a numbered slot. That sequential defect is
pre-existing (mid/last-stage stdio dups fail on master too) and
is not a §8 shape, so the decline now exempts pure stdio dups
(DuplicateInput/DuplicateOutput carrying an `&N`/`&N-` spelling
of fd 0/1/2, no {var}) via redirect_is_stdio_dup. File, close,
and dup-of-a-non-stdio-fd bindings keep the decline: `3>&-` at
2 stages stays fixed by it, and dup-of-file / append / readwrite
producers verified fixed by it at >=3 stages. At >=3 stages the
stdio dup is lost on both paths today; the exemption preserves
master behaviour there instead of changing its shape.

Verification (this workdir, scrubbed fd environment):
issue2255 target 14/14 (11 + 3 new), issue1963 6/6, issue2235
11/11, issue2250 5/5, issue2256 25/25; regression goldens show
only the four known local stock reds (master-red in this VM,
CI-green); streaming spot checks (yes | head -3, yes | head -1 |
wc -l, compound writer | head -2) intact. A 19-case GNU-oracle
battery over the output/dup face matches GNU on 17; the two
remaining diffs (stage `2>file`, stage `3>/dev/fd/N` fresh-open)
are byte-identical on the master build — pre-existing, untouched
here, and recorded in the cluster REPORT addendum.

Refs #2255
@caomengxuan666
caomengxuan666 deleted the fix/2255-stage-regression branch November 2, 2026 01:08
SpookyWaste pushed a commit to SpookyWaste/rubash that referenced this pull request Nov 12, 2026
…wn numbered redirects

Section 8 of the reopened issue: in a >=3-stage pipeline whose
downstream stages are all concurrent-eligible externals
(`sh -c 'cat <&3' 3<seed.txt | cat | wc -c`), stage 0's own
numbered binding was lost entirely — the child had NO fd 3 at
all (GNU prints 10).

Root cause: execute_shell_writer_pipeline claimed stage 0 as a
"streamable external producer" without inspecting its ordered
redirects list, and run_shell_writer_pipeline spawns the
consumers on pre-created pipes BEFORE running the producer
through the sequential stage spawner. The producer child is
therefore forked while foreign anonymous pipes (the consumer
chain's ends) occupy the low descriptor slots; one parks
exactly on the fd-delivery pin's target slot, and
pin_ambient_fds_in_child's exec-error-channel guard
(rubash#1626, stdio-alias refinement rubash#2284) cannot tell
that foreign pipe apart from std's error channel, so it skips
the pin — apply_external_fd_delivery had opened the binding,
but the dup2 onto its slot never ran (strace: the staged pin
copy is closed without a dup2). The 2-stage composition
escapes because its only foreign pipe is the stage's own
stream pipe, which the stdio-alias check recognizes; fd >= 4
targets escape because the foreign pipes only reach the
lowest slots; middle stages escape because the concurrent
specs already decline list-only members there.

Fix: decline the shell-writer path for an external producer
carrying a list-only redirect (fd > 2 or {var}) — the same
decline the concurrent admissions make for such members — so
the sequential stage loop spawns stage 0 in a clean fd layout
and the shared delivery lands (GNU: execute_cmd.c:2620
execute_pipeline forks every element; redir.c:237
do_redirections applies the element's own list in the child).
Compound and function producers bind numbered redirects
through their own stage funnels, not the pin, and keep the
streaming path. The unixwin#2284 pin guard is untouched.

Pins: four new tests in tests/issue2255_pipeline_stage_redirects.rs
(stage-0 input in 3- and 4-stage pipelines, stage-0 output in a
3-stage pipeline, middle-stage control). Per compile rationing
they are NOT run locally; the collector batch-verifies the full
issue2255 target under a scrubbed fd environment.

Refs unixwin#2255
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