fix: keep signal subscription alive through shutdown (#1853)

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Drew Smirnoff <drew@charm.land>
Signed-off-by: Drew Smirnoff <drew@charm.land>
This commit is contained in:
draw me an elephant 2026-11-20 09:38:53 -05:00 committed by GitHub
parent 7ab4335dac
commit 7dd9c4c46e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 92 additions and 14 deletions

View file

@ -55,3 +55,69 @@ func TestHandleSignalsReturnsAfterContextCancelDuringPendingSend(t *testing.T) {
t.Fatal("handleSignals did not return after context cancel: signal goroutine is deadlocked")
}
}
// TestHandleSignalsCatchesSecondSignalDuringShutdown is a regression test for
// https://github.com/charmbracelet/bubbletea/issues/1849: after a quit
// message is forwarded, the signal subscription must stay alive until the
// program finishes, so a second SIGINT during shutdown is caught instead of
// taking the runtime's default path and killing the process.
func TestHandleSignalsCatchesSecondSignalDuringShutdown(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Register our own handler first so a SIGINT can never take down the
// test process, whether or not handleSignals is catching it.
ours := make(chan os.Signal, 2)
signal.Notify(ours, syscall.SIGINT)
defer signal.Stop(ours)
finished := make(chan struct{})
defer close(finished)
p := &Program{
ctx: ctx,
msgs: make(chan Msg),
finished: finished,
}
done := p.handleSignals()
time.Sleep(100 * time.Millisecond) // let the handler register its signal.Notify
// While the program runs, every signal is forwarded as an interrupt
// message, so a program that doesn't quit on the first one (for example
// because a filter dropped it) still gets the next one.
for i := range 2 {
if err := syscall.Kill(os.Getpid(), syscall.SIGINT); err != nil {
t.Fatalf("failed to send SIGINT: %v", err)
}
<-ours
select {
case msg := <-p.msgs:
if _, ok := msg.(InterruptMsg); !ok {
t.Fatalf("expected InterruptMsg, got %T", msg)
}
case <-time.After(5 * time.Second):
t.Fatalf("SIGINT %d was not forwarded", i+1)
}
}
// Context cancellation (the start of shutdown) ends the handler.
cancel()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("handleSignals did not finish after context cancel")
}
// Release our own watcher so that whether SIGINT is still caught depends
// solely on the program's subscription. A second SIGINT during the
// shutdown window must still be caught (and dropped); if the program had
// already released its subscription it would take the runtime's default
// path and terminate this test process — so surviving is the assertion.
signal.Stop(ours)
if err := syscall.Kill(os.Getpid(), syscall.SIGINT); err != nil {
t.Fatalf("failed to send second SIGINT: %v", err)
}
time.Sleep(200 * time.Millisecond)
}

40
tea.go
View file

@ -672,6 +672,11 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {
func (p *Program) handleSignals() chan struct{} {
ch := make(chan struct{})
// Run sets finished before it starts the handlers. Read it here, not in
// the goroutine below, which may still be running when Run is called
// again and replaces it.
finished := p.finished
// Listen for SIGINT and SIGTERM.
//
// In most cases ^C will not send an interrupt because the terminal will be
@ -683,27 +688,34 @@ func (p *Program) handleSignals() chan struct{} {
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
defer func() {
signal.Stop(sig)
close(ch)
}()
defer signal.Stop(sig)
for {
select {
case <-p.ctx.Done():
// The program is shutting down: report the handler done so
// shutdown can proceed, but keep the signal subscription
// alive until Run returns. A second SIGINT/SIGTERM arriving
// while the terminal is being restored is then caught and
// dropped instead of taking the runtime's default path,
// which kills the process before the terminal is restored.
close(ch)
if finished != nil {
<-finished
}
return
case s := <-sig:
if atomic.LoadUint32(&p.ignoreSignals) == 0 {
var msg Msg = QuitMsg{}
if s == syscall.SIGINT {
msg = InterruptMsg{}
}
select {
case p.msgs <- msg:
case <-p.ctx.Done():
}
return
if atomic.LoadUint32(&p.ignoreSignals) != 0 {
continue
}
var msg Msg = QuitMsg{}
if s == syscall.SIGINT {
msg = InterruptMsg{}
}
select {
case p.msgs <- msg:
case <-p.ctx.Done():
}
}
}