Skip to content

WIP: {fd} variable-form redirects — spawn delivery for all kinds - #2277

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/2272-fdvar-spawn-delivery
Oct 30, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/2272-fdvar-spawn-delivery

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: {fd} variable-form redirects — spawn delivery for all kinds (Refs #2272)

The dynamic-fd applier bound {var} descriptors into the shell's fd table, but apply_external_fd_delivery skipped fd_var entries outright, so external children never received the bindings (or received nothing where GNU delivers at the var's number). This PR adds the fd_var channel inside the shared delivery helper, per kind:

  • File kinds (<, >, >>, <>) already rode the ambient env+pin stamp once bound — verified against the GNU oracle, no change needed.
  • Heredoc / here-string bodies (Text endpoints, no OS descriptor) are materialized into carrier files and stamped/pinned at the var's number, the 外部命令的编号 fd heredoc body 未安装(子进程 fd 未开,EBADF) #2270 shape.
  • Dup forms resolve their source the way GNU's single ordered walk does (redir.c:237): the command's own earlier bindings first (walk-delivered descriptions, then the 列表级输入 procsub 3< <(...) 未物化(#2196 输入侧兄弟面) #2235 fd-0 replay), then the applier's ambient copy (File → ambient delivery; text snapshot → carrier), then a stdio source pinned child-locally at the var's number.
  • Close forms and move-form source closes are no longer performed in the parent's fd table by the applier: they are recorded, performed by the delivery helper just before the inheritance stamp (snapshots taken), and restored by the external face's undo — GNU scopes them to the child; the parent's bindings survive. This also removes the spurious "cannot duplicate fd" rejection the ordered redirect preflight produced for move forms.
  • Pipeline stage elements now run the dynamic-fd applier (scoped by the same undo) before the stage spawner, so stage {var} bindings exist for the shared helper to deliver; a failed {var} redirect fails the element with status 1, like a numbered one.

Regression pins: tests/issue2272_fdvar_spawn_delivery_regressions.rs (24 pins; expectations byte-verified against GNU bash 5.3.20 oracle probes — foreign-observer children only, no in-process script readers).

Compile rationing: local verification is cargo check + cargo check --tests only; all test execution is pending the collector batch / CI.

Refs #2272

…g in the parent table (Refs #2272)

Close-form targets ({var}<&- / {var}>&-) and move-form dup sources
were closed in the shell's own fd table by
execute_dynamic_fd_var_redirect. GNU scopes a command's closes to
the command (in the fork for external commands): an exec-bound
descriptor survives a command that closes it through a {var}
entry. Closing in the parent table destroyed the binding for
good, and the ordered redirect preflight — replaying the list
against the table — then rejected the very dup the applier had
just serviced ({v}<&3- failed "cannot duplicate fd").

On the simple-command funnel path (new defer_closes parameter;
the exec face and the compound scope path keep their persistent
closes) the applier now only records the descriptors in
fd_var_pending_closes. undo_child_fd_var_redirects restores the
snapshots apply_external_fd_delivery took when it performed the
closes for a spawn, and drops records no delivery consumed.
…2272)

The shared spawn delivery helper skipped every fd_var entry, so
the bindings the dynamic-fd applier installed in the fd table
reached external children only when they happened to be plain
File endpoints under the ambient stamp. Add the fd_var channel
inside the helper, reusing the existing env + pre_exec pin
machinery (no new protocol shapes):

- deferred {var} closes recorded by the applier are performed in
  the table (snapshotted for the undo) before the inheritance
  stamp, so the child does not inherit those slots;
- heredoc / here-string bodies (Text endpoints) are materialized
  into carrier files and stamped/pinned at the var's number,
  including the heredoc_redirects entries;
- dup forms resolve the source in GNU's walk order (redir.c:237):
  a binding this walk already delivered for the source, the #2235
  fd-0 replay for the command's own stdin binding, the applier's
  ambient copy (File rides the ambient stamp; a text snapshot
  rides a carrier), and finally a stdio source pinned
  child-locally at the var's number. When the applier could not
  bind (an own-list source), the delivery number is allocated
  with the applier's own lowest-free rule.

own_input_binding_before and dynamic_fd_variable_value widen to
pub(in crate::executor) for the replay/lookup.
)

GNU applies every pipeline element's redirections in the
element's fork (execute_cmd.c:2620 -> redir.c:237), {var}
entries included, but the sequential stage spawner bypasses the
simple-command funnel whose applier binds {var} entries into the
fd table — so a stage's {var} bindings never existed and the
shared delivery helper had nothing to deliver (every kind failed
EBADF in the stage's child). Run the applier in
execute_external_pipeline_stage, scoped by the same undo the
external face uses; a failed {var} redirect fails the element
with status 1 while the pipeline continues, like a numbered
redirect failure. Elements carrying fd_var redirects already
decline the concurrent stage path (is_list_only_redirect), so
the sequential spawner is the only stage face to serve.
24 pins covering every GNU-supported kind for external
commands: file in/out/append/read-write, heredoc, here-string,
two-var list-order allocation, ambient-slot collision, dup of an
ambient file fd / an ambient heredoc / the command's own list
bindings (input and output) / its own stdin redirect / ambient
stdin / stdout / stderr, close form (child closed, parent
binding and variable survive), unset-var close (ambiguous
redirect), move form (child copy + source closed + parent keeps
the source), missing-file abort, three pipeline-stage cells, a
numbered-fd control, and the in-process persistence guard.

Observers are real spawns only (a nested rubash binary, or dd
for the stdio faces) — never ./reader.sh scripts, which rubash
runs in-process and would pass vacuously. Expectations
byte-verified against GNU bash 5.3.20 oracle probes.
…emantics

Refs #2272

The pin expected the parent's post-command read of ambient fd 10 to
replay amb-line-1 after the child had already read that same
inherited descriptor to EOF. GNU bash 5.3.20 oracle probes (exact
shape / child-skips-ambient / plain-fork controls) show the forked
child's duplicate descriptor shares the parent's open file
description, offset included: the child's cat <&10 advances the
shared offset to EOF, so the parent's trailing read prints nothing.
The PR tree's output matches GNU byte-for-byte on all three probes;
the product behavior was already correct - only the pin's
expectation was wrong. The trailing read stays in the pin: it still
guards that the parent's fd 10 binding survives (a destroyed
binding would fail the redirect and the script's exit code).
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 30, 2026 20:47
@caomengxuan666
caomengxuan666 merged commit 0de0407 into master Oct 30, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2272-fdvar-spawn-delivery branch November 2, 2026 01:09
SpookyWaste pushed a commit to SpookyWaste/rubash that referenced this pull request Nov 12, 2026
Refs unixwin#2278

The numbered redirect walk in apply_external_fd_delivery skipped
dup spellings outright, so `cmd 3<&0 <seed` and `cmd <seed 3<&0`
both left the child's fd 3 unbound (EBADF). GNU services every
dup in its single ordered walk with a dup2 of the source as bound
at that walk position (redir.c:237 do_redirections; redir.c:1138
do_redirection_internal, dup2 at :1193).

Add deliver_numbered_dup, mirroring the unixwin#2277 {var} ladder by
mechanism: (1) a binding this walk already delivered for the
source, (2) the unixwin#2235 fd-0 replay for the command's own stdin
binding, (3) the ambient fd table's endpoint at the source
(a File endpoint stamps/pins; a text snapshot rides a carrier
through the generalized deliver_fd_var_text_binding), (4) a
stdio source — a child-local pin when no later entry rebinds the
source slot, or a parent-side capture of the ambient stdio
descriptor when one does (`3<&0 <seed` delivers the ambient
stdin, not the seed the later entry wires into the child's
fd 0). Bindings that are not FileFds are recorded per slot so a
later dup in the same list chains onto the same binding. Move
spellings (`4<&3-`) keep their existing fall-through.

Regression pins in tests/issue2278_numbered_stdio_dup_regressions.rs
(both dup orders, the output order pair, chains, controls),
expectations byte-verified against GNU bash 5.3.20 probe cells.
Pins are not run locally under the compile-rationing regime; the
collector batch-verifies them under a scrubbed fd environment.
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