Skip to content

#2398 foreground job stop detection (WUNTRACED wait + tcsetpgrp handoff) - #2406

Merged
caomengxuan666 merged 6 commits into
masterfrom
fix/2398-fg-stop-detect
Oct 31, 2026
Merged

caomengxuan666 merged 6 commits into
masterfrom
fix/2398-fg-stop-detect

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2398

Job control: a foreground child's SIGTSTP stop is invisible to the shell — the interactive foreground wait has no WUNTRACED and there is no tcsetpgrp handoff, so ^Z stops the shell's whole process group (shell shares the child's pgrp) and the session freezes; jobs/fg report "no job control".

GNU contract (bash 5.3 sources, verified against the local 5.3.20 oracle in a pty):

  • jobs.c:4077 waitchld — interactive foreground wait uses WUNTRACED|WCONTINUED under job control
  • jobs.c:4999 give_terminal_to — sigprocmask-guarded tcsetpgrp handoff to the child pgrp on foreground spawn / start_job (jobs.c:3918), reclaimed to shell_pgrp in wait_for (jobs.c:3253)
  • jobs.c:3243 — a stopped foreground job's wait verdict is 128+WSTOPSIG (oracle: RC=148 for SIGTSTP)
  • child side: setpgid (jobs.c:2390/2446) so the child leads its own pgrp

This first commit lands the end-to-end PTY pins only (tests/issue2398_jobstop_regressions.rs): ^Z via the pty VSUSP path, stop notice + RC=148 + jobs listing, fg resume + ^C -> 130, bg resume path. Implementation follows in increments on this branch.

Out of scope: prompt-side ^Z (reedline Undo, settled design); #2396/#2397 exit-time checkjobs (sibling cluster).

Spec-first pins for the Ctrl+Z execution face. GNU contract (bash 5.3
jobs.c): waitchld waits with WUNTRACED under job control (jobs.c:4077),
give_terminal_to (jobs.c:4999) hands the tty to the child pgrp, and a
stopped job becomes current with $? = 128+WSTOPSIG (jobs.c:3243).
Verified against the local 5.3.20 oracle in a pty: sleep 30 + ^Z ->
'[1]+  Stopped  sleep 30', RC=148, fg resumes, ^C -> RC=130.

Refs #2398
The stop-detection channel for interactive job control, per GNU
bash 5.3 jobs.c:

- Gate (job_control_wait::job_control_channel_active): interactive
  + monitor + top level (jobs.c:4077 subshell_environment == 0) +
  controlling tty present. Spawn, wait, and fg share the predicate.
- Spawn: job-control foreground children lead their own pgrp
  (pre_exec setpgid, jobs.c:2390/2446; parent repeats it after the
  fork, jobs.c:2455) — previously the child shared the shell's
  group, so ^Z stopped the shell too.
- Wait (wait_fg_child_job_control): the job is registered before
  the wait, the terminal is handed to its pgrp (give_terminal_to,
  jobs.c:4999), and a sliced waitpid(WUNTRACED|WNOHANG) observes
  the stop: job marked Stopped + current (waitchld bookkeeping),
  flipped to a background-listed job, handle parked in
  background_children, boundary stop notice queued, wait verdict
  128+stopsig (jobs.c:3243). Terminal reclaimed on every exit path
  (jobs.c:3253). Exit path deletes the job, as GNU does for a
  completed foreground job.
- fg (execute_fg_jobs): give_terminal_to(job pgrp) before SIGCONT
  (jobs.c:3918/3926), WUNTRACED wait (wait_for_fg_job_control) so a
  re-stop is an outcome, reclaim after (jobs.c:3253).
- bg/wait need no changes: the stopped job sits in the table +
  background_children in the shape their existing paths consume.
- Emulated sleep stands down to a real spawn while the channel is
  live — GNU's sleep is a job-controlled child; the in-process
  emulation has no pgrp to stop or resume.
- Stop notice renders at the reader boundary inside
  report_pending_signal_notices, jobs short format, ahead of death
  notices.

All unix machinery is cfg-gated; the Windows target keeps the
existing paths. Not yet verified (check + PTY pins pending).

Refs #2398
@caomengxuan666
caomengxuan666 force-pushed the fix/2398-fg-stop-detect branch from 36b1c28 to 0bb54d0 Compare October 31, 2026 18:27
…the wait

Two field corrections from the first PTY runs:

- The stdio plan arms an ExternalStdioOutcome for EVERY planned
  spawn (LiveHandles for the plain shape), so an is_none() gate
  never fired. The channel now claims every non-CapturePipe spawn,
  and the WUNTRACED wait is dispatched in the LiveHandles branch as
  well as the no-outcome plain branch. CapturePipe shapes keep the
  existing waits.
- The stop notice cannot ride the command-boundary flush
  (report_pending_signal_notices runs when the NEXT command is
  walked — a full prompt after GNU's position). GNU reports the
  foreground job's stop as wait_for completes (jobs.c:3385
  notify_and_cleanup), so the foreground wait now prints it
  synchronously via format_stopped_job_notice, and fg's re-stop
  notice rides fg's own stderr Vec (after the command text it
  printed, in wait_for's order). The boundary queue is gone.

Refs #2398
…orrow

fg's command text flushes when the builtin returns (the repo's
builtin-output plumbing — verified identical on master with a
background job), so the pin now asserts the resume by natural
completion (FGRC=0, text in transcript) on a short job, then runs
the ^C-kills-the-resumed-job cycle (RC2=130) as a second phase.
Also fix the finish()/text() use-after-move the first build hit.

