fix(bonfire): restore the terminal when a live view is interrupted

- while a view runs, runLive relays SIGINT and SIGTERM (unless ignored);
  on a signal it kills the program, which shows the cursor and restores
  the terminal, unregisters the view, stops relaying and raises the same
  signal again, so the default action or the command's own handler still
  applies
- the generated main, bonfire.Execute's context and Ctrl+C for commands
  without a live view are unchanged
- TestLiveViewRestoresTerminalOnSignal pins both paths in a re-executed
  helper process (WR-01)
This commit is contained in:
Jakub Zych
2026-10-22 02:14:10 +02:00
parent 86e4e270d3
commit d151a252c5
3 changed files with 184 additions and 2 deletions

View File

@@ -4,15 +4,20 @@ package bonfire
// process.go and progress.go) to bubbletea v2, without SSU's git and
// engine types. Each runs as a bubbletea program with input off (stdin
// stays in cooked mode, so Ctrl+C reaches the command's own signal
// handling), no signal handler of its own and the command's context.
// handling), without bubbletea's signal handler and with the command's
// context. While a view runs, runLive relays SIGINT and SIGTERM only to
// restore the terminal before the signal takes its usual effect.
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"charm.land/bubbles/v2/progress"
"charm.land/bubbles/v2/spinner"
@@ -366,6 +371,7 @@ func runLive(ctx context.Context, out io.Writer, c *console, model tea.Model, wo
defer close(finished)
_, _ = prog.Run()
}()
stopSignals := watchSignals(view, finished)
completed := false
defer func() {
@@ -384,12 +390,80 @@ func runLive(ctx context.Context, out io.Writer, c *console, model tea.Model, wo
prog.Send(finishMsg{err: result})
<-finished
liveMu.Unlock()
stopSignals()
}()
err = work(prog.Send)
completed = true
return true, err
}
// watchSignals relays SIGINT and SIGTERM (those not ignored) while view
// runs. The views run without bubbletea's own signal handling, so without
// this a Ctrl+C would end the process with the cursor still hidden. On a
// signal the program is killed (bubbletea then restores the cursor and the
// terminal), the view is unregistered and the signal is raised again on
// the process with the relay stopped, so its default action, or the
// command's own handler, still applies. The returned function stops the
// relay when the view ends normally; a signal that arrived meanwhile is
// raised again too.
func watchSignals(view *liveView, finished <-chan struct{}) (stop func()) {
var relay []os.Signal
for _, sig := range []os.Signal{os.Interrupt, syscall.SIGTERM} {
if !signal.Ignored(sig) {
relay = append(relay, sig)
}
}
if len(relay) == 0 {
return func() {}
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, relay...)
quit := make(chan struct{})
done := make(chan struct{})
go func() {
defer close(done)
select {
case sig := <-sigs:
view.prog.Kill()
<-finished
liveMu.Lock()
if activeView == view {
activeView = nil
}
liveMu.Unlock()
signal.Stop(sigs)
raise(sig)
case <-quit:
}
}()
return func() {
close(quit)
<-done
signal.Stop(sigs)
select {
case sig := <-sigs:
raise(sig)
default:
}
}
}
// raise sends sig to the own process; when that fails, it exits with 128
// plus the signal number, as a shell reports a death by that signal.
func raise(sig os.Signal) {
p, err := os.FindProcess(os.Getpid())
if err == nil {
err = p.Signal(sig)
}
if err != nil {
code := 1
if s, ok := sig.(syscall.Signal); ok {
code = 128 + int(s)
}
os.Exit(code)
}
}
// printAboveLines prints s, one or more lines, above the running live view,
// whichever console started it (bonfire.PrintAbove). It reports false when
// no view runs.