Skip to content

fix(fg-wait): interactive shell survives SIGINT around foreground waits (rubash#1250) - #1414

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/1250-fg-sigint
Oct 23, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/1250-fg-sigint

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

P0: niu -i died from the very SIGINT meant for the foreground child (GNU survives, rc=130).

Root causes (two wait shapes, one predicate):

  • The issue repro (sleep 30) lands in the sleep BUILTIN fast path, not wait_external_child (sleep is a rubash builtin). Untrapped SIGINT at SIG_DFL killed the shell mid-slice.
  • External fg wait had the same hole (blocking waitpid at SIG_DFL).
  • Tried pthread_sigmask BLOCK first for the builtin: reverted. The process is multithreaded, so a per-thread mask cannot stop delivery to the DFL main thread (verified by probe: guard armed, shell still died as Z).

Fix (GNU jobs.c:3086-3102 wait_for / trap.c model):

  • Shared predicate foreground_sigint_needs_protection (reuses the interactive-discard rule so the two cannot drift): interactive + SIGINT at SIG_DFL (trapped keeps its handler path, entry-ignored stays ignored, scripts keep DFL death).
  • External wait: SIG_IGN guard across blocking waitpid (child already spawned, keeps DFL and dies; wait reports 128+sig).
  • Builtin sleep: route INT to the mailbox handler and HOLD it (kill::hold/release_fg_sigint; reconcile skips the Default flip while held), consume in the slice loop -> Ok(Some(SIGINT)) -> 128+sig, reader continues. SIG_IGN would be wrong here (sleep would complete rc=0, GNU reaps a dead child -> 130). A trapped INT is never stolen (armed-gated take; trap action runs).
  • kill.rs reconcile gains one AtomicBool check on the INT-Default path only; steady-state path untouched.

Evidence (Termux aarch64, setsid+pipe harness, reap-based aliveness, no pgrep):

  • builtin sleep, pgroup SIGINT: STOCK Z (dies) / FIXED S + DONE rc=130 at 0.5s / GNU S + fresh prompt (GNU discards the line remainder, see below).
  • builtin sleep, shell-directed SIGINT: FIXED S + rc=130 (GNU: S + rc=0, sleep completes - micro-divergence, builtin-vs-external architecture, documented).
  • external (/usr/bin/sleep 20), pgroup: FIXED S + rc=130.
  • trapped INT: TRAP_FIRED runs, rc=130, alive.
  • scripts (non-interactive): unchanged DFL death (STOCK and FIXED both SIGNALED; GNU script pgroup also dies).
  • cargo fmt clean; lib 714/0; trap/signal integration: only the 2 pre-existing env failures (identical on pristine tree).

Known remaining (follow-ups, NOT this PR):

  • line-discard parity: GNU discards the remainder of the command line after a SIGINT-aborted fg wait (fresh prompt, foo in 'sleep 30; echo foo' never runs); rubash continues the ';' list with rc=130. Same shape for builtin and external; needs its own unwinding design.
  • read -t still dies (separate blocking surface, leaf-reader plumbing; issue notes it as sibling).
  • script-directed-only SIGINT (kill -INT , child unaffected): GNU script survives, rubash script dies (DFL). Exotic; same record-and-reraise design would cover it.

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