Skip to content

feat(jobs): Windows foreground Ctrl+Z stop surface — inject_console_ctrl_z + Stopped arm (Refs #3467) - #3538

Closed
caomengxuan666 wants to merge 4 commits into
masterfrom
fix/3467-windows-fg-ctrl-z-stop
Closed

caomengxuan666 wants to merge 4 commits into
masterfrom
fix/3467-windows-fg-ctrl-z-stop

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3467 (engine half of unixwin/niubash#877)

草稿:车道 CZ 进行中。虚拟 sleep 的 Stopped 载体是 #3467 上待裁的设计点,提案见 issue 评论;本 PR 按提案 A 实现为独立提交,可整体撤回。

修了什么

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)+ 非命令替换。
  • Windows 版 wait_fg_child_job_control:前台外部子进程登记为作业;收到请求时经 kill 内建已有的 Windows STOP 路径(signal_process 19/20 = 逐线程 SuspendThread)挂起子进程,按 waitchld 的停止簿记落 Stopped/当前作业、打印停止通知、把句柄停进 background_children,返回 128+SIGTSTP。unix 停止臂的簿记原样抽出为 settle_stopped_foreground_child 两端共用(unix 语义不变)。
  • Windows fg 的等待也可停止(fg 下再次 ^Z = 重停),同时挂起作业内其它存活成员;fg/bg 恢复沿用既有 deliver_job_sigcont 逐 pid ResumeThread。
  • (提案 A,独立提交)停止通道活跃时 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()(:4338 JSTOPPED、:4343 SIGTSTP 打断循环)、jobs.c:3220-3221 wait_for()(128 + WSTOPSIG)、jobs.c:3826 start_job()(fg/bg 恢复)。

验证

(进行中,下方逐项更新)

Owed

(进行中)

caomengxuan666 and others added 2 commits November 14, 2026 12:57
…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>
caomengxuan666 and others added 2 commits November 14, 2026 13:24
…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>
@caomengxuan666

Copy link
Copy Markdown
Member Author

静态审查发现一处需处理的竞态(尚未补并发复现):note_console_ctrl_z 先读取 FOREGROUND_STOP_WINDOWS,随后单独写 FOREGROUND_STOP_REQUESTED。两个 SeqCst 原子并不能把这两步变成同一操作:投递线程读到旧窗口仍活跃后,等待线程可能析构旧窗口并清空请求,甚至已为下一条命令重新 arm,投递线程此时才 store(true),请求便可能泄漏到下一次前台等待。这与注释中迟到 Ctrl+Z 不影响下一条命令的保证不符。需要把窗口身份/投递组成一个有线性化点的协议,并增加旧窗口结束与投递交错的确定性并发测试;当前保持 draft,不把本实现视为验收通过。此发现独立于 sleep 提案 A 的产品裁决。

@caomengxuan666

Copy link
Copy Markdown
Member Author

队长注记(#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 评论。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】s10 lane — 建议:FAIL(不合、不关;draft 保持)

head: 565b7fd base(merge-base): 8b48c16 文件: 6 个 +694/-63
关联 issue: #3467(状态:open,我核过的时间:2026-11-16 20:25 CST)— 本 PR 是 #3467 的先行实现,合入前不应关单

结论:不建议合入。钉子本身在合并形上全绿(下面给数),但队长 2026-11-14 06:02Z 静态审查指出的竞态仍未修,且本 PR 正文与 diff 已经不一致。按「正文描述了 diff 里根本不存在的修复 = 直接建议 FAIL」+ 队长「当前保持 draft,不把本实现视为验收通过」,本轮只发报告,不合不关。

F 点:

  • F1(阻断)note_console_ctrl_z 的「读窗口计数 → 写请求位」不是一个线性化操作,迟到的 ^Z 会泄漏到下一条前台等待。src/builtins/kill.rs:1062 先 FOREGROUND_STOP_WINDOWS.load(SeqCst) == 0 早退,:1065 再单独 FOREGROUND_STOP_REQUESTED.store(true);ForegroundStopWindow::drop 在 :1100-1101 是 fetch_sub 归零后 store(false),arm() 在 :1098 是裸 fetch_add 且不清请求位。可达交错(host 注入线程 vs shell 等待线程):
    1. host 线程 load(WINDOWS)==1 通过准入;
    2. shell 线程析构最后一个窗口:fetch_sub → 0,store(REQUESTED=false);同一条命令结束后开始下一条,arm():fetch_add → 1;
    3. host 线程此刻才 store(REQUESTED=true);
    4. 下一条命令的等待在第一个 slice 上 take_foreground_stop_request() 拿到 true → 上一条命令的 ^Z 停掉下一条命令的前台子进程。
      这正好违反 kill.rs:1041-1055 注释自己承诺的「a ^Z that raced the end of one wait can never stop the next command」。两个 SeqCst 原子各自成立,但组合起来没有线性化点。
  • F2(阻断,队长明确要求)缺「旧窗口结束 × 投递」的确定性并发测试。现有 6 格钉(tests/issue3467_windows_fg_ctrl_z_stop.rs)都是单序列行为格(stop / bg 恢复 / fg 重停 / 无 monitor 拒绝 / trap 旁路 / 窗口外丢弃),没有一格把 drop 与注入交错固定下来;也没有窗口身份(generation/epoch)可用于断言「请求属于哪个窗口」。
  • F3(文档级)正文与 diff 不一致:正文第 3 段写「(提案 A,独立提交)停止通道活跃时 sleep 仿真让位给它所仿真的 PATH 上真实 sleep 子进程」,但该提交 5e93fe4a 已被 357786de revert(revert 信息自述「Moved to its own stacked draft (branch fix/3467-sleep-carrier-a)」,即 feat(jobs): Windows virtual-sleep Stopped carrier — proposal A stand-down (Refs #3467) #3567)。diff 里没有任何 sleep stand-down 代码。正文未同步。
  • F4(文档级)正文 ## 验证 与 ## Owed 两节仍是「(进行中,下方逐项更新)」,PR 仍是 draft 状态未收尾。

修法建议(供续作参考,不是本轮已实施):

  • 把「窗口身份 + 投递」做成一个原子:例如 FOREGROUND_STOP_WINDOW: AtomicU64 存 (generation << 32) | armed,arm() 取 generation.wrapping_add(1) 并返回 ForegroundStopWindow(gen),注入端只做一次 compare_exchange/fetch_or 在同一原子上打位(拒绝条件 = armed==0 或 generation 已变),等待端 take 时读回自己的 generation、不匹配即丢弃;
  • 或者用 Mutex<Option<(u64 /*gen*/, bool /*requested*/)>> 把两者放进同一个锁域,线性化点就是锁;
  • 配一个确定性测试:直接调用 arm()/drop/note_console_ctrl_z() 三个 API,在持有旧窗口身份与已重 arm 两个状态下分别注入,断言请求只被旧窗口的 take 取到、新窗口 take 为 false(不需要真线程也能覆盖交错语义;如需真并发再加一次带屏障的压力格)。

机制核查(正面项,供后续吸收):

  • 不把 ^Z 塞进 shell 信号邮箱是对的:jobs.c:4949-4955 initialize_job_signals() 交互 shell 对自身 SIGTSTP 设 SIG_IGN,终端 SIGTSTP 经 jobs.c:2400 make_child() → give_terminal_to(pipeline_pgrp) 投给前台作业进程组;jobs.c:4077 waitchld() 的 WUNTRACED 只在 job_control && subshell_environment == 0 时成立,故 foreground_stop_channel_active()(interactive + monitor + 顶层 + 非命令替换)与 GNU 通道门一致。
  • 停止簿记抽出 settle_stopped_foreground_child 两端共用(jobs.c:4290 set_job_status_and_cleanup 的 JSTOPPED/jobs.c:3220-3221 wait_for() 的 128+WSTOPSIG 语义),unix 侧行为不变 —— 这是把两份改成一份,方向对。
  • Windows 停止投递复用 kill 内建既有 SuspendThread 面(19/20),与 fg/bg 的 deliver_job_sigcont 对齐,未新造第二套。
  • 无可疑/无关改动;无桥接/翻译层。

腿(逐项给数字与名字):

  • 钉自跑:issue3467_windows_fg_ctrl_z_stop 合并形(master tip 12fb4e6 + head)Windows 6/6(ctrl_z_bypasses_shell_tstp_trap / ctrl_z_outside_a_wait_is_dropped / ctrl_z_restops_job_under_fg / ctrl_z_stopped_job_resumes_with_bg / ctrl_z_stops_foreground_external_and_fg_resumes_it / ctrl_z_without_monitor_is_refused)。
  • fail-first:不适用 —— 本 PR 是新增宿主面(inject_console_ctrl_z 在 base 不存在),base 上该钉不编译;不按「钉红」判定。
  • lib 双边:未跑(见「未跑」)。
  • -D warnings:未跑(见「未跑」)。
  • 卫生:未跑(见「未跑」)。
  • CI:红腿 3 个(Rust tests / macOS / GNU goldens),run 43561573341 的 Rust tests 作业(132346973562)实读日志仅 api::consumer_tests::issue2875_…(src/api/consumer_tests.rs:2911)= 基线 issue2875;其余 9 腿 pass。
  • GNU 对照:Windows 无 SIGTSTP,本 PR 是宿主替代面,GNU 对照已在上面「机制核查」按 jobs.c 行号逐条对;不跑 Windows↔GNU 行为对照(平台不可比),Linux 停止语义由既有 unix 臂承载,不在本 PR 变更范围。

F 点汇总:F1/F2 阻断,F3/F4 文档级。

未跑/VOID 的腿:

@caomengxuan666

Copy link
Copy Markdown
Member Author

【P1 车道复核 · #3467 三选一裁决】superseded by #3746(不关闭本 PR)

本车道把 #3538 / #3567 / #3746 放在同一张语义桌上判定,结论:

判定依据(两台都实测,不是只凭 draft 状态)

本 PR 单独验证(merge origin/master c8109c585 前,worktree 独立构建):

  • cargo build(Windows MSVC debug)绿。
  • 钉 tests/issue3467_windows_fg_ctrl_z_stop.rs → 6/6 passed(含 bypasses_shell_tstp_trap / outside_a_wait_is_dropped / restops_job_under_fg / stopped_job_resumes_with_bg / stops_foreground_external_and_fg_resumes_it / without_monitor_is_refused)。

即本 PR 不是构建/pin 红的淘汰,而是设计选择落败:

  1. 规范面只留一套:fix(windows): foreground Ctrl+Z stop surface — inject_console_ctrl_z + Windows Stopped arms (Refs #3467) #3746 的静态面同名同契约(script_driver::inject_console_ctrl_z),但走 shell 自身信号队列(20 = SIGTSTP)并在两个非 unix 前台等待臂(wait_external_child、sleep_interruptibly)落 Stopped;本 PR 走独立的 ForegroundStopWindow 停止请求通道。GNU 依据两者都能自洽(jobs.c:4949-4955 交互 shell 对自身 SIGTSTP SIG_IGN;jobs.c:4077 WUNTRACED 仅 job_control && subshell_environment == 0;jobs.c:4330-4348 JSTOPPED + breaking = loop_level),但一个仓里保留两套并行投递面会让 niubash 的宿主面接线二选一产生分叉,故只取 fix(windows): foreground Ctrl+Z stop surface — inject_console_ctrl_z + Windows Stopped arms (Refs #3467) #3746。
  2. 载体决策:fix(windows): foreground Ctrl+Z stop surface — inject_console_ctrl_z + Windows Stopped arms (Refs #3467) #3746 采用「停止时物化真实 sleeper 子进程」(lazy spawn,只在 ^Z 时发生),避开了本 PR 依赖的提案 A(sleep 仿真常驻让位)带来的跨仓回归——feat(jobs): Windows virtual-sleep Stopped carrier — proposal A stand-down (Refs #3467) #3567 自述该让位会让 niubash 三条 ctrl_c fixture(占位 sleep.exe)变红(20/20 → 17/20)。fix(windows): foreground Ctrl+Z stop surface — inject_console_ctrl_z + Windows Stopped arms (Refs #3467) #3746 的 pin 3/3 在 Windows 本机复跑全绿,没有这个副作用。
  3. 本 PR 的增量确实存在(作为后续 backlog 记录,不因此保留本 PR):fg 等待期再次 ^Z 重停、以及停止时挂起作业内其它存活成员。fix(windows): foreground Ctrl+Z stop surface — inject_console_ctrl_z + Windows Stopped arms (Refs #3467) #3746 的 Known gaps 明确列了这一条(wait_for_background_pid 无停止臂);若要补,应在已合入的 fix(windows): foreground Ctrl+Z stop surface — inject_console_ctrl_z + Windows Stopped arms (Refs #3467) #3746 面上加臂,而不是复活本 PR 的第二套通道。

本 PR 未关闭、未合并;#3467 保持 OPEN。建议后续以「fg 期再停 + 多成员挂起」为题单独开 PR,直接扩展 wait_for_background_pid 的非 unix 停止臂。

caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
…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).
caomengxuan666 pushed a commit that referenced this pull request Nov 19, 2026
…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)。
@caomengxuan666

Copy link
Copy Markdown
Member Author

审计复核(2026-11-19):确认维持 2026-11-17 P1 车道裁决——本 PR superseded by #3746。

复核依据:#3746 已 squash 合入(c8109c58,merge-base --is-ancestor 对 origin/master 校验 ANCESTOR-OK),是 #3467 引擎面(inject_console_ctrl_z + Windows Stopped 臂)的规范实现;本 PR 是同一引擎面的另一设计(停请求位 + ForegroundStopWindow),与 #3746 不叠加。本 PR 此前另有两处独立否决项未消:队长 2026-11-14 静态审查指出的 note_console_ctrl_z 读窗口/写请求位竞态(非线性化点),以及 2026-11-16 车道核出的正文与 diff 不一致。

建议作者关闭本 PR(关闭 #3538 本身;关联的 #3467 跟踪单保持现状,不在本建议范围内)。若后续需要吸收本 PR 独有的钉格(tests/issue3467_windows_fg_ctrl_z_stop.rs,6/6 在隔离构建下绿),按 2026-11-15 队长注记由队长在 #3746 合并形上逐格复核后另立新钉,不在本 PR 复活。

@caomengxuan666

Copy link
Copy Markdown
Member Author

队长执行既有裁决:关闭——superseded by #3746。

#3467 本体保持 open,等 niubash #877 宿主侧接线。

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