Skip to content

fix: Ctrl-C during fg wait discards rest of line (#1639) - #1702

Closed
arookieofc wants to merge 1 commit into
masterfrom
fix-1639-ctrlc-discard-rest
Closed

arookieofc wants to merge 1 commit into
masterfrom
fix-1639-ctrlc-discard-rest

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Fixes #1639.

Bug

Ctrl-C during a foreground wait ran the rest of the line (sleep 25; echo AFTER printed AFTER rc=130); GNU discards it (fresh prompt, $? = 130 for the NEXT command). Both the builtin-virtual-child and external wait paths diverged the same way.

GNU rule (probed + sourced)

third_party/bash/jobs.c:3305-3361: a job-control foreground job killed by SIGINT with SIGINT untrapped sets interrupt_state, so the loop or list breaks. Probes against bash 5.2 pinned the boundaries:

  • Job-control fg SIGINT death: rest of line discarded (; tail, || fallback, whole loop body + following commands).
  • No job control (scripts, piped -i): a child SIGINT death is just status 130, execution continues.
  • Trapped SIGINT: the trap runs, the list continues.
  • read interrupted by ^C: rest of line discarded, next command sees 130.

Rubash has no job control; its interactive fg wait is the job-control-like mode (jobs.c wait_for model, rubash#1250), so the discard applies to interactive waits.

Changes

  • New sigint_discard_pending class marker riding the existing DISCARD abandon vehicle (ast_exec loop-top check), leaving 130 instead of the plain DISCARD 1. It is cleared at every pending-clearing boundary like the other class markers.
  • arm_sigint_discard (external_inner.rs), gated on interactive AND untrapped, called from: the four external-wait status sites, the two untrapped returns of sleep_interruptibly, and the two read sites that actually return 128+SIGINT (the invalid-name/error arms return 1 and do not arm).
  • Inside an eval/source boundary list the discard abandons the whole unit (GNU's interrupt_state breaks the entire parse_and_execute loop), unlike evalerror DISCARD's per-line skip (rubash#637). At the top-level reader a following queued line still runs as a new command.

Verification

New tests/issue1639_ctrlc_discard_rest.rs (script(1) PTY harness, transcript assertions only — script itself sits in the PTY foreground group, so its exit code is contaminated after ^C): builtin, external (/usr/bin/sleep), ||, loop, trapped-continues, read, single-line eval, multi-line eval, plus a script-mode child-SIGINT-continues negative — 9/9.

Regression: DISCARD family (1226, 1318, 1541, 1469, 1603), eval family (637, 1563, 1013, 282), coproc/jobs family, signal/trap family, read family — all green. issue1035_1036::background_compound_body_can_still_trap_int fails identically on pure master (verified via stash during #1640) — pre-existing, unrelated.

Known adjacent items, not fixed here

  • Pipeline fg waits need their own SIGINT scan over stage statuses.
  • ^C inside a flat ( ) region kills the shell today (the fg SIGINT guard arms only at subshell_depth == 0) — a pre-existing flat-region signal gap, separate issue. The boundary branch in the abandon check mirrors the evalerror subshell branch for when that gap closes.
  • A trapped SIGINT inside a loop keeps the loop in rubash while GNU job-control breaks it (out of this issue's untrapped scope).
  • The flag rides the sleep/read aborts, so [bug] shell-directed SIGINT aborts builtin sleep with 130; GNU lets it complete (rc=0) #1640's kernel-caused gate (its own branch) decides when a shell-directed kill(2) INT arms it at all.

GNU (jobs.c:3305-3361) sets interrupt_state when a job-control
foreground job is killed by SIGINT with SIGINT untrapped, so the
loop or list breaks and the reader discards the rest of the line,
leaving 128+SIGINT for the next command. Rubash ran the rest with
rc=130 instead, on both the builtin-virtual-child and external
wait paths. The fix rides the existing DISCARD abandon vehicle
(ast_exec loop-top check) with a new sigint_discard_pending class
marker that leaves 130 instead of the plain DISCARD 1.

Trigger sites (all via arm_sigint_discard, gated on interactive
AND untrapped): the four external-wait status sites, the two
untrapped returns of sleep_interruptibly, and the two read sites
that actually return 128+SIGINT. The gates match GNU's probed
boundaries: scripts continue after a child SIGINT death (bash 5.2
probe), a trapped SIGINT runs its action and the list continues,
and the discard dies with a flat ( ) region like GNU's forked
subshell boundary. Inside an eval/source boundary list the
discard abandons the whole unit (GNU's interrupt_state breaks the
entire parse_and_execute loop), unlike evalerror DISCARD's
per-line skip.

Known adjacent items, not fixed here: pipeline fg waits (stage
statuses need their own SIGINT scan); ^C inside a flat ( )
region kills the shell today (the fg SIGINT guard arms only at
subshell_depth == 0 — a pre-existing flat-region signal gap);
a trapped SIGINT inside a loop keeps the loop in rubash while
GNU job-control breaks it; the flag rides the sleep/read aborts,
so #1640's kernel-caused gate (its own branch) decides when a
shell-directed kill(2) INT arms it at all.

Tests: new tests/issue1639_ctrlc_discard_rest.rs (script(1) PTY
harness, transcript assertions only): builtin, external, ||,
loop, trapped-continues, read, single-line eval, multi-line
eval, plus a script-mode child-SIGINT-continues negative — 9/9.
caomengxuan666 added a commit that referenced this pull request Oct 26, 2026
…rrupts read (#1639)

A Ctrl-C interrupting a blocking read is an untrapped-SIGINT
foreground wait: GNU returns 128+SIGINT from the builtin and the
executing list throws to the top level (jobs.c waitchld
ADDINTERRUPT model), discarding the rest of the line. The read
interrupt flag is only ever set under the armed builtin guard
(interactive + untrapped), so mark fg_sigint_unwind at the three
points where an interrupted status is actually returned; the
list loop converts it to SigintUnwind like the sleep/external
producers.

Read shape and test set ported from PR #1702 by
a-rookie-of-C-language (whose evalerror-vehicle implementation
covered read/loop/eval shapes this slice lacked); loop and eval
frame tests added on the same PTY carrier — the SigintUnwind
error already crosses those frames.
@caomengxuan666

Copy link
Copy Markdown
Member

Thank you for this — it covered shapes the parallel sig-trap slice (#1784, now merged) did not, and those parts were ported into the slice with credit rather than merging a competing implementation of the same issue.

What #1784 already covered (independently, via a different vehicle — a SigintUnwind error unwinding to the reader instead of the evalerror DISCARD vehicle): builtin-sleep and external-wait producers, function and subshell frames, || discard priority, trapped-INT continuation, interactive-only gating. GNU bash 5.3.20 PTY probes (script(1) carrier, ^C byte to the foreground group) confirm the behaviour matches on all of those (discard + NEXT_130), and the merged PR's CI ran the full suite green on Linux.

What your PR had that the slice lacked — verified against GNU 5.3.20 under the same PTY carrier and ported into #1784 (commit a2500d6, credited to this PR in the commit message):

  • read interrupted by Ctrl-C discards the rest of the line (GNU probe: read x; echo AFTER + ^C → no AFTER, NEXT_130). The slice had no read producer; the port marks fg_sigint_unwind at the three points where read returns an interrupted status (your three arming sites), with regression tests.
  • Loop-frame and eval-frame abandonment tests (your issue1639_loop_abandoned_after_ctrl_c, issue1639_eval_*): the slice's unwind already crosses those frames; tests were added on the slice's carrier to pin them.

One divergence worth noting: your header describes the discard as subshell-bounded (parent list continues), but the GNU 5.3.20 probe for ( sleep …; echo IN_SUB ); echo AFTER discards AFTER too (NEXT_130), which is what the merged slice implements.

Closing as incorporated/superseded by #1784. The duplication came from the slice branch not being pushed while in flight — on us, not on you.

@caomengxuan666
caomengxuan666 deleted the fix-1639-ctrlc-discard-rest 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.

[bug] Ctrl-C during fg wait runs the rest of the line; GNU discards it (fresh prompt)

2 participants