Skip to content

WIP: fix/2356 respawn state channel — oversized state rides a temp file, not env - #2366

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2356-respawn-state-file
Oct 31, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2356-respawn-state-file

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2356

Root cause

Realized children (background &, coproc, process substitution) receive shell state as execve arguments/environment. A single env string is capped at MAX_ARG_STRLEN (131,072 B) and the block at ARG_MAX. For the niubash#362 shape (6,000 functions), the stamped __RUBASH_FORK_FN_SPANS entry alone is 205,593 bytes → execve = -1 E2BIG (strace-verified on master), hello never printed; the pin issue_niubash_362::case_1_background_command_with_large_function_table is deterministically red.

GNU has no cap because it never serializes: background realization is fork (execute_cmd.c:664 async → execute_in_subshell, body execute_cmd.c:1573; make_child, jobs.c) — the child inherits memory directly.

Fix (mechanism, all four respawn sites)

  • New length-prefixed state-entry codec in src/invocation.rs (__RUBASH_STATE_FILE pointer key), sibling to the existing __RUBASH_CMD_FILE transport that already carries oversized -c source.
  • Executor::spill_respawn_env_entries: entries over 8 KiB individually, or enough largest entries to bring the inline block under 1 MiB in aggregate, move into a temp file; only the pointer crosses in env. Planted pointers are dropped (only the freshly minted one rides). Temp-write failure degrades to the previous all-inline behaviour.
  • Child side (Executor::new_inner): pointer consumed and stripped, file read + deleted, entries spliced into the variable table with process-env mirroring before any downstream consumer.
  • Applied at: background spawn (compound_exec), coproc respawn (compound_exec), procsub consumer (external_setup), procsub producer (trap_exec). Spawn-error paths remove the temp file.

No key is special-cased and no buffer grew: any state payload of any size now crosses. Foreign/external children keep GNU's own env protocol (and its physical limits) by design — they are not realized shell children.

Verification (details in the cluster REPORT.md)

  • cargo check + cargo check --tests: rc 0.
  • Direct probe (6000 functions + echo hello & wait): master → E2BIG, hello missing, 11.2 s CPU; this branch → hello + large-fn-status:0, empty stderr, rc 0, 13.1 s CPU — byte-equal to the GNU oracle output.
  • Pin target run on this box: E2BIG is gone (stderr empty); the run currently times out at the harness's 60 s wall limit only because the shared box is oversubscribed (load ~15, ~13 s CPU of pre-existing function-definition cost is paid by master and branch alike). Collector: please re-run the pin + the issue2292/issue2308 neighbor targets scrubbed in a quiet window.
  • New unit tests: codec round-trip incl. a 300 KB value, all-or-nothing decode; spill policy (small stays inline, 200 KB entry rides the file, aggregate spill bounded, planted pointer dropped).

Do not merge from this description alone — the pin re-run under the scrubbed-fd protocol is the gate.

caomengxuan666 and others added 2 commits October 31, 2026 14:25
…Refs #2356)

A realized child (background &, coproc, process substitution) received
the parent's shell state as execve arguments/environment. A single env
string is capped at MAX_ARG_STRLEN (128 KiB) and the whole block at
ARG_MAX, so a large function table wedged the spawn: the stamped
__RUBASH_FORK_FN_SPANS entry alone was 205,593 bytes for 6,000
functions and execve failed E2BIG (the niubash#362 shape, pin
issue_niubash_362::case_1 deterministically red on master).

GNU has no such cap because it never serializes: a background command
is realized by fork (execute_cmd.c:664 async -> execute_in_subshell,
body at execute_cmd.c:1573; make_child, jobs.c), so the child simply
inherits the parent's memory.

Mechanism fix, applied at all four respawn sites (background, coproc,
both process-substitution directions): env entries whose individual
size or aggregate size would threaten execve now spill to a sibling
temp file (the existing __RUBASH_CMD_FILE transport pattern); only a
small __RUBASH_STATE_FILE pointer crosses in the environment. The
child splices the entries back into its variable table during
Executor::new, deletes the file, and strips the pointer. Any state
payload of any size now crosses; no per-key special cases and no
bigger buffers. Temp-write failure degrades to the previous inline
behaviour instead of failing the spawn.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 31, 2026 16:52
@caomengxuan666
caomengxuan666 merged commit 376e041 into master Oct 31, 2026
@caomengxuan666
caomengxuan666 deleted the fix/2356-respawn-state-file branch November 2, 2026 01:09
SpookyWaste pushed a commit to SpookyWaste/rubash that referenced this pull request Nov 12, 2026
unixwin#2423 adjudication outcome: the E2BIG defect is real at niubash's
rubash pin 76f611d (reproduced exactly: line 6001 "Argument list too
long", background output lost) but was already fixed on master by
unixwin#2356 (0a0666e, PR unixwin#2366) — every fork-shaped respawn builder runs
its composed environment through spill_respawn_env_entries, which
moves oversized entries to the __RUBASH_STATE_FILE channel by size
class, never by key. The pin predates that commit (not an ancestor);
niu picks the fix up with the 1.5.0 pin bump.

Master verified clean at 6000 function definitions across the seed
shape plus background external/function, subshell, comsub, procsub,
pipelines and coproc. The companion wait-status observation is
refuted: bare `wait` returns 0 in GNU as well (builtins/wait.def
no-arg path via jobs.c:2796 wait_for_background_pids; oracle
5.3.20-verified), and the failed launch itself reports status 1 with
$! unset — GNU's fork-failure face, not a phantom success.

These pins extend the existing issue_niubash_362 case_1 coverage
(tests/regression.rs) to the crossing region (4800) and the sibling
fork shapes (background function, procsub, coproc). Test-only; no
product code changes.

Refs unixwin#2423
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