Skip to content

fix(executor): compound scope undo covers {var} closes (#2614) - #2617

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2614-scope-restore-undo
Nov 2, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2614-scope-restore-undo

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2614

Code-only cluster delivery (captain verifies/merges centrally). Full write-up: the cluster REPORT (restore-set audit, approach, unrestorable-by-design items).

What

The compound scope in command_input_scope.rs snapshot/restores per kind, and its restore set omitted the closes its {var} pass performs immediately: a move-form {v}>&9- (or pure {v}>&-) inside { ...; } closed the parent's descriptor for good (D1: POST dies 9: Bad file descriptor). GNU registers every descriptor mutation in the command's undo chain (redir.c do_redirections / undo_redirections; the dup/move case saves the source via add_undo_redirect (redir_fd, r_close_this, -1)).

  • SavedDynamicClose: every {var} entry's close targets are snapshotted before the entry applies and replayed on EVERY exit (body end, dup-preflight abandonment, mid-list apply/open failure — those exits previously unwound nothing).
  • Scope-end cleanup now closes recorded (variable, fd) bound pairs, only while the variable still names the bound descriptor — replacing the by-name tail close that re-closed close-form variables' restored targets and chased body-reassigned variables.
  • Numbered arms aligned to GNU's activity gate (rubash#1235): the input opener now snapshots move sources (it never did), and both openers gate the source snapshot on the destination being open at the walk position (the output opener saved unconditionally and over-restored).

One file changed: src/executor/command_input_scope.rs. No trap_exec.rs / pipeline / lexer changes.

Pins

tests/issue2614_scope_restore_undo_regressions.rs — stem issue2614_scope_restore_undo_regressions, 12 tests, expectations byte-verified against GNU bash 5.3.20 script-file probes. UNVERIFIED-BY-CAPTAIN (local cargo check --tests clean only).

Guards: issue2322_close_position_regressions, issue2281_pending_close_positions, issue2562_numbered_dup_same_list, issue2503_born_closed_stdin_regressions.

GNU registers every descriptor mutation of a command's redirections
as a reversible undo record (redir.c do_redirections:
add_undo_redirect / add_undo_close_redirect; the dup/move case also
saves the source via add_undo_redirect (redir_fd, r_close_this, -1),
gated on the destination being active) and undo_redirections
replays the whole chain. The compound scope's snapshot/restore sets
only covered what the numbered openers overwrote, so the {var}
pass's immediate closes - a move source ({v}>&9-) or a pure close
({v}>&-) - survived the group and killed the parent's descriptor;
its scope-end cleanup also closed BY VARIABLE NAME, re-closing a
close-form variable's just-restored target. The numbered input
opener never saved a move source at all, and the output opener
saved one unconditionally where GNU's save is activity-gated
(rubash#1235: a move onto a closed target closes the source for
good).

- snapshot every {var} close target before the entry applies and
  replay the snapshots on every exit (body end, dup-preflight
  abandonment, mid-list apply/open failure)
- close scope-bound descriptors by recorded (variable, fd) pairs,
  only while the variable still names the bound descriptor
- gate both numbered openers' move-source snapshots on the
  destination being open at the walk position

Pins: issue2614_scope_restore_undo_regressions (UNVERIFIED-BY-CAPTAIN)
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 12:35
@caomengxuan666
caomengxuan666 merged commit 3dfacb3 into master Nov 2, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2614-scope-restore-undo 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