Skip to content

fix(executor): deliver stage {var} fd bindings through the shared applier space (rubash#2613) - #2628

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/2613-stage-varfd-delivery
Nov 3, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/2613-stage-varfd-delivery

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2613

Defect

A pipeline stage whose redirect list references, by number, the descriptor an earlier {var} entry in the same list allocated died with 10: Bad file descriptor and never ran:

exec 9>seed.txt
./w1.sh {v}>&9 9>o1.txt 1>&10 | cat >/dev/null   # GNU: runs, seed.txt gets the payload

GNU runs ONE do_redirections walk per pipeline element (execute_cmd.c:2620 execute_pipeline -> redir.c:237 do_redirections) and the {var} entries allocate their descriptor inside that walk (redir.c:1470 redir_varassign) — there is no stage dialect. rubash judged the stage's list in three descriptor spaces that never contain the applier's allocation: the stage reject gate (fd table + numbered overlay only; its fd_var arm validated the source then continued without recording the allocation), the stage output preflight (seeded state, fd_var entries skipped), and the post-execution routing walk (runs after the stage executor undid the applier). The delivery path itself (apply_external_fd_delivery) was already the shared one — the stage's auxiliary walks were the dialect.

Fix — one applier, one descriptor space

Every stage auxiliary walk now runs bracketed by the SAME dynamic-fd applier the plain-command funnel runs ahead of its faces (apply -> judge -> undo), gated on the list carrying a {var} entry; lists without one take the exact previous path:

  • src/executor/pipeline_exec.rs — the stage reject gate in execute_pipeline_stage_inner sits inside an applier bracket; an applier failure verdict kills the stage with the captured pipe bytes final, mirroring the existing preflight arms and the plain funnel's return on the same verdict. Only the gate is bracketed: the word-expansion faces below still expand $v against the outer binding (GNU expands words before do_redirections).
  • src/executor/redirection.rs — pipeline_stage_output_redirect_fails self-brackets (covers both the driver gate and the builtin stage executor's own preflight), and route_pipeline_stage_streams wraps its walk the same way (body moved to route_pipeline_stage_streams_inner). New helper command_has_dynamic_fd_var_redirects.
  • src/executor/pipeline_stages.rs — the function stage clone runs the applier for the duration of the execute_function call: the plain invocation reaches execute_function with the funnel's bindings standing in the executing executor's table, and the stage clone must present the same space or the call's redirect walk leaves fd 1 on the pipe.

Pins — UNVERIFIED-BY-CAPTAIN

New target tests/issue2613_stage_varfd_delivery_regressions.rs, 9 pins, all asserting GNU bash 5.3 behavior:

  • issue2613_s02_pipe_left_var_dup_then_numbered_rebind_and_reference
  • issue2613_s02_pipe_right_var_dup_then_numbered_rebind_and_reference
  • issue2613_s04_pipe_left_var_dup_then_close_source_and_reference
  • issue2613_s04_pipe_right_var_dup_then_close_source_and_reference
  • issue2613_s12_pipe_left_move_form_reference_parent_keeps_source
  • issue2613_s12_pipe_right_move_form_reference_parent_keeps_source
  • issue2613_headline_c1_ebadf_stage_varfd_delivery
  • issue2613_builtin_stage_var_dup_reference
  • issue2613_function_stage_var_dup_reference

Guard stems that must stay green (captain's central verification): issue2255_pipeline_stage_redirects, issue2272_fdvar_spawn_delivery_regressions, issue2281_pending_close_positions, issue2322_close_position_regressions, issue2483_stream_drain, issue2612_varfd_applier_walk_regressions.

Cluster-local evidence (not a substitute for central verification): the audit shape battery (C1 both pipe positions, S02/S04/S12, builtin/function/compound/subshell stage faces, plus five failing-list controls incl. 2>err.txt diagnostic routing) matches GNU bash 5.3.20 byte-for-byte after the fix, and the 9 pins pass locally.

…nd fd space (rubash#2613)

GNU runs ONE do_redirections walk per pipeline element
(execute_cmd.c:2620 execute_pipeline -> redir.c:237
do_redirections) and the {var} entries allocate their descriptor
inside that same walk (redir.c:1470 redir_varassign), so a later
entry may reference the allocated descriptor by number
({v}>&9 1>&10).

The stage output preflight and the post-execution routing walk
replayed the list in a seeded descriptor space that never contains
the applier's allocation: the stage executors undo the dynamic-fd
applier before routing runs, and the preflight ran before it. A
numbered reference to the allocated slot resolved as a bad fd —
the preflight killed live stages and the routing walk dropped the
stage's payload and forced its status to 1.

Bracket both walks with the SAME applier the plain-command funnel
runs ahead of its own faces (apply -> judge -> undo), gated on the
list carrying a {var} entry; lists without one take the exact
previous path. The plain faces need no bracket: their walks run
while the funnel's applier bindings still stand.
…ier (rubash#2613)

reject_invalid_redirects validates a stage list's dup sources
against the shell's fd table plus its own overlay of the list's
numbered entries. A {var} entry's allocated descriptor is in
neither space, so a stage list referencing an earlier {var}
allocation by number ({v}>&9 9>o1.txt 1>&10, fd-mechanism audit
shape S02) was rejected with '10: Bad file descriptor' and the
command never ran — while the identical plain command passed,
because the plain funnel runs apply_dynamic_fd_var_redirects
ahead of the same gate (redir.c:237 do_redirections binds the
redir.c:1470 redir_varassign allocation in the element's one
walk; there is no stage dialect).

Apply the applier around the stage gate and undo it on every
exit; an applier failure verdict kills the stage with the
captured pipe bytes final, mirroring both the preflight arms
below and the plain funnel's return on the same verdict. Only
the gate sits inside the bracket — the word-expansion faces
below still expand $v against the outer binding, as GNU
expands a command's words before its do_redirections run.
…2613)

A function stage executes on a command-substitution clone of the
parent executor. The plain invocation reaches execute_function
with the funnel's applier bindings standing in the executing
executor's fd table, and the call's redirect walk resolves a
numbered reference to a {var} allocation against them; the stage
clone never ran the applier, so the allocated slot was missing
from its space and '{v}>&9 1>&10' on a function element silently
left fd 1 bound to the pipe — the payload went down the pipe
instead of to the dup target (redir.c:237 do_redirections;
execute_cmd.c:2620 execute_pipeline: the element's list is
applied in the element's own fork).

Run the same applier on the clone for the duration of the
execute_function call and undo it afterwards, gated on the call
carrying a {var} entry.
…sh#2613)

Nine pins in a new target,
tests/issue2613_stage_varfd_delivery_regressions.rs:

- S02 x {pipe-left, pipe-right}: {v}>&9 9>o1.txt 1>&10 — var dup,
  numbered rebind of the source, reference by number.
- S04 x {pipe-left, pipe-right}: {v}>&9 9>&- 1>&10 — plus the
  parent shell's fd 9 must survive (the close is the element's).
- S12 x {pipe-left, pipe-right}: {v}>&9- 1>&10 move form — plus
  parent fd 9 survival.
- The fd-mechanism audit's C1 reproducer verbatim (the EBADF
  headline case).
- The builtin-stage and function-stage faces of the same class.

All assert GNU bash 5.3 behavior (payload in seed.txt, PIPESTATUS
0, no Bad file descriptor diagnostic). Pins are
UNVERIFIED-BY-CAPTAIN.
@caomengxuan666
caomengxuan666 force-pushed the fix/2613-stage-varfd-delivery branch from ce7f7fb to 5059a24 Compare November 2, 2026 23:58
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 23:58
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长验收(2026-11-03):

@caomengxuan666
caomengxuan666 merged commit da3fe1d into master Nov 3, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2613-stage-varfd-delivery branch November 3, 2026 09:08
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