Skip to content

fix(executor): warm the cwd anchor when pushd/popd move the session cwd - #3384

Merged
caomengxuan666 merged 2 commits into
unixwin:masterfrom
Uarz:fix/issue-3336
Nov 12, 2026
Merged

caomengxuan666 merged 2 commits into
unixwin:masterfrom
Uarz:fix/issue-3336

Conversation

@Uarz

@Uarz Uarz commented Nov 11, 2026

Copy link
Copy Markdown
Member

Fixes #3336

What was fixed

After pushd into a deleted cwd, external children ran in the startup cwd instead of inheriting the deleted cwd.

Root cause: rubash never chdirs the process (G-02 model), so a child can only land in a deleted inode via an anchor fd fchdir (stamp_spawn_cwd). execute_cd warms the cwd anchor after every move, but the pushd/popd funnel never did — so after a pushd move the spawn stamp found no anchor naming the current cwd and let the child through unstamped, inheriting the process cwd (= startup cwd).

The fix is in execute_stack_builtin (the single pushd/popd/dirs funnel, src/executor/trap_stack_builtins.rs, +24 lines, no deletions): compare the session cwd before and after execution and call refresh_cwd_anchor() when it moved — the same call execute_cd uses. The gate is "cwd moved", not status == 0: a readonly-bind failure still moves the chdir (pushd.rs G-02 note), while pushd -n / dirs do not move and need no refresh. This covers pushd entry, popd top, pushd +1 rotation, and bare pushd swap in one place.

GNU: pushd's Dir operand delegates to cd_builtin (builtins/pushd.def:298; rotation via change_to_temp → cd_to_string → cd_builtin, :265/:284/:576-586/:562-574; popd top via cd_to_string, :400); change_to_directory chdirs the process cwd (builtins/cd.def:600); execute_disk_command forks via make_child and the child inherits the shell's cwd (execute_cmd.c:5771) — a deleted directory is still the child's cwd, with getcwd failing.

What was deleted

Nothing. No files, features, or tests were removed.

Verification (all run, per-item counts)

