Skip to content

WIP: fix(kill): %jobspec delivers via killpg to the job's pgrp (#2624) - #2625

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/2624-kill-jobspec-group
Nov 2, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/2624-kill-jobspec-group

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: kill %jobspec half-kills pipelines — it signals only pids.last() instead of the job's process group.

Refs #2624

GNU law

  • builtins/kill.def jobspec arm: pid = IS_JOBCONTROL (job) ? j->pgrp : j->pipe->pid, then kill_pid (pid, sig, 1).
  • jobs.c:3961-4020 kill_pid group arm: own-pgrp job → killpg (jobs[job]->pgrp, sig); stopped job + TERM/HUP → follow-up killpg (…, SIGCONT); stopped job + SIGCONT → set_job_running (bg emulation). Shared-group job (pgrp == shell_pgrp) → per-member pipeline walk (already implemented in rubash, kill builtin: signaling a dead-but-unwaited child/job reports ESRCH where GNU succeeds #2381).
  • Plain pid operands: kill_pid (pid, sig, pid < -1) — single-process delivery, unchanged.

Plan

Cluster delivery: code + pins only, captain verifies.

@caomengxuan666

Copy link
Copy Markdown
Member Author

Cluster delivery complete (code + pins, captain verifies):

  • d5dfb48d cherry-picks WIP: fix(jobs): fg/bg resume via killpg to the job's pgrp (#2622) #2623's primitive commit c07a82d9 verbatim (master lacks kill::send_signal_to_process_group while WIP: fix(jobs): fg/bg resume via killpg to the job's pgrp (#2622) #2623 is open; whichever lands second rebases it away).
  • 50040970 fix: jobspec operands resolving to a job-control job take a killpg arm in execute_tracked_background_kill — group delivery via the shared primitive, stopped-job TERM/HUP → SIGCONT follow-up (jobs.c:4009-4010), per-member note_kill_delivery bookkeeping; kill -0 probes the group. Shared-group jobs keep the pipeline walk; plain pid operands unchanged.
  • e53d2050 pins: tests/issue2624_kill_jobspec_group_delivery_regressions.rs — TERM kills all pipeline stages, STOP/CONT group faces (incl. the unrecorded-child shape), plain-pid boundary guard. UNVERIFIED-BY-CAPTAIN (not run, per pace rules).

cargo check --lib --tests clean on host. Full decision table, face→code map, and surveyed non-changes (J_NOTIFIED parity note) in the cluster REPORT.md.

@caomengxuan666

Copy link
Copy Markdown
Member Author

队长验收裁定(2026-11-02 晚):1 绿 3 红——其中 1 红是真存量缺陷(已另立案 #2626),2 红待复现定性

亲验环境:scrubbed_cargo、构建槽内、逐 stem 在场核对(并行 + --test-threads=1 串行各跑一遍,结论一致)。

绿:term_jobspec_kills_every_pipeline_stage ✅;守卫 issue2381 7/7、issue2398 2/2 ✅;regression 222p/6f = 基线 ✅。

红 1 — plain_pid_kill_hits_exactly_one_process:真缺陷,但不是本 PR 的账。
队长用 script(1) PTY + /proc 三方对照(同 harness、同序列):

构建 sleep 51 | sleep 52 & 后 kill -TERM $!
GNU bash 5.3.20 sleep 51 存活(S,1.5s 后仍在)
rubash master 77ba76e0 sleep 51 在 0.5s 内消失
本 PR e53d2050 同 master

即流水线一段被杀后其余段被连带终止,是 master 存量缺陷(本 PR diff 不触流水线监督路径)。已按立案卫生另开 #2626(P1,挂 1.5.0),本钉即其回归钉(期望不用改)。

红 2/3 — stop_cont_jobspec_reach_unrecorded_child / stop_cont_jobspec_stop_whole_pipeline:kill -STOP %1 的 wait_until 在 cargo test 内超时(两轮同签名)。但队长用同款 script(1) harness 在测试外逐字复刻钉的节奏(800ms 引导、sleep 45|sleep 45、kill -STOP %1 立即轮询 /proc),结果两段瞬间双双 T**;sh -c 子进程形同样 STOP/CONT 全通(leader+child 双 T、双 S)。产品行为在测试外是对的——红源在 cargo test 环境与钉/会话的交互,定性未明,属修复轮第一题:先在 cargo test 内复现(单钉独跑 vs 同二进制串行),找到触发差(候选:同二进制前序会话残留、pgrep 系统级计数的跨测试污染、引导 800ms 固定睡眠与提示符锚定的差别——#2398 钉用提示符锚定且同轮全绿),再决定修钉还是修产品;不许掰钉(期望全是 GNU 真值)。

处置:本 PR 不合,修复轮同批处理三红(#2626 的修复可落本分支或独立 PR,钉不重复);修好后队长复验全 stem + 守卫再走 CI。

…Refs #2624)

GNU kill.def's jobspec arm resolves the spec to the job's pgrp for
a job-control job (`pid = IS_JOBCONTROL (job) ? j->pgrp :
j->pipe->pid`, kill.def:241) and kill_pid's group arm delivers with
a single killpg (jobs.c:4008), with a SIGCONT follow-up when a
stopped job takes SIGTERM/SIGHUP (jobs.c:4009-4010) and
set_job_running when it takes SIGCONT (jobs.c:4013-4018).

rubash resolved the spec through the shared path to
entry.pids.last() and signaled that one pid, so `kill -TERM %1` on
a pipeline killed only the last stage and left the rest running;
`kill -STOP %1`/`-CONT %1` likewise missed every other member and
every child a member forked after spawn.

execute_tracked_background_kill now takes a killpg arm for a
jobspec that resolves to a job-control job (entry.job_control —
the J_JOBCONTROL record — with a recorded pgrp): delivery via
kill::send_signal_to_process_group (the primitive from #2623,
cherry-picked onto this branch), the stopped-job SIGCONT follow-up
for TERM/HUP, and note_kill_delivery per recorded member — the
same deferred-mark bookkeeping as the shared-group walk arm.
kill -0 probes the group and takes the killpg result as the
verdict. Shared-group jobs keep the per-member walk (jobs.c
pgrp == shell_pgrp arm); plain pid operands keep single-process
delivery (kill.def:196 kill_pid (pid, sig, pid < -1)).
PTY pins (script(1) harness shape from the #2622 pins), kernel-state
assertions via /proc / ps on the exact `sleep N` cmdlines:

- kill -TERM %1 kills all three stages of a background pipeline
  (wait reports 143); pre-fix two stages kept sleeping.
- kill -STOP %1 / -CONT %1 reach the unrecorded child of
  `sh -c 'sleep 47; echo …'` (state T, then S) — the child is in
  the job's pgrp but in no recorded pid list.
- kill -STOP %1 stops BOTH stages of a two-stage pipeline, -CONT
  resumes both.
- A plain-pid kill (kill -TERM $!) still hits exactly one
  process: the sibling stage survives running — the pid/jobspec
  boundary does not move.

UNVERIFIED-BY-CAPTAIN: not run by the delivering cluster.
…own job (Refs #2624)

Diagnosis (captain adjudication on PR #2625: STOP pins timed out
under cargo test but passed instantly under a standalone script(1)
replication): the defect was in this harness, not the product.

* sleep_pids/job counting matched `^sleep N$` SYSTEM-WIDE. Foreign
  processes with the same cmdline — the captain's CI-watcher poll
  loops spawn a bare `sleep 45` pair every cycle — entered the
  counts nondeterministically (a probe latched onto the watcher
  pair's pgrp in 2 of 6 rounds), and once a pin panicked, its
  session LEAKED (PtySession had no Drop): a stopped `sleep 99`
  never expires and poisoned every later run in the binary.
* The child pin's final assert (`status.code() == Some(0)` after
  bare `exit`) was not GNU-true: bare `exit` propagates
  `wait %1`'s 128+SIGKILL. Measured against GNU bash 5.3.20 with
  the identical command sequence: script rc 137; rubash: 137.
  The product agreed with GNU; the expectation did not.

Fix, with every GNU expectation from the original pins preserved:
counts are scoped to the pin's own job (pipeline stages by
pgrp == job leader parsed from the spawn notice, the unrecorded
child by ppid == job leader), `finish()` ends the script with an
explicit `exit 0`, and PtySession kills+waits its child on Drop
so a panicked pin cannot leak its session. Cluster-run after the
repair, product still unfixed: both STOP pins and the TERM pin
pass; the plain-pid pin fails only on the #2626 survivor assert.
…age (Refs #2626)

GNU runs a pipeline's stages as its own direct children: $! IS
the last stage's pid, and jobs.c's waitchld (bash-5.3 jobs.c:4057)
reaps per process — a job completes only when ALL its processes
have completed, and nothing ever signals the siblings. Measured
on the GNU 5.3.20 oracle: `sleep 51 | sleep 52 &; kill -TERM $!`
leaves sleep 51 running.

rubash executes a background body in a wrapper process ($! names
the wrapper) whose pipeline stages are armed with
PR_SET_PDEATHSIG/SIGKILL (the rubash#737 child-cleanup design),
so ANY wrapper death — including an ordinary catchable SIGTERM
addressed to $! — cascaded into every stage. Master and this
branch both killed sleep 51 within 0.5s (strace: the session's
only kill syscall is the builtin's kill(wrapper, SIGTERM); the
stages died by the kernel's parent-death signal).

Fix by mechanism, keeping the #737 contract (issue866 W1 pin
stays green): while a wrapper process supervises a foreground
pipeline, it must be transparent for individually addressed
catchable signals. At role-arming time — the one single-threaded
moment with entry dispositions untouched — the wrapper installs
a minimal handler (async-signal-safe self-pipe write) for the
relay signal set and saves each signal's entry disposition. The
Unix concurrent pipeline path publishes its LAST stage's pid for
exactly the span of its wait (WrapperRelayTargetGuard, inert in
every non-wrapper process). A relay thread then, per delivery:
forwards to the published last stage when the relay still owns
the disposition; restores the saved entry disposition and
re-raises when no pipeline is in flight (die-by-signal,
SIG_IGN-swallow, and entry-handler semantics reproduced); and
drops the copy when the disposition has moved to the engine's
handler, which records deliveries itself and chains to ours —
re-raising there looped one delivery into ~200 trap firings
(measured). Trap priority is preserved: a body that traps a
relayed signal gets the engine handler at its next reconcile,
and the relay steps aside. SIGKILL/SIGSTOP/SIGCONT are
uncatchable or kernel-handled and stay out of the set.

Residuals, deliberately not papered over: a wrapper that dies
by an UNcatchable signal (SIGKILL, crash) while supervising a
pipeline still cascades via PDEATHSIG where GNU would orphan
the surviving stages — flipping that is the #737/W1 contract
decision, not this fix. Sequential (shell-stage) pipelines are
not relay-covered; no last-stage pid exists to publish mid-list.

Cluster-run: issue2624 pins 4/4 (plain_pid pin is the #2626
regression pin), issue2381 7/7, issue2398 2/2, issue866 4/4,
lib kill tests 22/22; non-interactive repro now matches the GNU
matrix exactly (last stage dead, sibling alive, jobs shows the
job still Running).
@caomengxuan666
caomengxuan666 force-pushed the fix/2624-kill-jobspec-group branch from e53d205 to bafc83a Compare November 2, 2026 14:41
@caomengxuan666

Copy link
Copy Markdown
Member Author

W24 repair round — all three reds cleared

Branch rebased onto master a89e560 (#2623 merged 2026-11-02T14:23Z); the cherry-picked #2622 primitive dropped out of the rebase as previously-applied. New head bafc83a (force-with-lease, fresh remote sha). Full evidence: repair-REPORT.md in the wave dir. All tallies cluster-run.

(1) STOP-pin timeouts — harness defect, product innocent

  • The pins counted ^sleep N$ system-wide; the captain's CI-watcher poll loops spawn a bare sleep 45 pair every cycle and entered the counts nondeterministically (probe latched onto the watcher pgrp in 2/6 rounds).
  • PtySession had no Drop: a panicked pin leaked its session (a STOPPED sleep never expires), poisoning later runs in the binary — the parallel→serial same-signature mechanism.
  • The child pin's exit-code assert was not GNU-true: bare exit propagates wait %1's 128+SIGKILL. Identical sequence measured on both binaries: GNU 5.3.20 → 137, rubash → 137.
  • Fix (1235e7b): counts scoped to the pin's own job (stages by pgrp == leader from the spawn notice; child by ppid == leader), explicit exit 0, Drop kills+waits the session. No GNU expectation weakened. With the product still unfixed, the plain-pid pin then failed only on the 后台流水线单段被杀后其余段被连带终止(GNU 其余段继续运行) #2626 survivor assert.

(2) #2626 — fixed on this branch (bafc83a), PDEATHSIG kept

Mechanism: $! names the background wrapper process; its pipeline stages are PDEATHSIG/SIGKILL-armed (#737 design), so any wrapper death — including a catchable TERM addressed to $! — cascaded into every stage (strace: the session's only kill syscall is kill(wrapper, TERM)). GNU runs stages as direct children; jobs.c waitchld (bash-5.3 jobs.c:4057) reaps per process and never signals siblings.

Fix: while supervising a foreground pipeline, the wrapper is transparent for individually addressed catchable signals — entry dispositions saved + a self-pipe relay handler installed at role-arming time; the Unix concurrent pipeline path publishes its last stage pid for the span of its wait (WrapperRelayTargetGuard, inert outside wrappers); the relay forwards to that stage, restores+re-raises the entry disposition when no pipeline is in flight, and drops copies when the engine handler owns the signal (it records and chains to ours — re-raising looped one delivery into ~200 trap firings, caught and fixed mid-round). Trap priority verified (trapping body: trap fires exactly once, as GNU).

Residuals flagged for captain ruling: (R1) wrapper death by an uncatchable signal/crash still cascades via PDEATHSIG where GNU orphans survivors — measured GNU kill -KILL $! on a pipeline leaves the sibling alive; the issue866 W1 pin contradicts the GNU oracle (its exact script prints ORPHAN-SURVIVED under GNU 5.3.20), so flipping the #737 contract is a captain call, not made here. (R2) sequential shell-stage pipelines inside wrappers are not relay-covered (common sub-shapes still measure GNU-true via the no-target path).

(3) Verification (cluster-run, final head)

issue2624 4/4 · issue2622 2/2 · issue2381 7/7 · issue2398 2/2 · issue866 4/4 · lib kill tests 22/22 · regression 222p/6f — exact baseline set. Two earlier regression runs overlapped each other and were discarded as polluted (186p/42f, 143p/85f); the cited run was solo and scrubbed. Non-interactive #2626 repro now matches the GNU matrix exactly (last stage dead, sibling alive, jobs shows Running).

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 14:55
libc::pipe2 is glibc/musl-only — the wrapper relay's self-pipe broke
the darwin CI jobs (E0425). Mirror fd/unix.rs create_capture_pipe:
pipe2(O_CLOEXEC) on Linux, pipe + FD_CLOEXEC fcntl elsewhere.
Captain fix on the W24 repair branch; host cargo check --lib clean,
darwin verification is CI's macOS runner (no macOS SDK cc locally).
@caomengxuan666
caomengxuan666 merged commit c89db76 into master Nov 2, 2026
27 of 29 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2624-kill-jobspec-group branch November 3, 2026 09:08
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