Skip to content

WIP: fix pending {fd} close list position on in-process and script faces (#2322) - #2367

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2322-close-position
Oct 31, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2322-close-position

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2322 — do not merge/close from this PR; the parent session adjudicates.

GNU contract

One ordered walk over the live descriptor table (redir.c:246 do_redirections). A {var}<&- close removes its descriptor at its position (redir.c:1219-1248), so a dup after a same-list close of its source fails with redirection error: cannot duplicate fd; a {var} dup assigns its variable only after the dup succeeds (redir.c:1010/1113), so a rejected command leaves the failing variable unset while earlier binds persist. Verified against the GNU bash 5.3.20 oracle binary with script-file probes.

Divergences fixed

  • D2, in-process faces (builtin/function): the {var} applier serviced a doomed dup against the raw fd table (pending closes were only recorded for the gate) and bound the variable before the gate rejected the command — w=[10] where GNU leaves it unset. Dup arms now consult a list_closed overlay maintained by apply_dynamic_fd_var_redirects as it walks (pending + fixed closes poison, binds revive); immediate-close call sites pass an empty set.
  • D1a, same-exe script face: numbered outputs bound in a separate later pass, after the [bug] heredoc 正文的 eof6 预检三形状误报:有效 $' 正文被吞、两类诊断走错家族(#1184 f6 残面) #2310 segmented {var} walk, so a {v}>&- close of a list-bound output never reached the child (child saw the ambient descriptor on the dup and the "closed" output still open). Outputs now bind per segment beside the inputs (open_compound_output_redirects_in); perform_script_fd_var_close consults the output saves; the own-binding predicate is direction-agnostic (one table entry per descriptor — an input bind governs a later output-spelled dup and vice versa, restoring issue2310_output_dup_resolves_own_bind).

Verification (local, fd-scrubbed runner; binary artifact timestamp-verified in-view)

  • Probe corpus (11 script-file probes, GNU vs fixed binary, normalized): byte-identical.
  • New pins tests/issue2322_close_position_regressions.rs: 13/13 pass.
  • Neighbors: issue2310 8/8, issue2314 12/12, issue2281 6/6, issue2308 8/8.
  • Extras: function-face and builtin-output-face rejections byte-identical to GNU; the e8 earlier-bind shape matches modulo GNU's undo-save allocation number (GNU a=11 vs rubash a=10 — state identical, accepted).

Known residual (NOT fixed here, characterized in the cluster REPORT.md)

Foreign-spawn face only: with a {var} dup AND a pending close of its source in the same list, the stage spawner re-opens the dup's target file O_TRUNC immediately after wait4, destroying bytes the foreign child wrote through the dup fd (strace-verified; call goes through std::fs::OpenOptions::open in the spawner post-wait finish path; exact call site not isolated — no gdb/lldb on the VM). Shell children are unaffected. Recommended as its own follow-up issue/cluster rather than a tail-end patch to the shared spawner finish path.

…script faces

GNU applies a command's redirect list as one ordered walk over the
live descriptor table (redir.c:246 do_redirections): a {var}<&-
close removes its descriptor at its position (redir.c:1219-1248),
so a dup positioned after a same-list close of its source fails,
and a {var} dup assigns its variable only after the dup succeeds
(redir.c:1010/1113 redir_varassign) — a rejected command leaves
the failing entry's variable unset while earlier binds persist.

In-process faces (builtin/function): the {var} applier serviced a
doomed dup against the raw fd table — pending closes were only
recorded for the gate — and bound the variable before the gate
rejected the command (GNU: unset). The dup arms now consult a
list_closed overlay of descriptors closed by earlier entries of
the same list, maintained by apply_dynamic_fd_var_redirects as it
walks (recorded pending closes and fixed closes poison, binds
revive); call sites whose closes are performed immediately pass
an empty set.

Same-exe script face: numbered outputs bound in a separate later
pass, after the #2310 segmented {var} walk, so a {v}>&- close of
a list-bound output never reached the child. Outputs now bind per
segment beside the inputs (open_compound_output_redirects_in in
command_input_scope.rs; execute_direct_shell_script consumes the
segment saves), perform_script_fd_var_close consults the output
saves, and the own-binding predicate is direction-agnostic — one
table entry per descriptor, so an input bind governs a later
output-spelled dup and vice versa (restores
issue2310_output_dup_resolves_own_bind).

Pins: tests/issue2322_close_position_regressions.rs, 13 tests,
expectations byte-verified against GNU bash 5.3.20 script-file
probes under a scrubbed fd layout (only 0/1/2 open at launch).

Refs #2322
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