Owed

  1. Background spawn face is still broken (needs a separate issue): spawn_async_direct → raw_fork_async_child (external_inner.rs:5605) chdirs by path string and never consults the anchor — a background ls in a deleted cwd reports chdir: …: No such file or directory, rc 1, and this is broken after cd too (baseline measurement), so it is an independent [bug] kill -INT does not kill a non-monitor async job; GNU wait returns 130 #2925 async-spawn defect, not a [bug] after pushd into a deleted cwd, external children run in the startup cwd instead of inheriting the deleted cwd #3336 residual. Fix would be to pass the anchor fd into the raw-fork child as an fchdir fallback. A background case was in this pin originally and was removed because it was a false pass on the baseline (ls never ran, output was empty).
  2. Stale logical cwd under readonly PWD ([bug] cd 对软链父目录的 .. 按物理路径解析,默认与 -L 下均偏离逻辑目录 #2796 family, needs a separate issue): after readonly PWD; cd a, a subshell's logical cd .. resolves against the stale $PWD (lands in /tmp); GNU resolves against the chdir-updated the_current_working_directory (lands in $D). Confirmed by baseline vs GNU measurement; out of this issue's scope.

After pushd into a deleted cwd, external children ran in the startup cwd instead of inheriting the deleted cwd. Root cause: rubash never chdirs the process (G-02 model), so a child can only land in a deleted inode via an anchor fd fchdir (stamp_spawn_cwd). execute_cd warmed the anchor after every move, but the pushd/popd funnel never did, so the spawn stamp found no anchor naming the current cwd and let the child through unstamped, inheriting the process cwd (= startup cwd). The fix, in execute_stack_builtin (the single pushd/popd/dirs funnel, trap_stack_builtins.rs, +24 lines, no deletions): compare the session cwd before and after execution and refresh_cwd_anchor() when it moved — the same call execute_cd uses. The gate is 'cwd moved', not status==0: a readonly-bind failure still moves the chdir (pushd.rs G-02 note), while pushd -n / dirs do not move and need no refresh. GNU pushd's Dir operand delegates to cd_builtin (pushd.def:298; rotation via change_to_temp -> cd_to_string -> cd_builtin, :265/:284/:576-586/:562-574; popd top via cd_to_string, :400); change_to_directory chdirs the process cwd (cd.def:600); execute_disk_command forks via make_child and the child inherits the shell's cwd (execute_cmd.c:5771).

Refs unixwin#3336

Regression pin tests/issue3336_pushd_deleted_cwd_spawn.rs (7 tests; fail-first 1 passed / 6 failed on the unpatched tree, 7/7 pass after; supervisor verified both rounds with the patch method). The issue's seed script seed_r88e2.sh: GNU 5/5 PASS, rubash 5/5 PASS after the fix. Adjacent: 20 files, 166 items green (issue3198 7/7, unixwin#3275 4/4, unixwin#3268 four files 12/12+9/9+10/10+19/19, dirstack/pushd family incl. unixwin#2424 21/21 — supervisor re-ran issue3198 and unixwin#2424). cargo fmt clean.
@caomengxuan666 caomengxuan666 added bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger labels Nov 11, 2026
@caomengxuan666 caomengxuan666 self-assigned this Nov 11, 2026
@caomengxuan666 caomengxuan666 added P2-medium 中:次要缺陷或边缘路径,排期修复 area:path path model, command lookup, drive/POSIX spellings, argv translation labels Nov 11, 2026
@caomengxuan666

Copy link
Copy Markdown
Member

Thanks for the PR. This is queued for captain review — collaborator PRs are reviewed by the captain before merge, and #3336 stays open until that review lands.

CI at intake: all 10 checks green (1 skipping). First pass: one funnel function, +24 lines, pin 7/7 fail-first (1 passed / 6 failed on the unpatched tree), plus 166 adjacent items green. Two things noted for the captain: your Owed items (background-spawn face, readonly-PWD stale cwd) are flagged as separate issues, and #3386 touches the same execute_stack_builtin function — the combined function will need one review. No action needed from you.

@caomengxuan666

Copy link
Copy Markdown
Member

Captain review — PASS (merge conditions below)

Joint review with PR #3386 (same funnel, one dossier: review-3384-3386/CAPTAIN-VERIFY.md). All numbers below are the reviewer's own runs on the old server (tree identity hash-verified against the PR head 2594cbb0; real merge-base f5f65d35 computed with git).

Declaration reconciliation — every claim checks out

  • Diff read line by line: execute_stack_builtin +24/−0 plus the new pin file, exactly as declared; nothing removed.
  • Pin bilateral: head 7 passed / 0 failed; base (top-up form) 1 passed / 6 failed — the single green cell is the cd-entry control, the six red cells are exactly the pushd faces (entry, popd top, pushd +1 rotation, bare swap, pipeline stage, sh -c getcwd). Per-name match with the declared fail-first.
  • Lib: 1245 passed / 0 failed / 4 ignored on base, on this head, and on the fix(cd): share the deleted-cwd dot-only move between -L and -P, trust stored logical cwd #3386 head — failure name sets identical (empty).
  • Adjacent stems re-run: issue3198 7/7, issue2796 12/12, issue3275 4/4, issue2391 6/6 (the declared 166-item adjacent list was spot-verified on this load-bearing subset plus the full lib).
  • fmt clean; marker-lint and UTF-8 boundary hygiene clean; full-target -D warnings shows no error under src/ and no new point from this diff (the run's only error is a pre-existing test-file hygiene point outside the diff).
  • Three-way probes (GNU 5.3.20 oracle vs base vs head, byte-compared after path normalization): the pushd-entry shapes match GNU on the head and diverge on the base exactly as the issue describes; GNU anchors in the body (pushd.def:298/:265/:400, cd.def:600, execute_cmd.c:5771) were re-read in the 5.3 sources and are accurate.

One correction to the merge note in the companion PR: the two stack-funnel changes do not auto-merge — a trial merge conflicts in trap_stack_builtins.rs (two additive blocks around the same status flow). The union shape is pre-verified in the dossier (anchor refresh gated on the move itself, as here; the companion's logical-cwd maintenance gated on a successful move) and a combined tree carrying both changes passes both pins (7/7 and 15/15) plus the full probe matrix. Whoever merges second applies that union shape on update-branch and re-runs both pins on the merged head.

Owed items — captain delimitation (evidence in the dossier)

  1. Background spawn in a deleted cwd: confirmed real on base, on this head, and on the combined tree, for both cd and pushd entry — the async raw-fork child chdirs by path string and never consults the anchor fd, prints its chdir: diagnostic to the shell's own stderr (it runs before the child's dup2 of the job redirects), and the job ends with status 1 where GNU is silent with status 0. One citation needs correcting before anything is filed from it: this is not [bug] kill -INT does not kill a non-monitor async job; GNU wait returns 130 #2925 (that number is the closed kill -INT item); the face stands on its own mechanism (spawn_async_direct → raw_fork_async_child).
  2. The readonly-PWD stale-logical shape: not reproduced in any of three measured constructions (in-process subshell and child-process forms, directory alive and deleted) — base and GNU agree byte-for-byte in the first two, and in the deleted-directory child form GNU's own output degenerates to relative-string values, so there is no bindable GNU target as stated. The measured readonly-family residue that does exist is recorded under the companion PR's owed item and is handled as one family there; the anchor half of it is already covered by this PR's move-gated refresh (probe C5 in the dossier).

Merge conditions: final head CI finished all-green (the current head already is); if this PR merges second, the union protocol above applies.

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 12, 2026 02:58
@caomengxuan666
caomengxuan666 merged commit d83951e into unixwin:master Nov 12, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:path path model, command lookup, drive/POSIX spellings, argv translation bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger P2-medium 中:次要缺陷或边缘路径,排期修复

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] after pushd into a deleted cwd, external children run in the startup cwd instead of inheriting the deleted cwd

2 participants