Skip to content

fix(executor): confine fork/respawn state transport to the exec boundary (Refs #3049) - #3736

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3049-child-state-transport-boundary
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3049-child-state-transport-boundary

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

  • Root cause: rubash emulates GNU's fork by respawning rubash -c, serializing the whole variable table into __RUBASH_* env keys. A respawned process's environ then permanently carried its birth transport, and compose_child_environment's process-env mirror re-projected it into real exec'd children — VarTable::from_values applied the parent's attribute lists, so BATS_TEST_TAGS was born declare -ar inside bats-exec-test and bats-core test Missing external command resolution costs ~60-85ms; consider caching negative lookups #71 deadlocked on its readonly registration assignment.
  • Emission fix: compose_child_environment now projects __RUBASH_* names only when this session journaled a write for them (the G-03 mirrored-assignment contract); ambient frame stamps __RUBASH_CURRENT_LINE/__RUBASH_CURRENT_FILE never project (GNU's line_number/BASH_SOURCE are C globals, never environ data).
  • Ingest fix: Executor::new_inner decides respawn provenance on the raw env before VarTable::from_values imports attribute strings; non-respawn births strip the whole transport family (new fork_respawn_transport_key predicate) from both the import map and process env. The __RUBASH_STATE_FILE splice is gated on validated respawn and restores entries unjournaled.
  • Export-set fix: keep_shlvl (validated respawn) skips mark_initial_exported_vars — the transported __RUBASH_EXPORTED_VARS list already carries the parent's exported set; the recompute promoted the parent's unexported variables riding the fork channel.
  • Fork-shaped respawns untouched: env_map bases still select via rubash_spawn_inherited_state.

GNU ground: execute_cmd.c execute_in_subshell (fork shares the table in-process) vs variables.c maybe_make_export_env/make_env_array_from_var_list (exec hands over only the export environment).

Test plan

  • tests/issue3049_bats71_ctrl_c_hang.rs: 2/2 — real bats Missing external command resolution costs ~60-85ms; consider caching negative lookups #71 chain completes in ~10s (was hung past 120s cap)
  • issue2259_interactive_marker_leak_regressions: 6/6 (was 5/6 on base — ambient_frame_stamps_do_not_reach_script_children fixed)
  • 19 adjacent env/spawn-boundary suites green (2058/2423/2723/2586/1074/1083/1274/1276/2565/2894/1461/1497/1766/1806/s6/1654/70/2064/2884)
  • issue2136_respawn_provenance: 9/10 — orphan arm is environment-bound flaky under WSL subreaper reparenting (verified identical on base)

Generated with Devin

…ary (Refs #3049)

GNU's fork and exec boundaries differ exactly where rubash conflated
them: a forked subshell shares the parent's variable table in-process
(execute_cmd.c execute_in_subshell), while a newly exec'd shell
receives only the export environment (variables.c
maybe_make_export_env / make_env_array_from_var_list) — internal
state like readonly/array attributes is never environment data.

Rubash emulates the fork by respawning `rubash -c`, so the parent's
table is serialized into `__RUBASH_*` environment keys. Two leaks
followed:

1. A respawned process's environ permanently carries its birth
   transport, and compose_child_environment's process-env mirror arm
   re-projected every `__RUBASH_*` entry into REAL children. The
   attribute lists landed in the exec'd bats-exec-test, whose
   VarTable::from_values applied them: BATS_TEST_TAGS was born
   `declare -ar`, its registration assignment failed "readonly
   variable", and bats-core test #71 deadlocked.
2. VarTable::from_values imported the `__RUBASH_*_VARS` attribute
   strings before respawn provenance was validated, so even a
   non-respawn birth applied a foreign parent's attributes.

Fixes:

- executor/init.rs::new_inner: decide respawn provenance on the raw
  environment BEFORE from_values; for non-respawn births strip the
  whole transport family (new predicate
  compound_exec::fork_respawn_transport_key) from both the import map
  and the process env, so residue can neither be imported nor ride
  the child-env mirror onward.
- executor/init.rs: gate the __RUBASH_STATE_FILE splice on validated
  respawn provenance, and restore spliced entries to the process env
  WITHOUT journaling (execve-shape restoration, not a session write).
- executor/init.rs::initialize_fresh_shell_env_vars: for keep_shlvl
  (validated respawn) skip mark_initial_exported_vars entirely — the
  exported set is already imported from the transported
  __RUBASH_EXPORTED_VARS list, and the recompute promoted the
  parent's UNEXPORTED variables (which ride the respawn env as the
  fork channel) to att_exported, snowballing into exec'd children.
- executor/readonly_functions.rs::compose_child_environment: project
  `__RUBASH_*` mirror entries only when this session journaled a set
  for them (the G-03 mirrored-assignment contract). This also
  reproduces the rubash#2259 adjudication — a script's own
  entry-stamped __RUBASH_SCRIPT_NAME keeps projecting while ambient
  foreign copies stay blocked — and additionally blocks the ambient
  frame stamps __RUBASH_CURRENT_LINE/__RUBASH_CURRENT_FILE (GNU's
  line_number/BASH_SOURCE are C globals, never environ data).

Fork-shaped respawns are untouched: env_map bases still select via
rubash_spawn_inherited_state and mint their own stamps.

Evidence (WSL, GNU Bash 5.3.20 oracle):
- tests/issue3049_bats71_ctrl_c_hang.rs: 2/2 — the real bats-core #71
  chain (bats -> bats-exec-suite -> bats-exec-file -> bats-exec-test)
  completes in ~10s; previously hung past the 120s cap.
- issue2259_interactive_marker_leak_regressions: 6/6 (was 5/6 on
  base — ambient_frame_stamps_do_not_reach_script_children fixed).
- 19 adjacent env/spawn-boundary suites green (2058/2423/2723/2586/
  1074/1083/1274/1276/2565/2894/1461/1497/1766/1806/s6/1654/70/2064/
  2884). issue2136: 9/10, the orphan arm is environment-bound flaky
  under WSL subreaper reparenting (fails identically on base).

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 force-pushed the fix/3049-child-state-transport-boundary branch from 884e0aa to 64af72a Compare November 15, 2026 18:40
@caomengxuan666
caomengxuan666 merged commit 0a405d8 into master Nov 15, 2026
7 of 11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/3049-child-state-transport-boundary branch November 15, 2026 18:52
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
…awn strip (Refs #3856) (#3857)

0a405d8 (#3736) added __RUBASH_FD_ to fork_respawn_transport_key, so
Executor::new_inner deleted every fd-delivery stamp for any birth that
is not a validated internal respawn — before init.rs could collect them
into inherited_fd_handles. exec/external/comsub/pipeline children are
never respawns, so all fd>=3 delivery through the env channel died on
Windows (unix masks it via pin+adoption). The keys are per-spawn
delivery minted fresh for THIS child and consumed+stripped at entry —
not respawn residue — so they do not belong to the #3049 family.

Verified: tests/issue3311_exec_child_fd_delivery 4/4 red->green;
exec-own/exec-amb/plain-own fd>=3 writes reach children again.
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
#3560) (#3858)

* fix(executor): keep __RUBASH_FD_* delivery stamps out of the non-respawn strip (Refs #3856)

0a405d8 (#3736) added __RUBASH_FD_ to fork_respawn_transport_key, so
Executor::new_inner deleted every fd-delivery stamp for any birth that
is not a validated internal respawn — before init.rs could collect them
into inherited_fd_handles. exec/external/comsub/pipeline children are
never respawns, so all fd>=3 delivery through the env channel died on
Windows (unix masks it via pin+adoption). The keys are per-spawn
delivery minted fresh for THIS child and consumed+stripped at entry —
not respawn residue — so they do not belong to the #3049 family.

Verified: tests/issue3311_exec_child_fd_delivery 4/4 red->green;
exec-own/exec-amb/plain-own fd>=3 writes reach children again.

* fix(executor): revoke delivered fd N on command-local N>&- / N<&- (Refs #3560)

The external fd delivery stamped every ambient fd>=3 into the child's
__RUBASH_FD_* env keys (and unix pins) before the redirect walk, and
the walk's close arm only skipped the entry — so `cmd 3>&-` left the
ambient stamps in place and the child still saw fd 3. GNU services
the close with r_close_this inside the ordered do_redirections walk
(redir.c:237; close_buffered_fd at redir.c:1239) in the forked child.

Route the numbered close through apply_pending_close_at — the same
revocation a {var} pending close uses: env stamps removed, pins
dropped, carriers retired to the dead list, walk bindings cleared,
and the table step snapshots into fd_var_close_restore so
undo_child_fd_var_redirects restores the parent's binding at command
end (the close is child-scoped, never a parent exec N>&-).

Depends on the __RUBASH_FD_* delivery-strip fix (#3856): on master
the env channel is dead anyway, so this pin is only observable once
delivery works. Verified: pin 5/5 green here, 4/5 red without this
commit; GNU bash 5.3.20 script probes match byte-for-byte.
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