Repository navigation
WIP: bats CTRL-C family — job-control process groups + group-delivered SIGINT - #2251
Merged
Merged
Conversation
Refs #2241. GNU groups every job under job control: make_child (jobs.c:2390) runs setpgid(mypid, pipeline_pgrp) in the child between fork and exec, and the parent repeats the call after fork (jobs.c:2455) so the group exists whichever side runs first. A single-process job's recorded pgrp is its own pid. rubash recorded exactly that pgrp in the job table (what jobs -x prints) but never created the group, so the group did not exist in the kernel: a builtin `kill -- -$!` answered ESRCH and the job survived — the delivery bats' CTRL-C tests are built on could not even be addressed. Wire the invariant through every async spawn site: - simple external spawn (compound_exec): the monitor pre_exec arm becomes reset+setpgid composed (child_pre_exec_reset_grouped); the parent repeats setpgid(pid, pid) after spawn, gated on the same job_control value that feeds async_job_pgrp. - async compound-body respawn (fd::spawn_whitelisted): new new_process_group field on WhitelistedSpawn carries the spawning shell's monitor state; the unix backend setpgids in pre_exec and again parent-side after spawn. The Windows backend accepts the field for parity and ignores it (no process groups there). - coproc spawn: monitor-gated pre_exec setpgid plus the parent-side repeat, matching the pgrp its job registration already claimed. Helpers live beside the existing pre_exec reset hooks in builtins/kill.rs (child_pre_exec_setpgid / child_pre_exec_reset_grouped / group_spawned_child). Without job control nothing changes: children keep sharing the shell's group, as GNU leaves them.
Refs #2241. bats CTRL-C face 2: the delivery itself. Once the target group exists, bats' driver delivers SIGINT by kill(2) addressed to the whole process group. GNU's real foreground sleep dies from that delivery, jobs.c:3064 wait_for returns immediately, and the pending INT trap dispatches at once (trap.c:545). rubash's in-process sleep emulation has no child to die: the #1034 model ends the virtual sleep early only for kernel-caused (terminal) deliveries and defers every kill(2) delivery to the sleep's full remainder, so the trap sat queued for the fixture's whole 10s sleep and the outer driver's 1s liveness check failed the run. siginfo cannot distinguish a pid-addressed kill(2) from a group-addressed one, but the sender's process-group membership can: - a sender OUTSIDE the shell's process group addressing the group is the only way such a delivery both reaches the shell and would have killed GNU's real child (which shares the shell's group exactly when job control is off); - a sender INSIDE the group is one of the shell's own jobs (kill -INT $$, the #1034 shape): the addressing was the shell alone, the child sleeps on, deferral stands; - an exited sender (getpgid fails) keeps the deferral too: the #1034 sender exits right after sending, while a group-kill sender (bats' driver) stays alive waiting on the group. The kernel backend's drain now records the sender pid of the latest kill(2) delivery per signal (LAST_SENDER_PID, beside the KERNEL_CAUSED origin bit; kernel-caused arrivals clear it, note_self_delivery records the shell itself), and sleep_interruptibly's virtual-child adjudication gains the foreign-group branch via delivery_would_kill_virtual_child, gated on monitor being off in the waiting shell. The accepted residue — an outside sender pid-addressing the shell alone is indistinguishable from the group delivery — is documented at the model; deferring there was the failure this issue reports.
Refs #2241. tests/issue2241_job_group_signal_regressions.rs: - monitor-mode background job (simple external, compound respawn, coproc) leads its own process group; builtin group kill reaches it; wait reports 128+SIGTERM — GNU jobs.c:2390/2455 semantics. - control: without monitor the job shares the shell's group and no group is named by its pid (holds before and after the fix). - end-to-end: a script shell with an INT trap blocked in a foreground sleep, SIGINT addressed to its whole process group by a living driver shell outside the group (the bats-exec-file posture), runs the trap promptly and exits 130 instead of deferring for the sleep's full remainder. Baseline at 1d5d62d: the three grouping pins and the group-delivery pin fail (ESRCH/shared group; still alive 8s after delivery), the control pin passes — same shapes as the P5/P6/P14 isolation probes in the cluster report. Post-fix all five pass, and the rubash#1034 deferral pins stay green (in-group sender).
caomengxuan666
marked this pull request as ready for review
October 30, 2026 18:15
caomengxuan666
added a commit
that referenced
this pull request
Oct 30, 2026
Conflict resolution (compound_exec.rs, one hunk): the branch's Gap-2 pre_exec setpgid at the direct async spawn site is dropped — master's rubash#2241 wiring (1909f37, PR #2251) already composes reset + setpgid there (child_pre_exec_reset_grouped) and adds the parent-side group_spawned_child the branch lacked. An explanatory comment marks the spot; the composed diff vs master for this file is that comment only. All other #2233 changes (EOF-arm hangup classification, canonical SIGHUP trap key, settled poll, finish_shell mark re-take) merge unchanged. Refs #2233.
This was referenced Oct 30, 2026
SpookyWaste
pushed a commit
to SpookyWaste/rubash
that referenced
this pull request
Nov 12, 2026
…ixwin#2287) Investigation result: the unixwin#2287 claim does not reproduce on master. The issue's grep premise is literally true (pipeline_exec.rs has no setpgid), but its behavioral conclusion does not follow in rubash's architecture: a background pipeline's job is the grouped background wrapper wired by unixwin#2251 (spawn_whitelisted new_process_group under monitor mode), and every pipeline stage is a child of that wrapper inheriting its group. The recorded pgrp (wrapper pid, `jobs -p`) is a real, populated kernel group, so hangup_all_jobs' killpg and `kill -- -<pgrp>` deliver to every stage. Probed on a master-equivalent build against GNU bash 5.3.20, three independent paths, same outcomes both shells: - login route (pty, --login -i): set -m + shopt -s huponexit, `sleep N | cat &`, exit -> wrapper and both stages die; without huponexit all survive (control). - script route: set -m, `kill -HUP -- -$(jobs -p)` -> killpg rc=0, both stages die. - /proc membership: stages' pgid == recorded pgrp == wrapper pid. GNU spec (for the record): jobs.c:2265 make_child — child-side setpgid(mypid, pipeline_pgrp) at jobs.c:2390, pipeline_pgrp set to the first child's pid at jobs.c:2372, parent repeats setpgid(pid, pipeline_pgrp) parent-side (~jobs.c:2455, errors ignored), job pgrp recorded at jobs.c:649; jobs.c:1783 hangup_all_jobs killpg's jobs[i]->pgrp; shell.c:1017 is the huponexit exit path. GNU reaches the observable contract by grouping the shell's direct children; rubash reaches it through the wrapper group. No product code changes: grouping pipeline_exec stage spawns per the GNU in-shell model would move stages OUT of the group that reaches them today — foreground stages would leave the tty's foreground group (rubash performs no tcsetpgrp handoff, so SIGINT/SIGTSTP/SIGTTIN delivery would break), and background stages would leave the wrapper's group so hangup_all_jobs' killpg(wrapper pgrp) would miss them, creating the alleged failure. These pins lock the invariant at the /proc level (membership, killpg delivery by the recorded pgrp, and a no-monitor control asserting nothing is grouped without job control) so a future refactor that detaches stages from the job's group fails here. Refs unixwin#2287
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #2241 — bats CTRL-C / signal family (7 tests in bats.bats). WIP: two of the family's three mechanisms are fixed on this branch; the third is characterized below and is not fixed here (it lives in a file outside this cluster's mandate). The bats-level family therefore stays red until that third fix lands — see "End state".
Characterization — three independent mechanisms
(a) Trap-context face (
bats_debug_trap: command not found): REPRODUCED at master; root cause is a reader completeness mis-scan, not trap visibility. The plain trap mechanism (handlers defined in a sourced library, traps installed inside a function, redirected groups,set -ETinheritance) is byte-identical to GNU in every probe. The real trigger is narrower: a line-spanning array assignment whose opener shares a line with the enclosing compound construct's opener —GNU:
A=1 2, rc 0. rubash (master and this branch):line 1: unexpected EOF while looking for matching ')', rc 1 — on every reader path (script file,-c, stdin,.source,-n,eval). Same-line openers withif…thenbodies mis-scan identically; a subshell opener yields a line-4 syntax error instead; a bare brace group, a top-level array, and an opener alone on its line are all fine. In the bats geometry this is exactly the shape of the respawned child's session reconstruction: the reprinted prelude puts each function body on one line but preserves the newline insidebats_error_trap's multi-line array assignment, whose opener sits on the definition's first line. The reconstruction child dies in ~0.13s with rc 127 (strace-verified), the DEBUG/ERR traps — already re-armed from the__RUBASH_TRAP_*env carriers — fire against not-yet-registered handlers, and the nested bats run never signals its latch. The implicated verdict isunclosed_input_close_char_posixinsrc/lexer/continuation.rs(plus itsscript_driver.rsgatherer mirrors); that file is outside this cluster's change mandate, so the fix is handed off with the minimal repro above rather than folded into this PR. GNU has no corresponding layer: its reader is the parse.y token stream and array lists are consumed byparse_matched_pair(parse.y:3877), where newlines inside(...)are ordinary input.(b) Grouping defect (CONFIRMED, fixed here). rubash never created the job-control process group:
async_job_pgrprecorded the GNU value forjobs -xwhile the source comment admitted "Rubash never calls setpgid". GNU: jobs.c:2390make_childsetpgids the child intopipeline_pgrp; jobs.c:2455 the parent repeats it after fork. Baseline evidence:set -m; sleep 20 &→ job PGID = inherited harness group; builtinkill -TERM -- -$!→ ESRCH, job survives. Every async launch shape shared the defect.(c) Delivery defect (CONFIRMED, fixed here, independent of (b)). Even with the group existing, a group-addressed kill(2) SIGINT during the in-process
sleepemulation deferred the INT trap for the sleep's full remainder: controlled-geometry probe (external driver, group delivery) — GNU nested: trap runs in 0.01s, exit 130; rubash nested: trap at 29.02s, at the emulated sleep's natural end. The #1034 virtual-child model only ended the sleep early for kernel-caused (terminal) deliveries and explicitly punted on group-addressed kill(2) — exactly bats' delivery (kill -s SIGINT -- -$pgidfrom a living bats-exec-file).Fix (two commits, one per mechanism)
job_controlvalue that feedsasync_job_pgrp: simple external spawn, async respawn viafd::spawn_whitelisted(newWhitelistedSpawn::new_process_group; windows backend accepts the field for parity), coproc spawn. Helpers beside the existing pre_exec hooks inbuiltins/kill.rs.LAST_SENDER_PID, besideKERNEL_CAUSED);sleep_interruptibly's virtual-child adjudication also ends the sleep when the sender sits outside the shell's process group and monitor is off (the virtual child shares the shell's group exactly then, so a group-addressed delivery would have killed GNU's real child). An in-group sender (the [bug] Trapped signals deferred while blocked in foreground external command #1034 shape) and an exited sender keep the deferral. The one indistinguishable residue (outside sender pid-addressing the shell alone) is documented at the model.Regression pins
tests/issue2241_job_group_signal_regressions.rs(5 tests): grouping for external/compound/coproc jobs, no-monitor control leg, and the end-to-end group-delivery geometry with a living driver-shell sender (bats-exec-file's posture). Baseline at 1d5d62d: 4 red / control green. Post-fix: 5/5 green, andissue1034_sleep_trap_deferral_regressionsstays 2/2 green.End state (bats level, this branch)
Per-test runs on the unmodified bats tree, fixed debug binary: 0/7 green, as expected while (a) is open. The six
CTRL-C aborts …tests fail on the coordination-latch timeout (the nested run's signaling child is the one killed by the (a) reconstruction defect; thecommand not foundlines surface verbatim in the hang test's and the current-test test's captured output).Don't hang on CTRL-Cprints itsnot ok(latch wait killed at status 143) but the outer process tree does not exit and had to be timeout-killed at 600s. Full ledger, probes and the (a) handoff: the cluster REPORT (fix-2241-bats-signal).GNU citations: jobs.c:2390, jobs.c:2455, jobs.c:3064, trap.c:545, trap.c:744, parse.y:3877 (vendored bash-5.3 tree). No
Fixes/Closes— the issue stays open until the (a) scanner fix lands and the family is re-weighed.