Skip to content

WIP: fix mid-wait untrapped Ctrl+C killing interactive sessions (niubash#698) - #2145

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/698-untrapped-ctrlc-midwait-discard
Oct 29, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/698-untrapped-ctrlc-midwait-discard

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs unixwin/niubash#698 (closure comes after niu-side verification of the merged engine; this PR does not close it)

Defect (confirmed by the issue's native-harness re-verification)

In interactive niu on Windows, an untrapped Ctrl+C during a foreground command (sleep 10) exited the whole shell with rc=130 in ~101ms — no prompt return. niu's watch_imp::deliver (ctrl_c.rs:395) interrupts the foreground command through the engine mailbox (inject_console_ctrl_c + group broadcast); the delivery chain itself is sound (trapped face GNU-aligned, chord verifiably consumed). The failure is the classification of what the engine's mid-wait drain does with the delivery.

Localization

The engine has three mailbox drains that convert an untrapped delivery into ExecuteError::ExitCode(128+sig):

drain sets the DISCARD mark (signal_discard_pending) for interactive sessions?
boundary dispatch run_pending_signal_traps yes
wait builtin's wait_pending_signal_status (rubash#1825) yes
observe_signals_while_blocked (mid-wait) no — the defect

observe_signals_while_blocked is the only consumer on the paths where no kernel delivery adjudicates first: the Windows foreground external wait, the #2020 pipeline member waits, and the non-unix builtin sleep. Its bare ExitCode(130) reaches run_source_impl, which — finding no discard mark — records an exit jump; Session::run then reports Flow::ExitRequested(130), and niu's REPL (which correctly honors the mark when set, niubash#610 note_exit_code_unwind) ends the process.

Face-split probes (Linux, this tree vs GNU bash 5.3.20 oracle): standalone rubash -i under a PTY — ^C during sleep 10 re-prompts with $?=130, identical to GNU (the unix kernel path uses the foreground-wait SIGINT guard + fg_sigint_unwind, never this drain). Non-interactive -c + group SIGINT: both shells die by SIGINT immediately — the shape this PR preserves byte-identically. Verdict: the defect is the mailbox-delivery classification only; it is niu-visible because niu's console Ctrl+C is an event-shaped mailbox delivery on Windows.

Fix (by mechanism)

GNU: jobs.c:3064 wait_for observes the recorded ^C during the wait; jobs.c:3315 ADDINTERRUPT throws the interrupted wait to the top-level reader as a DISCARD — the job settles 128+SIGINT and the interactive session survives; a non-interactive shell keeps the exit classification.

  1. observe_signals_while_blocked's untrapped branch now makes the same classification as its two sibling drains: record exit_code = 128+sig and, when the session is interactive, set signal_discard_pending. Interactivity is read from the executor's session flag — the host-declared session state both sibling sites read — never from the process environment. Non-interactive sessions set nothing.
  2. The mark's invariant (it classifies the unwind it was set for; it never outlives its unit) is completed at the two places that convert/swallow that unwind: the unix builtin-sleep fold that forgets a shell-only SIGINT consumes the mark with the delivery (otherwise a later genuine exit in the same unit would be misclassified as a discard), and run_source_impl's SigintUnwind arm consumes a mark the same interrupt left behind before the wait converted it to that spelling.

Trapped deliveries (re-queue semantics), the §6.4 stale-discard ruling, and #2020's tree-kill settle behavior are untouched.

Pins — tests/issue698_untrapped_sigint_midwait_discard.rs

  • Windows (the defect's home; CI adjudicates): Session-API faces mirroring the niu composition — interactive session + mailbox inject_signal during (a) builtin sleep 30 and (b) external ping -n 30: run settles fast at 130 with Flow::Completed and the session continues; (c) non-interactive guard: same injection keeps Flow::ExitRequested(130).
  • Unix: PTY interactive foreground sleep ^C guard (NEXT_130, session alive); non-interactive group-SIGINT dies-by-signal guard; the mid-sleep fold bookkeeping pair (a forgotten queued SIGINT leaves no mark — a following exit 3 still exits; and completes normally at 0).

Local verification: unix pins 4/4 green; the settlement unit was additionally proven with a temporary in-crate probe (removed before commit): baseline fails exactly at "discard mark must be set" for the interactive arm while producing the same ExitCode(130); the fold pin goes red if the fold's mark consumption is reverted. Regression battery green: lib 965/1 (the 1 is the known perennial ownership_unary_operators_check_existing_file), #1639/#1662/#1825/#1990-1991/session-api-interrupt/signal-trap suites all pass; #2020's file is windows-gated (compiles, 0 local tests).

niu-side assessment

No niubash change is needed. niu already consumes this exact classification on every route: Shell::note_exit_code_unwind (niubash#610) checks take_signal_discard_pending() for each ExecuteError::ExitCode unwind out of execute_ast, and repl.rs ends the session only when take_session_exit_jump() was armed — a discard never arms it. The Session route classifies through the same run_source_impl. With the mark now set on the mid-wait path, niu's REPL will simply regain control with $?=130. The deliver path itself (#669) needs no change. (A companion cluster is editing niubash-runtime shell.rs startup regions; this fix touches no niu code and no startup region.)

…RD in interactive sessions (niubash#698)

observe_signals_while_blocked — the mailbox drain every blocked
wait polls on Windows (wait_external_child, the #2020 pipeline
member waits, the non-unix builtin sleep) — converted an untrapped
delivery to a bare ExitCode(128+sig) without the discard
classification its two sibling drains attach (the boundary
dispatch in run_pending_signal_traps; the wait builtin's drain,
rubash#1825). The source driver therefore recorded an exit jump,
Session::run reported Flow::ExitRequested(130), and niu's REPL
ended the whole process at rc=130 on an untrapped Ctrl+C during a
foreground command — where GNU jobs.c:3064 wait_for observes the
recorded ^C and jobs.c:3315 ADDINTERRUPT throws to the top-level
reader (DISCARD): the job settles 130 and the session survives.

Mirror the sibling classification at the drain (interactive = the
executor's session flag, as at both sibling sites; non-interactive
sessions set nothing and keep the exit classification
byte-identically), and keep the mark's invariant — a mark
classifies the unwind it was set for and never outlives its unit —
at the two places that convert or swallow that unwind: the unix
sleep fold that forgets a shell-only SIGINT consumes the mark with
the delivery, and run_source_impl's SigintUnwind arm consumes a
mark the same interrupt left behind.

Pins: tests/issue698_untrapped_sigint_midwait_discard.rs —
Windows Session faces (builtin sleep / external / non-interactive
guard) where the mailbox wait lives, unix PTY + die-by-signal
guards, and the mid-sleep fold bookkeeping pair.

Refs unixwin/niubash#698
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 29, 2026 17:49
@caomengxuan666
caomengxuan666 merged commit 2eb868b into master Oct 29, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/698-untrapped-ctrlc-midwait-discard branch October 30, 2026 01:41
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