Skip to content

fix(executor): yield parseable sleep to a real spawn on unix (Refs #3488) - #3510

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/3488-sleep-yield-unix
Nov 14, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/3488-sleep-yield-unix

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Refs #3488 — implements the ruling (a) recorded on the issue (user ruling, 2026-11-14).

根因

bats-core 的 timeout 监督靠进程同一性工作:bats-exec-test 的 bats_start_timeout_countdown 在测试壳内设 TERM trap,倒计时到期后 bats_kill_processes_of 用 ps 枚举测试壳的整棵后代树并逐 pid 发 TERM。GNU 下前台 sleep 是真子进程(jobs.c:2265 make_child,壳在 jobs.c:3064 wait_for 里等),投递杀掉子进程、等待立即返回、挂起的 trap 在下一命令边界运行(execute_cmd.c:643 run_pending_traps),内层约 1s 结束。

rubash 的进程内 sleep 仿真没有进程同一性(#2995 的缺口):树里根本没有 sleep 子进程可杀;投向壳本身的那一份投递又被 virtual-child 模型按「同组发送者 = 只投壳」归类而递延到 sleep 跑完,于是内层跑满约 10s(实测 rubash 10271ms/10439ms vs GNU 1021ms),timeout.bats #2/#3 只有最后一行 ((SECONDS < 10)) 不过。

改动

政策层翻转,不动 trap 递延机制本体:

  • src/executor/command_registry.rs:sleep 行的 resolvable 策略 Replace → Yield(附 2026-11-14 裁决注释,体例同 stty 行的用户裁定注记);模块文档与 ResolvablePolicy::Yield 的文档同步改述;一致性测试里「无行 Yield」的断言改为「Yield 行恰为 sleep」并注明裁决出处;TimedProducer 面补登 per-face 记录 (Replace, FailClosed)——该模型只查 fail-closed 谓词(可解析则建模、不可解析则退让),行策略翻转后账本须如实记其自有规则,per_face_stand_down_records_are_exact 同步扩为五条。TimedConsumer 的 KD-4 记录与 timed 模型代码一律不动(审计 R6:timed 面是语义模型、不是分发)。
  • src/executor/command_dispatch_primary.rs:Sleep 臂在 unix 下追加一道既有政策谓词 registered_stand_down("sleep")——行策略翻为 Yield 后该谓词在可解析轴为真,unix 上可解析形态全面退让 execute_external 真 spawn。job control: foreground child SIGTSTP stop is invisible to the shell (no WUNTRACED/tcsetpgrp in interactive wait path) #2398/[bug] Interactive shell stops itself on ^Z instead of only the foreground job #2836/[bug] Single-command -c self-invocation increments SHLVL one extra time (no exec-replacement) #1497 三道历史退让在 unix 上被其涵盖、保留存档;非 unix 面不求值该子句,fastpath 原样保留(Windows 无此 watchdog 契约)。不可解析形态与不可解析名称([bug] time 计时管道后段命令失败被吞(rc 0 应为 127) #1545 的 127 路径)行为不变。
  • docs/builtins.md:fastpath 表 sleep 行补 stand_down=Yield 裁定注记(体例同 stty 行),清单引言加 unix 例外一句。
  • tests/issue3488_bats_timeout_real_sleep.rs(新钉,首笔提交):断言 ① 前台 sleep 期间壳的子进程清单(ps 枚举,与 bats 同法)里有真 sleep 进程;② 树级 TERM 投递在 1s 后落地、墙钟 < 3s 且 TERM trap 在边界运行。钉内脚本形态已对 GNU bash 5.3.20 实测(watchdog 形态 1.03s、trap 运行、rc 42)。

GNU 锚点

  • jobs.c:2265 make_child(外部命令皆真 fork);jobs.c:3064 wait_for
  • trap.c:553 trap_handler → trap.c:545 set_trap_state(投递只记录);trap.c:328 run_pending_traps;execute_cmd.c:643(命令边界运行挂起 trap)
  • builtins/evalstring.c:125 should_suppress_fork(-c 尾命令 exec-replace 与本改动正交、原样保留)

本 PR 删除了什么

未删除任何产品代码路径:进程内 sleep 本体(builtins/sleep.rs、sleep_interruptibly、virtual-child 机制)保留给非 unix 面。替换掉的只有与新裁决矛盾的账本表述:command_registry.rs 里「Yield 无行使用」的模块文档与枚举文档两处、一致性测试中「无行 Yield」断言一处(改为精确定集断言),以及 builtins.md 中 sleep 行的旧备注。

验证状态

UNVERIFIED-BY-CAPTAIN

  • 已跑:rustfmt --check(改动文件全净);X299(Windows)双树腿全跑通——head/base 构建成功、registry 一致性测试 21/21、钉目标编译成功(Windows 下 0 测试,unix 门控预期形态)、RUSTFLAGS="-D warnings" 检查 rc=0 且零 error/warning 行(46 次 rustc 调用含 lib/bin/钉三目标实参记录,非空转)、utf8 hygiene clean、marker-lint 本地实跑 OK。lib 全量 head 1337 过 / 13 败 vs base 1340 过 / 10 败,名集差分全在 executor::path::tests 且经同树复跑证明为该模块在本机的不稳定抖动集(详见车道 REPORT)。
  • 欠腿(Linux 运行时):新钉的 fail-first 红(未修基线)与修后绿、守护茎(issue1034 / issue1497 / issue1545 / issue2241 / issue2398 / issue2836 共 8 茎)、bats timeout.bats 4/0。本地写墙关闭、box 磁盘满、老服务器 sshd 无横幅,三条 Linux 路由当前皆不可用,已在车道 REPORT 里逐项登记,待任一路由恢复由队长补跑。

@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — FAIL

Verdict: FAIL. Sole failing item is leg ⑦: a real macOS defect in issue2859. Everything else in this review is green; the rework is scoped to the macOS spawn face only. Dossier: review-3510/CAPTAIN-VERIFY.md (§§0–9), all 7 legs re-run from scratch on Linux (the previous review lane went silent; none of its numbers were used).

The defect (verified firsthand from live CI logs)

  • Baseline run 43405733520, macOS job: 1242 passed / 1 failed — the only failure name is issue2875; issue2859_jobs_face_reaped_job_does_not_count is ok in the macOS, Linux and GNU logs.
  • Head run 43476576194 (head 1e6846ee), macOS job: 1241 passed / 2 failed — failure names issue2859 + issue2875.
  • Second head macOS run (attempt 2, job 132320895843, completed 2026-11-14T03:51:01Z): 1241/2 reproduced, same two names — not a transient.
  • The panic, verbatim: assertion 'left == right' failed: run of "sleep 0.5", left 126 / right 0, at src/api/consumer_tests.rs:2911. The failing command is a foreground sleep 0.5 on unix — exactly the face this PR flips to a real spawn.
  • Linux control: bin-first bilateral single-cell of the same issue2859 stem — head 1/1 = base 1/1, both green. The defect is macOS-specific; that does not excuse it.
  • The fix(builtins): invalid-identifier compound operands report plain word; declare 'a[]=x' reports bare bad array subscript (Refs #3186) #3464 issue2859 VOID ruling does not apply here: that was a bilateral leg on a PR that did not touch the sleep face; here baseline-green → head-red twice, with this PR as the only variable.

What is green (do not re-open)

  • ① Pin fail-first on base 5219be90: 0/2, both reds with the exact expected signatures (no real sleep child; children seen: []; wall clock 10.028 s).
  • ② Pin on head: 2/2 in 1.14 s.
  • ③ Guards (8 stems): 1034, 1545, 2241, 2836×3 all green. issue1497 (0/1) and issue2398 (1/2 → 0/2 on re-run) are pre-existing, base-identical symptoms — not regressions, and not in this PR's scope.
  • ④ bats-core test/timeout.bats (bash-shim method): head 4/4, base 2/4 — base fails Track GNU Bash upstream test progress in CI #2/Track GNU Bash upstream test progress in CI #3 on ((SECONDS < 10)), the field case this PR fixes.
  • ⑤ Linux lib head-vs-base: 1246/0/4 = 1246/0/4, failure name-set difference ∅.
  • ⑥ -D warnings: both sides rc=0, zero diagnostics, 1,522 flagged rustc calls per side (non-idle).
  • Intersection vs live master aea1e241: ∅ (31 interval files ∩ 4 PR files). Hygiene: git diff --check and rustfmt --check clean.
  • GNU anchors re-verified (trap.c:328/545/553, jobs.c:2265/3064, execute_cmd.c:643, evalstring.c:125) and the oracle check holds (1.01 s, TRAPPED, rc 42).

Rework spec

Investigate why the macOS external spawn/parse path reports 126 for sleep — Session-context PATH resolution, the spawn mechanism, and any macOS-specific face. Linux spawn is normal, so macOS CI is the arbiter.

Rework acceptance: one fresh head macOS CI run with issue2859 green and no new failure names; Linux legs ①–⑥ re-run still green. The §6 pre-existing reds (issue1497, issue2398) do not expand this PR's scope — unless the rework touches the jobs face, in which case re-check them.

Notes for the merge (not for this round): type sleep still reporting emulation on unix belongs to #2983's scope, not this PR. The live PR body carries an UNVERIFIED-BY-CAPTAIN marker and a "to be filled" verification section — at merge time the merge body must carry the verified legs instead (same as the #3524 precedent).

…859 126)

Root cause: on macOS /bin/sleep is a Mach-O image (MH_MAGIC_64), and
is_enoexec_candidate treated every non-shebang, non-ELF executable as an
ENOEXEC candidate, so execute_external_inner refused it pre-emptively
with "cannot execute binary file" / 126 before any execve was attempted
-- foreground `sleep 0.5` returned 126 and
issue2859_jobs_face_reaped_job_does_not_count panicked (left 126, right 0).
Linux stayed green only because /bin/sleep there is ELF.

Fix: recognize Mach-O magics (thin 32/64, both byte orders; fat/universal
disambiguated from Java class files by the arch count) in the new shared
helper is_macho_binary_magic, and admit them in both pre-emptive
classifiers -- is_enoexec_candidate and program_execs_directly -- gated
on cfg!(target_os = "macos"). On Linux a Mach-O stays an ENOEXEC
candidate, preserving GNU's post-ENOEXEC refusal (126): the GNU 5.3
oracle reports 126 for a Mach-O file on Linux, and an ungated exclusion
would have flipped it to the glibc /bin/sh fallback's 127 (verified by
strace + oracle). program_execs_directly now reads with read() + slicing
so 4-7 byte files are still judged on their real prefix.

GNU anchors: execute_cmd.c:6126-6233 shell_execve, general.c:718-741
check_binary_file (untouched -- it stays the post-ENOEXEC check).

Tests: new unit tests macho_binary_magic_recognizes_thin_and_fat_images
and native_binary_classifiers_admit_macho_images (2/2, incl. short-file
cases); repro-macho: control /bin/sleep rc 0, fake Mach-O rc 126 on Linux
(GNU parity), no execve attempted.
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核 PASS(新头 7eb49f87)

FAIL 项已闭合:macOS issue2859_jobs_face_reaped_job_does_not_count——根因是 macOS /bin/sleep 为 Mach-O,预分类器把一切非 #! 非 ELF 文件当 ENOEXEC 候选、未 spawn 即 126 拒绝;修法在两处预分类器以 cfg!(target_os = "macos") 门控承认 Mach-O(thin 32/64 双字节序、fat 按 arch 数 1–16 与 Java class 区分),另修 4–7 字节短文件前缀判。四处调用点已核全覆盖。

腿(复核员自跑 r3 + 亲读 r2 原始日志,队长核查日志原文):

  • 钉:头 2/2;基底 fail-first 0/2(rc=101,签名与一轮逐字同一)。
  • issue2859 stem 头 6/6;邻居 issue1545 13/13;Mach-O 单测 2/2。
  • lib 双边:头 1248/0/4 vs 基底 1246/0/4,失败名集差 ∅(头多 2 = 新单测)。
  • RUSTFLAGS="-D warnings":rc=0,0 诊断(lib 重编,非空闲)。
  • Linux 126 保持:自制 32 字节 thin Mach-O,GNU 126 / 基底 126 / 头 126,strace 无 execve;/bin/sleep 对照 rc 0。
  • macOS 验收:新头 CI run 43609822916,macOS 1245/1/4(基线 1242/1、旧头 1241/2),issue2859 六格全绿,三任务唯一失败名皆 issue2875(基线)。
  • 1034/2241 与 bats L4 的环境噪声裁断成立(三方双边复核皆绿)。

残差:截断 Mach-O(< 28 字节头)在 macOS 上被承认为原生、execve 失败路径未实测——不拦本 PR,已立跟进单 #3625(P3-low)。

合入:Refs #3488(合后队长手关 #3488,随后派 #2983);合入正文须去掉 UNVERIFIED-BY-CAPTAIN 并代入已验腿。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 14, 2026 16:03
@caomengxuan666
caomengxuan666 merged commit 8eb0787 into master Nov 14, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 14, 2026
…(Refs #3500)

## Summary

A bad substitution inside a compound-assignment element was diagnosed with the `${...}` fragment instead of the element word. GNU quotes the whole element and reports the word being expanded, e.g. `a=([0]=${-x})` → `\[0\]=${-x}`.

## Root cause

`subscripted_parameter_element` hands the RHS to `whole_word_parameter_single_value`, whose `has_word_operator` arm misjudges `${-x}` (empty `var_name` at position 0) and expands it standalone with no pending diagnostic word installed, so `expand_braced_parameter_word` falls back to the synthesized `${-x}` fragment. The plain-element path had the same gap (the walker default names the raw token, without `quote_assign`'s `\[sub\]` quoting).

## Fix

- New `compound_element_diag_word` (module level, mirrors GNU `quote_assign`): indexed `[sub]=value` → `\[sub\]=value`; assoc elements → the bare value; a quoted value keeps the fragment's own word.
- The element word is installed as the pending diagnostic word at the two element-expansion sites (`subscripted_parameter_element`, `compound_plain_element_value[_fields]`), install-if-absent.
- An `assoc` parameter is threaded through `expand_compound_positional_at_assignment[_bare]` (4 call sites), computed by new `compound_assignment_is_assoc` (live `ASSOC_VARS` marking OR the declare-assoc hint).
- New declare-assoc thread-local hint (`DECLARE_ASSOC_HINT` + RAII guard), installed by the `declare_assoc_operand_names()` scan in `command_prepare.rs`, covering the `declare -A` command-word expansion path, which runs before the declare builtin.
- Bare (eval-argument) flatten installs no word: documented residual, not fixed here.

GNU anchors: `arrayfunc.c:557/:581/:594-599/:606/:630/:938-977/:1108-1129`; `subst.c:10278`.

## Regression pin

`tests/issue3500_compound_elem_diag_word.rs` — 19 cells (10 fix shapes + 6 guard shapes + 2 quiet guards + 1 first-bad-wins). Expectations verified against GNU bash 5.3.20.

## Verification (captain-verified legs)

- Merged head `d11ffbcf` (update-branch of `00c0a07` onto tip `021705cf`, three update-branch rounds: `8eb07872` (#3510), `12aa434e` (#3455, disjoint files), `021705cf` (#3220, disjoint files); intersection `{src/executor/assignment_expansion.rs}` via #3599, hunks ≥164 lines apart, no overlap): pin **19/19**; same-file neighbor issue3501 pin **68/68**; adjacent assoc-face issue3573 pin **14/14**.
- Fail-first (top-up form on base `11719895`): pin 11/19 — 8 red, each reporting `Some("${-x}")` where the element word is expected.
- Neighbor `array_compound_quote_regressions` 17/17 on head.
- Full lib bilateral (bin built first): base 1247 passed / 0 failed / 4 ignored; merged head 1249 passed / 0 failed / 4 ignored — failure name-set diff empty (the +2 are the tip's Mach-O unit tests from #3510).
- `RUSTFLAGS="-D warnings" cargo check` RC=0, 0 warnings; `cargo fmt --check` RC=0; utf8-boundary hygiene clean.
- Head CI run `43619058744` (on `10da84b4`, the second update-branched head): 3 failing jobs, sole FAILED name `issue2875` (known baseline). A fresh CI run starts on the final head `d11ffbcf` from the third update-branch; it differs from `10da84b4` only by the disjoint #3220 merge.
- `continuation.rs` untouched.

Refs #3500

## 本 PR 删除了什么

Nothing — purely additive fix plus the new regression pin. No code, test, file, or behaviour removed.
caomengxuan666 added a commit that referenced this pull request Nov 15, 2026
caomengxuan666 added a commit that referenced this pull request Nov 15, 2026
caomengxuan666 added a commit that referenced this pull request Nov 15, 2026
…875 veil

The issue2875 panic ran nextest's fail-fast at test 23/1249, so two
pre-existing Linux CI failures were never observed:

- tests/{issue2043,issue2046}/fn rubash() resolved the bin target by
  popping twice off current_exe — correct only under the deps/ layout.
  Under the newer cargo build-dir layout (test binaries inside
  build/<pkg>/<fingerprint>/out/) the walk lands inside the fingerprint
  dir and spawn fails ENOENT. Anchor on env!("CARGO_BIN_EXE_rubash")
  instead, like the rest of the suite.

- corpus.json case sleep-fake kept its Replace-era expectation
  (rc=0, no FAKE line) after #3510 flipped the unix sleep face to
  ResolvablePolicy::Yield (user ruling 2026-11-14, rubash#3488). A
  yielding face spawns the PATH sleep, so the fake script runs —
  GNU shape. Relabelled registered->gnu per the command-sleep-fake
  precedent in the same file.

Refs #3605

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
caomengxuan666 added a commit that referenced this pull request Nov 15, 2026
Same ruling as the corpus flip (rubash#3488 user ruling 2026-11-14,
#3510): the unix simple-command face yields every parseable sleep form
to a real spawn, so the PATH fake runs and prints FAKE-sleep — the GNU
shape (bash has no sleep builtin). The pin asserted the retired
Replace-era expectation 'fake sleep must not run'.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
caomengxuan666 added a commit that referenced this pull request Nov 16, 2026
…fixes (#3750)

* ci: record Session::cwd in api-ledger; surface issue2875 resolver diagnostics

Two pieces of master-CI repair:

- docs/api-ledger.md: niubash master calls Session::cwd
  (repl.rs spell-correct path, shell.borrow().session().cwd()) but
  the §5.5 census entry was never recorded — the 'niubash builds
  against this tree' check now fails census-missing. Record the face
  in §2.3 and the §6 symbol block.
- consumer_tests.rs issue2875_require_rubash_binary: the CI panic
  reports no diagnostics about which resolver arm fired or what
  current_exe was. Report CARGO_BIN_EXE_rubash, both arms, and the
  resolved path in the assert so the next failing run names the
  actual gap instead of only the precondition.

Refs #3605

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(test-resolver): accept the build/<pkg>/<fp>/out test-binary layout

CI's newer toolchain places the unit-test binary for a package with a
build script inside the package's own build-dir out dir
(target/debug/build/rubash/<fingerprint>/out/rubash-<fingerprint>), so
the deps-only gate in test_rubash_binary_from_current_exe returned
None and issue2875_input_procsub_child_runs_as_rubash_shell panicked
on every CI run while passing locally under the classic deps layout.

Accept 'out' alongside 'deps'; the ancestor walk already reaches
<target>/debug/rubash (which the workflow's cargo build --locked step
produces) from that layout, and a file literally named rubash[.exe]
still never matches inside it, so production fallback to current_exe
is unchanged.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test: fix masked out/-layout + stale-corpus failures under the issue2875 veil

The issue2875 panic ran nextest's fail-fast at test 23/1249, so two
pre-existing Linux CI failures were never observed:

- tests/{issue2043,issue2046}/fn rubash() resolved the bin target by
  popping twice off current_exe — correct only under the deps/ layout.
  Under the newer cargo build-dir layout (test binaries inside
  build/<pkg>/<fingerprint>/out/) the walk lands inside the fingerprint
  dir and spawn fails ENOENT. Anchor on env!("CARGO_BIN_EXE_rubash")
  instead, like the rest of the suite.

- corpus.json case sleep-fake kept its Replace-era expectation
  (rc=0, no FAKE line) after #3510 flipped the unix sleep face to
  ResolvablePolicy::Yield (user ruling 2026-11-14, rubash#3488). A
  yielding face spawns the PATH sleep, so the fake script runs —
  GNU shape. Relabelled registered->gnu per the command-sleep-fake
  precedent in the same file.

Refs #3605

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* style: fmt-wrap issue2875 resolver diagnostics

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: drop Session::cwd ledger record — adjudicated to the #3704 + niu#1021 lockstep pair

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test: flip issue2036 shape1 sleep pin to the yield/GNU shape

Same ruling as the corpus flip (rubash#3488 user ruling 2026-11-14,
#3510): the unix simple-command face yields every parseable sleep form
to a real spawn, so the PATH fake runs and prints FAKE-sleep — the GNU
shape (bash has no sleep builtin). The pin asserted the retired
Replace-era expectation 'fake sleep must not run'.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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