Skip to content

perf(background): bare-external-command & bodies spawn directly - bgwait engine overhead (three CR rounds) - #834

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/809-bgwait-direct-spawn
Oct 18, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/809-bgwait-direct-spawn

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #809 (engine half; final 2.47x→? numbers come from the Android/Termux device re-test per the §7 checklist in the investigation report).

Narrow gate direct-spawn evolution across three review rounds: bare-external bodies exec directly (async stdio + external env + unix child_pre_exec_reset) instead of respawning a full wrapper; four equivalence faces closed over the rounds (restricted shell arm, parent-process expansion side-effects via raw scan fallback, fd≥3 forwarding, fatal parameter pre-scan) — each verified GNU/wrapper-equivalent by independent CR with reproduction reversals. $! becomes the real worker pid (GNU-aligned). 30→40-probe background battery, command_chaining 1517 passed, hot path sleep 0 & unaffected.

…rubash -c wrapper respawn (#809)

GNU runs an async SIMPLE command as fork+exec: execute_connection '&'
forks, the child's execute_simple_command ends in shell_execve for an
external, and no second shell process survives. Rubash modeled every
'&' body as a full 'rubash -c' respawn — one extra process spawn plus
a complete shell init per background job. That is the engine half of
the #809 bgwait_750 2.47x gap on Android (bionic fork tax grows with
RSS, #680): the tablet pays wrapper-spawn + wrapper-init + sleep-spawn
where GNU pays one fork+exec of sleep.

Add background_direct_external_command: a deliberately narrow gate that
resolves a background body to (program, argv) only when it is a bare
external — no compound carriers, no assignments, no redirects, no
process substitutions, no alias/function/builtin/jobspec resolution,
not a script needing the shell fallback, no xtrace, no capture pipe —
and spawn it directly with std::process::Command on the same
resolve_background_stdio async stdio (fd 0 -> /dev/null rules
unchanged), the same apply_external_environment, and the unix
child_pre_exec_reset pre_exec (#229/#737). Everything else keeps the
wrapper, unchanged.

$! now names the external's pid — GNU's value — so wait $!/kill $!
and jobs report the real worker instead of a wrapper; #737's
PDEATHSIG wrapper indirection simply does not arise for this shape.
Windows cell: CreateProcessW floor is ~2ms regardless of image, so
bgwait750 moves 1622 -> 1789ms (min-of-3 interleaved, ~10%: the
wrapper's second spawn was overlapped, the direct spawn is serial);
30-probe background-semantics battery byte-identical; executor_tests
command_chaining 1517 passed / 9 pre-existing Windows failures, both
before and after; regression+traps+background+cli sweep failure sets
identical (23 pre-existing Windows failures each way).
…and fd>=3 gates for the #809 direct-spawn fast path

CR findings F1/F2 (must-fix) plus N1/N2/N3 on 28744c8:

F1 restricted shell: the gate mirrored execute_prepared_command's
builtin/alias/function arms but not restricted_command_error, so
'set -r; /usr/bin/touch /tmp/x & wait' executed under the fast path
where base/GNU refuse. Gate returns None whenever the restricted option
is set; the wrapper keeps today's rejection.

F2 parent-side expansion: the gate expanded words in the parent before
resolving to a disk command. Three regressions — 'echo $(echo tick >>
log) &' double-fired the substitution when the shape fell back to the
wrapper (whose child re-expands the source), ${FOO:=0.05} leaked the
assignment into the parent shell, and 'sleep $(sleep 1; echo 0.1) &'
blocked the parent for the substitution's runtime. The gate now refuses
any word whose raw text carries $( / backtick / := or an assignment-
typed braced expansion (parameter_expansion_assigns scan — the
structured parameter_expansion nodes are not populated by this parser);
such shapes keep the wrapper, whose child expands them exactly as
today. ${x:-v}/${x-v} reads stay on the fast path.

N1: the gate now runs before the wrapper-only exe resolution and env
block build, so direct spawns stop paying the 0.17-0.44ms env_map
prep.

N2: any live fd>=3 entry (persistent 'exec 3<file', #180) keeps the
wrapper, which forwards them via __RUBASH_FD_*; a native direct exec
cannot.

N3: battery extended with the F1/F2 shapes and a $! probe asserting
the background pid's image: base reports the wrapper exe, fixed reports
sleep.exe — the intended GNU-parity change; all other 36 probes remain
byte-identical. F2 probes on fixed: tick log 1 line, FOO=[UNSET] for
:= and = shapes, parent spawn latency ~45ms (was ~1030ms blocked).
command_chaining 1517 passed / 9 pre-existing failures unchanged;
background/trap suites green; bgwait750 min-of-4 base 1664 vs fixed
1658 (parity, load-controlled window).
…awn gate (CR F2-R2)

The gate's expand_command_words does not own the fatal parameter
verdicts: the set -u unbound-variable scan and the ${x:?msg} /
${x?msg} fatal checks live on the dispatch path (command_execute.rs ->
validate_command_parameter_expansions -> parameter_expansion_error,
parameter_errors.rs:737). Expanding in the gate therefore swallowed
those verdicts and handed degraded words to argv — 'set -u; sleep
$NOVAR &' executed sleep with an empty argument and 'sleep ${X:?boom}
&' executed sleep with the message text, where base/GNU refuse.

The gate now runs parameter_expansion_error(command) before expanding;
any fatal shape falls back to the wrapper, whose child runs the same
scan on its own dispatch and reports byte-identically. Safe against the
scan's prefix-comsub side-effect replay: the F2 arm above already
routed every command-substitution-bearing word to the wrapper, so the
replay can never fire in the parent.

Battery +3 error-shape probes (set -u unbound / ${x:?msg} /
${x?msg}): 40 probes, byte-identical to base except the intended $!
image-name change. command_chaining 1517 passed / 9 pre-existing
Windows failures unchanged; background/trap suites green.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant