Skip to content

WIP: fix #2552 #2562 fd routing - #2566

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/2552-fd-routing
Nov 2, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/2552-fd-routing

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

W13 fd-routing cluster — CODE-ONLY delivery (captain directive 2026-11-02 17:03). Refs #2552, Refs #2562 (no closing keywords — captain merges).

All tallies below are cluster-reported and UNVERIFIED-BY-CAPTAIN. The captain verifies centrally. Stems to run through the slot (scrubbed_cargo.py cargo test --test ): issue2552_varfd_close_drain · issue2562_numbered_dup_same_list · issue2322_close_position_regressions · issue2368_foreign_spawn_post_wait_trunc_regressions · issue2483_stream_drain · issue2310_scriptface_fdvar_regressions · issue2261_child_script_readwrite_fd0_regressions. Note: the last two guards are environment-nondeterministic in the cluster VM (flip with build layout/slot state, incl. on pure master — comment-only control in the workdir REPORT.md); judge them by the central queue draw.

Four commits on base 2ce27e6 (rebase at harvest):

Cluster tallies (UNVERIFIED-BY-CAPTAIN): issue2552 5/5, issue2562 9/9, issue2322 13/13, issue2368 6/6, issue2483 6/6; issue2310/issue2261 see nondeterminism note. Full root-cause + GNU file:line ledger in the workdir REPORT.md.

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 09:06
#2552)

A redirect list that genuinely closes fd 1 via {v}>&- (v=1) lost
the foreign child's drained fd-2 output entirely; the mirror
({v}>&- with v=2) lost the drained fd-1 output the same way.

Root cause: the spawn takes the fd-copy capture route for the
closed-stdio shape, but the applier only RECORDS a {var} close
(fd_var_pending_closes) for the spawn delivery instead of closing
the descriptor in the shell's own table, and the delivery consumes
the records before the post-wait routing walk runs. The walk
skipped fd_var entries wholesale, so its seeded state never showed
the close: it reported "no output redirect" and the caller's
closed-fds branch discarded the whole drained pair.

GNU applies the list as one ordered walk in the child
(redir.c:237 do_redirections); {v}>&- is r_close_this under
REDIR_VARASSIGN (redir.c:1219-1248, redirector resolved from the
variable's value), so the close is a positional fact of the list.

Fix: in apply_ordered_output_redirects, fold a {var} close into
the walk state at its list position, resolving the variable
exactly as the applier did (dynamic_fd_variable_value — the same
resolution varfd_close_hits_stdio falls back to for the routing
caller). The {var} dup move form ({v}>&N-), the other recorded
output close, folds the same way. Binds and plain dups stay
skipped: the applier installed those in the fd table, so the
seeded state already carries them.

Regression: tests/issue2552_varfd_close_drain.rs — headline
{v}>&- (v=1), v=2 mirror, static 1>&- control, close+2>file
routing control, builtin {var}-close control; expectations
byte-verified against GNU bash 5.3.20 script-file probes.
Tally: 5 passed / 0 failed via scrubbed cargo test.
…position (Refs #2562)

A numbered output dup whose source an earlier entry of the SAME
redirect list bound input-side was rejected with
`9: Bad file descriptor`:

    echo hi 9<f9.txt 10>&9        (GNU: passes)
    echo hi 9<>f.txt 10>&9        (GNU: passes)
    cat 9<f9.txt 10>&9 <&10       (GNU: passes, prints the file)

GNU applies a command's redirections as ONE ordered walk in the
child (redir.c:237 do_redirections); the dup arms of
do_redirection_internal (redir.c:1139-1275) require only an open
descriptor at the dup's position — direction is irrelevant to
dup2 (redir.c:1193), and `<>` binds O_RDWR|O_CREAT
(make_cmd.c:682).

Root cause: the ordered walk's state is seeded from the AMBIENT
fd table only, and the walk skipped Input / ReadWrite /
DuplicateInput / CloseInput / HereDoc entries entirely, so a
same-list input binding had no model in the output-side target
graph and fd_target returned None. The builtin-side validator
(validate_builtin_dup_source) consulted only the ambient table
too, which is why `: 9<f 10>&9` diagnosed twice.

Fix, by mechanism:
- The walk now records each input-side binding at its list
  position (OutputFdState.list_binds: ReadOnly / Target /
  Closed). A `<>` plain file is opened O_RDWR exactly as the
  reject gate probes it, so a dup clones the shared description
  (write arm and offset included); a dup of a read-only
  description clones the write-less model and the target fd is
  itself recorded ReadOnly, so dup chains (`11>&10` after
  `10>&9`) resolve positionally. Output-binding arms clear the
  record (the output graph owns the newest model); close arms —
  including the rubash#2552 {var} fold — record Closed, so a
  list close dominates a still-open ambient entry. Both dup
  resolution sites in the walk share walk_dup_source_target.
- validate_builtin_dup_source now also accepts a source bound
  earlier in the command's own list (in_list_binding, resolved
  positionally; the mirror-field call sites pass the projected
  entry's list position).
- deliver_numbered_dup rung (1) stamps the WHOLE description
  (both channels off one dup, via stamp_child_fd_description —
  the same helper W7 added for deliver_fd_var_dup in the
  rubash#2531 fix) instead of a single channel, so a child that
  reads AND writes through a dup of a same-list binding gets the
  one cloned description.

Positionality is preserved both ways: `echo hi 10>&9 9<f9.txt`
still fails with exactly one diagnostic, as GNU's does.

Regression: tests/issue2562_numbered_dup_same_list.rs — the
three issue shapes, the readwrite write-through (file bytes
`data\n\nline2\n`, byte-verified against GNU bash 5.3.20), a dup
chain, the dup-before-binding control, the mirror control, the
`:` face, and an external face whose child reads through the
dup. Tallies (scrubbed cargo test, build slot): issue2562 9/9,
issue2552 5/5; guards issue2322 13/13, issue2368 6/6,
issue2483 6/6, issue2310 8/8, issue2261 9/9.
#2552, #2562)

A late full-output delivery of the first issue2552 run exposed a
fragility in two of this branch's own pins (the branch's other
tallies stand): both asserted the parent shell's diagnostic with
a bare `case.sh:` prefix, but the prefix is the script name AS
INVOKED — the harness passes an absolute path, so the line is
`/tmp/.../case.sh: line 2: ...` there and `case.sh: line 2: ...`
under a relative invocation. GNU prints the name it was given in
both forms; the product behavior is correct and identical, only
the pins were over-specified (verified byte-for-byte against the
local binary under both invocation forms). Both assertions now
pin the invariant substance — exactly one diagnostic line with
the expected tail (chkwrite verdict for #2552's builtin close;
the single EBADF for #2562's dup-before-binding control).
…(Refs #2562)

`cat 9<f9.txt 10>&9 <&10` — the third shape in the issue — still
failed `10: Bad file descriptor` at spawn setup on the final
tree (a verification run in this environment caught it after an
earlier run had reported the target green): the stdin face's
positional resolver, own_input_binding_before, scanned the list
for a binding of fd 10 but matched only CloseInput /
DuplicateInput / Input / ReadWrite entries. The winning entry
`10>&9` is a DuplicateOutput, so the scan skipped it and the
source read as unbound.

GNU's walk binds a descriptor by position regardless of which
dup spelling wrote it (redir.c:237 do_redirections), and the
codebase's own in_list_binding_depth already treats the two dup
kinds symmetrically. Mirror that here: the DuplicateInput arm
now also matches DuplicateInput's output twin, resolving the
source through the fd table's read endpoint or by recursion,
and the close arm also stops at CloseOutput (a `10>&-` binding
ends the scan, exactly as in_list_binding_depth's Closed
verdict). This also repairs the mixed chain
`cat 9<f 10>&9 11<&10 <&11`, and `cat 9<f 10>&9 10>&- <&10`
still fails with GNU's exact diagnostic and status.

Verified against GNU bash 5.3.20 with script-file probes on a
locally built binary (clone shapes, mixed chain, close control,
mirror control all byte-identical).
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