Skip to content

fix(executor): slice the process-global command lookup cache per session (rubash#2078) - #2149

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2078-parse-cache-session
Oct 29, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2078-parse-cache-session

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #2078

Root cause

The command-resolution cache in src/executor/path.rs (introduced by #159) was a process-global single-fingerprint store: one results/listings/probes set guarded by one env fingerprint, plus one hashed map mirroring every session's user-visible __RUBASH_HASH_TABLE. The mirror deliberately survives fingerprint resets (#1137), so two Sessions held in one process (the embed API's supported shape, contract Q5) cross-contaminated their parse/resolution results:

  • Session A hash -p X tool, then Session B resolves tool: B's find_user_command consulted the one global mirror before its PATH walk (path.rs hashed consult) and returned — and executed — A's path.
  • Session B hash -r (or any PATH bind → flush_command_hash_table) flushed the one global mirror and dropped Session A's remembered locations; A's pinned command then died with 127.

Fix — the issue's key-extension shape

The static store becomes a map of per-session slices keyed by SESSION_SCOPE_KEY (__RUBASH_SESSION_SCOPE), stamped fresh per Executor::new entry (src/executor/init.rs):

  • an env that already carries the token keeps it — a lineage re-entering the constructor on a cloned env (the fork-copy shape command_substitution_executor uses) stays on the parent's slice, exactly the sharing the hashed_cache_fork_guard brackets assume;
  • the token is in-memory only: __RUBASH_* names are excluded from the initial export set (env_helpers::is_initial_export_candidate) and from the spawn env (rubash_spawn_inherited_state filter), so it never crosses a process boundary;
  • env maps without the token (hand-built test maps, composed child-env lookups) share the "" fallback slice with the old single-store behavior;
  • slices are capped at 16 live scopes with clear-on-overflow — a pure cache, so eviction is a cold cycle, never a correctness change.

Chosen over re-homing the cache into Session: every cache entry point already receives the session's env map (and the builtin faces receive only VarTable), so key extension isolates the whole class with a fraction of the ripple a per-Session instance would need.

GNU anchor: findcmd.c:367-380 search_for_command consults THAT shell's own hashed_filenames, and sv_path (variables.c:5919) → phash_flush (hashcmd.c:57) flushes that shell's table — nothing in GNU shares a hash table between shells.

Verification

  • tests/issue2078_session_parse_cache_regressions.rs (new, 4 pins, two Sessions driven through the Tier-C API in one test process; execution-based observables):
    • fix: 4/4 pass;
    • pristine d5c172ae: pins 1-2 fail with the issue's exact signatures — pin 1: B executes A's binary (count ["A","A"], expected ["A","B"]); pin 2: A's pinned command exits 127 after B's hash -r. Pins 3-4 are guards that hold on both (interleaved fingerprints; single-session Q5 semantics).
  • cargo test --release --lib: 1063 passed, 0 failed.
  • Hash/session integration suites (issue1360/1361/1362/1368/1382/1489/1551/1648/2066, session_api, session_api_interrupt): every failure is byte-identical on pristine master — pre-existing, environment-bound.
  • scripts/true-baseline.sh type builtins shopt invocation (WSL GNU Bash 5.3 patch 20): fix == pristine (type 6 env=0 — the harness's own /tmp/bash vs /tmp/rubash.exe shell-name asymmetry; builtins/shopt/invocation all 0).

Disclosure

cargo fmt fails in this environment (os error 206, the documented hook precedent); rustfmt --edition 2021 was run explicitly over every changed file instead, and the commit uses --no-verify for that reason only.

Do not merge — awaiting review.

…ion (rubash#2078)

The command-resolution cache in executor/path.rs (rubash#159) was a
process-global SINGLE-fingerprint store: one results/listings/probes set
guarded by one env fingerprint, plus one `hashed` map mirroring every
session's user-visible `__RUBASH_HASH_TABLE`. The mirror deliberately
survives fingerprint resets (rubash#1137), so two Sessions held in one
process (the embed API's supported shape, contract Q5) cross-
contaminated their parse/resolution results:

- Session A `hash -p X tool`, then Session B resolves `tool`: B's
  find_user_command consulted the one global mirror before its PATH walk
  and returned (and executed) A's path.
- Session B `hash -r` or any PATH bind flushed the one global mirror and
  dropped Session A's remembered locations (the command died with 127).

Fix, in the issue's key-extension shape (chosen over re-homing the cache
into Session because every cache entry point already receives the
session's env map, while the builtin faces receive only VarTable): the
static store becomes a map of per-session slices keyed by
SESSION_SCOPE_KEY (`__RUBASH_SESSION_SCOPE`), stamped fresh per
Executor::new entry; an env that already carries the token keeps it, so a
lineage re-entering the constructor on a cloned env (the fork-copy shape
command_substitution_executor uses) stays on the parent's slice - exactly
the sharing the hashed_cache_fork_guard brackets assume. The token is
in-memory only: `__RUBASH_*` names are excluded from the initial export
set (env_helpers is_initial_export_candidate) and from the spawn env
(rubash_spawn_inherited_state filter). Env maps without the token
(hand-built test maps, composed child-env lookups) share the "" fallback
slice with the old single-store behavior. Slices are capped at 16 live
scopes with clear-on-overflow - a pure cache, so eviction is a cold
cycle, never a correctness change.

GNU anchor: findcmd.c:367-380 search_for_command consults THAT shell's
own hashed_filenames, and sv_path (variables.c:5919) -> phash_flush
(hashcmd.c:57) flushes that shell's table; nothing in GNU shares a hash
table between shells.

Verification:
- tests/issue2078_session_parse_cache_regressions (new, 4 pins, two
  Sessions driven through the Tier-C API in one test process): 4/4 pass
  on the fix; pins 1-2 fail on pristine master with the issue's exact
  signatures (B executes A's binary, count [A,A] vs expected [A,B];
  A's pinned command exits 127 after B's hash -r).
- cargo test --release --lib: 1063 passed, 0 failed.
- Hash/session integration suites (issue1360/1361/1362/1368/1382/1489/
  1551/1648/2066, session_api, session_api_interrupt): every failure is
  byte-identical on pristine master - pre-existing, environment-bound.
- scripts/true-baseline.sh type builtins shopt invocation (WSL GNU Bash
  5.3 patch 20): fix == pristine (type 6 env=0 harness shell-name
  asymmetry, /tmp/bash vs /tmp/rubash.exe; all three others 0).

fmt disclosure: `cargo fmt` fails in this environment (os error 206, the
documented hook precedent); rustfmt --edition 2021 was run explicitly
over every changed file instead, and this commit uses --no-verify for
that reason only.
@caomengxuan666

Copy link
Copy Markdown
Member Author

主会话审阅(2026-10-30,含 GNU bash 5.3.20 oracle 实测):

行为锚核实通过——本单要求的行为就是 5.3.20 的模型,主会话在自编 oracle(bash 5.3.20)上逐条验过:① hash -p 钉与 hash 表是单 shell 私有状态,另一 shell 以不同 PATH 独立解析、互不可见;② 给 PATH 赋值即冲掉本 shell 的 hash 表(sv_path variables.c:5919 函数体仅一句 phash_flush(),赋值后 hash 输出为空、type 按新 PATH 解析);③ GNU 以「一 shell 一进程」天然达成隔离,嵌入式多 Session 进程里按 session 切片正是该保证的等价复现。PR 引的 findcmd.c/sv_path 锚点与源码、实测均一致,方向(key-extension 切片)采用 issue 给出的第一形状,认可。

合并前需改一处(语义,非风格):CACHE_SLICE_CAP=16 到顶时 caches.clear() 是全量清——其中 hashed 镜像不是纯缓存,它是用户可见状态(hash -p 钉、hash 列表与命中数)。「per-request session」宿主(PR 正文自己举的例子)滚过 16 个 scope 时,会把仍然活着的 session 的 hash 钉一并清掉:A 的 hash -p 钉失效后命令改走 PATH 甚至 127——这正是 pin 2 修掉的失败形状换了扇门(从「被 B 的 hash -r 冲掉」变成「被第 17 个 scope 的创建冲掉」)。GNU 的保证是 shell 的 hash 表随 shell 终生,清表只能由本 shell 的 PATH 赋值/hash -r 触发。请拆开两类数据:纯解析缓存(results/listings/probes)可维持上限+逐出(逐出请改单片 LRU,不要全清);hashed 镜像豁免逐出(它有界于本 session 自身的 hash 使用量)。补一条钉:第 17 个 scope 创建后,存活 session 的 hash -p 钉与 hash 输出不变。

记录在案、不拦的两点:① scope 令牌从克隆 env 继承(lineage 复入)在本设计内成立,令牌不出进程、且同进程的 session 同属一个宿主,故不按 #2129 的跨界伪造面处理;但请在令牌注释里写清这条边界(环境若由不可信输入拼装,宿主不得直喂 Executor)。② 无令牌 env 共用 "" 回退片:Executor 入口恒盖章,实际 session 不受影响,与正文披露一致。

CI:其余全绿;Windows 金标 1 红为 issue_1903::issue1903_write_open_blocks_until_reader_arrives——与 #2147 上红的另一条 #1903 钉(foreign marker)是同族不同钉、不同分支各红一次,fifo 时序钉在 Windows runner 上的散发 flake 特征明显,主会话已触发重跑;若转绿不拦本 PR,但该族钉的 runner 稳定性主会话会另行跟踪。

…rubash#2078 review)

The 16-scope cap cleared the WHOLE store on overflow, including the
hashed mirror of still-live sessions. The mirror is not a cache: it is
the user-visible hash table (hash -p pins, hash listing), and GNU keeps
a shell's hashed_filenames for the shell's whole lifetime — entries die
only by that shell's own PATH assignment (sv_path, variables.c:5919 =
hashcmd.c:57 phash_flush) or hash -r. A per-request host rolling past
16 scopes would silently drop a live session's pins, re-entering pin
2's failure shape (pinned command falls back to PATH/127) through the
cap instead of a sibling's hash -r.

Split the store: CommandLookupStores holds the pure resolution slices
(results/listings/probes/scaffold — capped, single-slice LRU eviction,
never a global clear) apart from the per-scope hash-table mirrors,
which are never evicted and are bounded by each scope's own hash usage.
Mirror-only operations (hash -p write, PATH-bind flush, fork guard) no
longer create or churn resolution slices either.

New pin 5 in tests/issue2078_session_parse_cache_regressions.rs: a live
session's hash -p pin and hash listing survive 20 churn sessions
rolling the cap. It fails on the pre-split tree (clear-on-overflow) and
passes here; pins 1-4 unchanged and green.
@caomengxuan666

Copy link
Copy Markdown
Member Author

主会话接管返工完成(作者侧不迭代,按惯例主会话侧收尾):新 commit 01491f0 压在本 PR 原提交之上,未改作者提交。

按审阅意见拆开了两类数据:CommandLookupStores 里纯解析切片(results/listings/probes/脚手架)维持 16 上限、改单片 LRU 逐出、再无全量 clear;hashed 镜像独立成图、豁免逐出(GNU 的 hash 表随 shell 终生,条目只死于本 shell 的 PATH 赋值/hash -r,这两条本就已按片作用域)。镜像类操作(hash -p 写、PATH 绑定冲刷、fork guard)顺带不再创建/搅动解析切片。

新增 pin 5(17+ scope 滚动后存活 session 的 hash -p 钉与 hash 输出不变):在拆分前的 clear-on-overflow 树上实测红、在本树 5/5 全绿(本地已做对照)。CI(01491f0e)全绿、mergeable_state: clean——含先前红过的 Windows 金标(本轮干净通过)与 niubash builds。可合,等主会话裁定。

@caomengxuan666
caomengxuan666 merged commit eace24c into master Oct 29, 2026
9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2078-parse-cache-session 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

Development

Successfully merging this pull request may close these issues.

进程级单指纹命令解析缓存(path.rs)可使同进程多 Session 互串解析结果

1 participant