Skip to content
Myr

fix: recover from panics within cmds

Before adoption: this commit came in with the history the project adopted, so no landing records it.

We should recover from panics within a cmd. Currently, Bubble Tea only recovers from panics within the eventLoop. This makes it so that it also recovers from panics within cmd go routines.

authored by Ayman Bagabas · committed by Ayman Bagabas · parent 7ca1b0b · Browse files · 1 file+16 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Supersedes: https://github.com/charmbracelet/bubbletea/pull/846
  • Fixes: https://github.com/charmbracelet/bubbletea/issues/234

Diff stats

1 file+16 added−8 removed

  • other:+16 added−8 removed

1 file+16 added−8 removed

modified:tea.go+16 added-8 removed
310 unmodified lines
311 // (e.g. tick commands that sleep for half a second). It's not312 // possible to cancel them so we'll have to leak the goroutine313 // until Cmd returns.314 go func() {315 // Recover from panics.316 if !p.startupOptions.has(withoutCatchPanics) {317 defer p.recoverFromPanic()318 }319320 msg := cmd() // this can be long.321 p.Send(msg)322 }()323 }
192 unmodified lines
516 }517518 // Recover from panics.519 if !p.startupOptions.has(withoutCatchPanics) {515 defer func() {516 if r := recover(); r != nil {517 p.shutdown(true)518 fmt.Printf("Caught panic:\n\n%s\n\nRestoring terminal...\n\n", r)519 debug.PrintStack()520 return521 }522 }()520 defer p.recoverFromPanic()521 }522523 // If no renderer is set use the standard one.524 if p.renderer == nil {
163 unmodified lines
688 _ = p.restoreTerminalState()689 p.finished <- struct{}{}690}691692// recoverFromPanic recovers from a panic, prints the stack trace, and restores693// the terminal to a usable state.694func (p *Program) recoverFromPanic() {695 if r := recover(); r != nil {696 p.shutdown(true)697 fmt.Printf("Caught panic:\n\n%s\n\nRestoring terminal...\n\n", r)698 debug.PrintStack()699 }700}701702// ReleaseTerminal restores the original terminal state and cancels the input703// reader. You can return control to the Program with RestoreTerminal.704func (p *Program) ReleaseTerminal() error {705 atomic.StoreUint32(&p.ignoreSignals, 1)
76 unmodified lines