Refs #2398
@caomengxuan666 caomengxuan666 changed the title WIP: #2398 foreground job stop detection (WUNTRACED wait + tcsetpgrp handoff) #2398 foreground job stop detection (WUNTRACED wait + tcsetpgrp handoff) Oct 31, 2026
@caomengxuan666

Copy link
Copy Markdown
Member Author

Status from the fix cluster (head 58b8746, rebased on master 7f682e0):

Scope — only the fg-recovery face of #2398, per coordination: stop detection in the interactive foreground wait + job Stopped bookkeeping + terminal hand-back, so fg/bg/jobs see and resume the job. No prompt-side / host-side Ctrl+Z work (landed elsewhere).

Mechanism (GNU bash 5.3 jobs.c citations in the commits):

  • Gate job_control_channel_active: interactive + monitor + top level (jobs.c:4077 subshell_environment == 0) + controlling tty; shared by spawn, wait, fg, and the sleep stand-down so they cannot drift.
  • Spawn: job-control fg children lead their own pgrp (pre_exec setpgid jobs.c:2390/2446; parent repeats post-fork jobs.c:2455); every non-CapturePipe stdio outcome is claimed (the plan arms LiveHandles for plain spawns — an is_none() gate never fired).
  • Wait wait_fg_child_job_control: job registered pre-wait, tty handed to the child pgrp (give_terminal_to jobs.c:4999), sliced waitpid(WUNTRACED|WNOHANG) with the [bug][P1] Ctrl+C 报 130、回提示符,但前台子进程没死(Android 实机,#1662 后续) #1990 SIGINT-forward discipline; stop → mark Stopped+current, flip to background-listed, handle parked in background_children, stop notice printed synchronously (jobs.c:3385 position — the boundary flush runs a prompt late), verdict 128+stopsig (jobs.c:3243); exit → job deleted, tty reclaimed on all paths (jobs.c:3253).
  • fg: give_terminal_to(job pgrp) before SIGCONT (jobs.c:3918/3926), WUNTRACED wait wait_for_fg_job_control (re-stop is an outcome; shell-directed signals adjudicated via the pending drain), reclaim after; re-stop notice rides fg's stderr after its command text. bg/wait needed no changes.
  • Emulated sleep stands down to a real spawn while the channel is live (GNU's sleep is a job-controlled child; the emulation has no pgrp).

Local verification so far (compile rationing; full batch is the collector's):

  • cargo check clean on the final tree.
  • PTY pins: bg-resume pin green locally; the fg pin's stop/notice/RC=148/jobs segments verified in transcript, its fg assertion was restructured after isolating a pre-existing repo trait (builtin output — fg's command text included — flushes when the builtin returns; identical on master) — full pin re-run deferred to the CI/collector batch per tonight's fire-at-will directive.
  • Behavior additionally verified by hand-driven pty sessions against the built binary: ^Z → child T-stopped in its own pgrp, shell prompt back with tty reclaimed, [1]+ Stopped + RC=148 + jobs listing, fg resumes to FGRC=0, fg+^C → 130; byte-compared against the local bash 5.3.20 oracle transcript.

Residuals (named, not filed): foreground PIPELINES as stoppable jobs (stage waits live in the pipeline spawner); fd-copy-capture foreground face; subshell-depth>0 foreground face (flat-model two-table shape); background jobs stopped by an external killer are still learned only via the kill-path deferred marks + boundary refresh (no background WUNTRACED poller); other emulated externals besides sleep under job control; $- not showing m for interactive shells (pre-existing, monitor itself is on).

Stays draft; do not merge from here.

@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 31, 2026 21:22
@caomengxuan666
caomengxuan666 merged commit aa80b9a into master Oct 31, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2398-fg-stop-detect branch November 2, 2026 01:09
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