Skip to content

followup-a: PATH bind flushes command hash (#1551) + ambient 2>&1 pipeline stderr (#1552) - #1572

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix5/slice-followup-a
Oct 25, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix5/slice-followup-a

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

followup-a slice: two fixes, one commit per issue, cherry-picked onto master a4c6c2ad with zero conflicts (diff stats identical to the cluster branch).

#1551 — PATH assignment flushes the command hash table (sv_path)

GNU variables.c:5919 sv_path() is exactly phash_flush(): any bind or unbind of PATH flushes the whole hashed-command table (same-value assignment and += included; pure attribute operations like export PATH / readonly PATH with no value do not). rubash kept two stores (the __RUBASH_HASH_TABLE env key and the process-global lookup cache's hashed mirror) and flushed neither, so stale entries kept being served after PATH=..., unset PATH, etc.

The fix hangs a name == "PATH" gate on every binding funnel (assignment funnel incl. temp-prefix bind and restore, declare/typeset/local operands, setattr with-value binds, unset, printf -v, arithmetic binds, read/for/select binds) via a new builtins::hash::flush_command_hash_table + path::flush_command_lookup_cache_hashed. Two same-family defects found by the probe matrix are fixed in the same commit:

  • results-copy resurrection: the hash table's only write path also copied entries into the fingerprint-keyed results memo, so a flushed entry could resurrect on a fingerprint coincidence; the setter now writes only the hashed mirror.
  • fork-boundary leakage: the process-global hashed mirror crossed in-process fork boundaries both ways (pipeline stages, command substitution — both the &self and &mut dispatch entries — ( ) subshells, input-form procsub). A new RAII hashed_cache_fork_guard (snapshot/restore, same discipline as the sigpipe guard) now covers all of them; the lastpipe final stage is deliberately unguarded (it runs in the current shell).

Regression: tests/issue1551_path_assignment_hash_flush_regressions.rs (14 tests), plus the #[ignore]d pin in the issue1545 file is activated (13/13 there).

Fixes #1551

#1552 — non-final pipeline stage diagnostics lost under an ambient 2>&1

Root cause differs from the issue's original hypothesis. The bare timed form does not reproduce; the actual trigger is an ambient (environmental) 2>&1 binding around the pipeline (a compound command / group / exec redirecting stderr onto the inherited stdout). It is not limited to dash words and not limited to timed pipelines: { nosuchA | nosuchB | nosuchC; } 2>&1 lost every non-final stage's diagnostic.

GNU (execute_cmd.c:2620) binds each stage's fd 2 to the inherited fd-1 object, never to the inter-stage pipe. rubash's sequential pipeline driver unconditionally folded each stage's captured stderr into next_input whenever fd 2 was Stdout, so a non-final stage's diagnostic became the next stage's unread stdin. The fix accumulates non-final stage stderr in stage order (ambient_stderr) and writes it into the pipeline output stream after the loop; the concurrent external-prefix path gets the same treatment. A stage's own 2>&1 / |& is unaffected (already folded into the pipe payload before the merge point).

Regression: tests/issue1552_ambient_stderr_pipeline_diagnostic_regressions.rs (8 tests).

Fixes #1552

Out of scope

Residual R3 — a pipeline last stage's own 2>file loses its command-not-found diagnostic entirely (neither on the stream nor in the file; pre-existing, unrelated mechanism) — is filed separately as #1571 and is not closed by this PR.

Verification on the integrated tree (gated build, purified environment) is reported in the slice report; local full verification is the merge gate per campaign policy.

… — variables.c sv_path -> phash_flush (#1551)

GNU registers an assignment function for PATH in the special_vars
table (variables.c:5819 { "PATH", sv_path }) whose whole body is
phash_flush() (variables.c:5919 -> hashcmd.c:57): any bind or unbind
of PATH empties the remembered command locations, even a same-value
bind, while attribute-only operations (export PATH, declare -p PATH)
never bind and keep the table. rubash never fired it: `hash -p`
entries survived PATH assignments and kept executing, `hash` showed
stale entries with bumped hit counts, and `unset PATH` left the table
populated.

- builtins::hash::flush_command_hash_table clears the state's
  __RUBASH_HASH_TABLE plus the lookup cache's hashed mirror
  (path::flush_command_lookup_cache_hashed); the fingerprint-keyed
  scan stores stay self-invalidating. The path.rs comment claiming a
  PATH change does not flush is corrected (that reading came from
  #1137's initialization scenario, not from assignment).
- The flush fires at every bind funnel, mirroring the #1531 sv_locale
  discipline: the executor assignment funnel (plain/+=/temp-prefix/
  read/for/select/getopts), declare/typeset/local operands (with and
  without a value), export/readonly value binds (has_assign-gated —
  their name-only forms materialize the current value for the mirror
  but do not bind in GNU), unset, the temp-assignment and
  function-local restore paths (a restore is a re-bind), printf -v,
  and the arithmetic bind points.
- set_command_lookup_cache now writes ONLY the hashed mirror: its
  copy into the fingerprint-keyed `results` memo was not scan
  evidence and resurrected flushed entries whenever the fingerprint
  later coincided.
- Fork brackets for the process-global mirror (RAII
  path::hashed_cache_fork_guard, same shape as the SIGPIPE guard):
  pipeline elements, command substitution (both the &self wrapper and
  the &mut typed dispatcher), ( ) subshells, and input process
  substitutions are GNU forks (execute_cmd.c:2620, subst.c:7143,
  execute_cmd.c:1576, subst.c:6362) — a child's PATH-bind flush or
  `hash -p` write must not reach the parent's table.

Regression pins: tests/issue1551_path_assignment_hash_flush_regressions.rs
(14), and the #1545 file's previously-ignored sv_path pin is activated
(its GNU expectations were already written).
…output stream, not the next stage's stdin (#1552)

Filed as: a timed pipeline whose first stage is a dash word loses
its `command not found` diagnostic (`time '-p' true | cat` under a
bad PATH reported only `cat:`). The bare form never reproduced —
the parsetime2 comparison harness wraps cases in `( eval ... ) 2>&1`,
and the enclosing `2>&1` is the real trigger. GNU binds a pipeline
element's fd 2 to the fd-1 object the pipeline INHERITED (the
enclosing stream): execute_cmd.c:2620 execute_pipeline connects the
elements with fresh pipes first, and an ambient `2>&1` (enclosing
compound, brace group, subshell, function, or `exec`) dups onto that
inherited object, never onto the inter-stage pipe.

rubash's sequential pipeline driver folded every stage's captured
stderr into next_input whenever fd 2 resolved to Stdout, so a
non-final stage's diagnostic became the next stage's stdin and died
unread — dash words and ordinary names alike, timed or not
(`{ nosuchA | nosuchB | nosuchC; } 2>&1` showed only the last). The
final stage was accidentally right: its next_input IS the pipeline
output. The concurrent-prefix recovery merge in the same function
had the same flaw.

Fix: under an ambient binding, non-final stages accumulate their
stderr in stage order and it is written with the pipeline's output
(preceding the final stage's contribution, matching GNU's
concurrent starts); the concurrent prefix's captured stderr takes
the same route instead of the pipe payload (its bytes also leave
the input_timing payload length, which now measures only real
payload). A stage's OWN `2>&1`/`|&` is untouched — those bytes are
resolved against the pipe-bound fd 1 by
route_pipeline_stage_streams before this merge and arrive already
folded.

Regression pins:
tests/issue1552_ambient_stderr_pipeline_diagnostic_regressions.rs (8)
— both filed forms, ordinary-name timed first stage, three-stage
ordering, external-prefix middle stage, the comsub `2>&1` capture
idiom, and the no-redirect / stage-own-dup controls.
@caomengxuan666
caomengxuan666 merged commit 5b8dcd3 into master Oct 25, 2026
7 of 10 checks passed
@caomengxuan666
caomengxuan666 deleted the fix5/slice-followup-a branch October 30, 2026 01:41
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