Skip to content

WIP: fix(fd): script face re-resolves fd_var dups against the command's own binds (#2310) - #2321

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/2310-scriptface-fdvar
Oct 31, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/2310-scriptface-fdvar

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2310 (1.5.0 milestone blocker). Draft for the collector's batch + composition.

Defect

The same-exe script face (execute_direct_shell_script) serviced a command's {var} redirects from the funnel's pre-dispatch pass only: dups were copied from the ambient fd table before the command's own binds existed, and {var} closes were only recorded, never performed for the child. Headline: rubash obs.sh {v}<&- 9<f9 {w}<&9 delivered the parent's ambient SEED at fd 10; GNU delivers the f9 content. The spawn faces already resolve at position (#2272/#2279); this face did not.

GNU ground (clean-room: behavior facts)

  • redir.c:237 do_redirections — one ordered walk over the list, each element applied to the live table in order.
  • redir.c:1144-1150 — REDIR_VARASSIGN dup allocates fcntl(redir_fd, F_DUPFD, SHELL_FD_BASE) against the table as bound so far.
  • redir.c:1193 — the dup copies the descriptor as bound at that walk position (dup2).
  • redir.c:1161-1166 — a varassign failure closes the just-duplicated fd.
  • redir.c:1239-1248 — r_close_this + REDIR_VARASSIGN closes the fd the variable holds at that position.

Fix (by mechanism, no per-shape guards)

  • execute_direct_shell_script now binds numbered inputs in segments split at the {var} entries and applies each entry's dup/close effect at its segment point, when the table holds exactly the binds GNU's walk would have produced by then.
  • open_compound_numbered_input_redirects is split into a whole-list wrapper + open_compound_numbered_input_redirects_in(range, &mut saved); the accumulator's first-snapshot-wins dedup makes segmented binding restore-identical for all existing callers.
  • Governed dups re-copy from the at-position table; when the ambient-only applier bound nothing (source opened only by the command's own list), the bind happens at position so the allocator sees the own binds (chain shape 9<f9 10<&9 {w}<&10 → fd 11, matching GNU).
  • Closes are performed in-table for the child with pre-command snapshots (from the numbered accumulator / heredoc saves when an earlier own-list bind already snapshotted the slot) pushed to fd_var_close_restore — the spawn prologue's bookkeeping — so the paired undo restores the parent. Side effect: the parent no longer loses its own fd 9 to the undo on this face (reproduced on master for every {v}<&- shape).

Files

  • src/executor/external_finish.rs — call site swap (bind block only).
  • src/executor/command_input_scope.rs — segmented range variant + SavedNumberedFd accessors.
  • src/executor/trap_exec.rs — the segmented binder + at-position fd_var applier (one new block, +280 lines).
  • tests/issue2310_scriptface_fdvar_regressions.rs — 8 pins, GNU expectations.

No overlap with PR #2300: this change touches neither src/executor/external_inner.rs nor redirection.rs.

Verification (local, scrubbed fd environment)

  • 13 script-file probes byte-equal against the GNU bash 5.3.20 oracle binary (headline, reversed order, shared-offset drain, dup-before-bind, output spelling, dup chain, close delivery, dup-then-close, parent restoration ×3).
  • Pins: issue2310 8/8. Controls: issue2272 24/24, issue2279 13/13, issue1313 2/2 — all green. (No issue2281 test target exists at master 8a3d6a9; grep-verified.)
  • Full regression + lib batches deliberately left to the collector.

Known adjacent gap (not fixed here, flagged in REPORT.md and the pin file header)

s4: 9<f9 {v}<&- {w}<&9 (v=9) — GNU rejects the command (the dup's source is closed at its walk position); both rubash faces currently run the child. It is a pending-close-position question in the shared applier records (positions are not recorded), separate from this issue's dup-resolution/close-delivery defects.

…n binds

Refs #2310

GNU applies a command's redirect list as ONE ordered walk over
the live descriptor table: do_redirections (redir.c:237) services
each element in order, a REDIR_VARASSIGN dup allocates with
fcntl(F_DUPFD) against the table as bound so far
(redir.c:1144-1150) and copies the descriptor as bound at that
walk position (redir.c:1193, dup2), a varassign failure closes
the just-duplicated descriptor (redir.c:1161-1166), and a
{var}<&- close closes the descriptor the variable holds at its
position (redir.c:1239-1248).

The same-exe script face (execute_direct_shell_script) never
revisited the funnel's pre-dispatch fd_var service: dups were
copied from the AMBIENT table before the command's own binds
existed, and closes were only recorded, never performed for the
child. `rubash obs.sh {v}<&- 9<f9 {w}<&9` therefore delivered
the parent's ambient seed at fd 10 where GNU delivers the f9
content, and `{v}<&-` left the child reading the parent's fd 9.

Fix by mechanism, mirroring the walk: the face now binds its
numbered inputs in segments split at the command's {var} entries
(open_compound_numbered_input_redirects_in carries the segment
range; the whole-list wrapper is unchanged for other callers)
and applies each entry's effect at its segment point, when the
table holds exactly the binds GNU's walk would have produced by
then. A dup whose source an earlier own-list entry binds is
re-copied from the at-position table (or bound outright when
the ambient-only applier bound nothing, the allocator then
seeing the command's own binds like F_DUPFD does); a close is
performed in the table with a pre-command snapshot — taken from
the numbered accumulator or the heredoc saves when an earlier
own-list bind already snapshotted the slot — pushed to
fd_var_close_restore, so the paired undo restores the parent
(the spawn delivery prologue's bookkeeping). This also repairs
the parent losing its own fd 9 to the undo on this face, which
reproduced on master for every {v}<&- shape.

Verification (oracle: GNU bash 5.3.20 binary, script-file
probes): 13 shapes byte-equal with GNU, including the headline,
the shared-offset dup, dup-before-bind, the output spelling,
a numbered-dup chain, close delivery, and parent restoration
after close and bind-then-close commands. Pins:
tests/issue2310_scriptface_fdvar_regressions.rs 8/8 under a
scrubbed fd environment; controls issue2272 24/24 and
issue2279 13/13 stay green (no issue2281 test target exists at
this base). Full regression + lib batches are the collector's.
caomengxuan666 and others added 2 commits October 31, 2026 11:01
… field (Refs #2310)

Master's #2281 made fd_var_pending_closes carry (position, fd) pairs;
this branch's apply_script_fd_var_entry still took &mut Vec<u32>,
which failed compilation on the composed tree (CI E0308). The callee
now takes &mut Vec<(usize, u32)> and matches records on the fd
component at its two consumption sites — the branch's own semantics
(consume-by-fd) unchanged.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 31, 2026 05:07
@caomengxuan666
caomengxuan666 merged commit eee3f6e into master Oct 31, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2310-scriptface-fdvar branch November 2, 2026 01:09
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