Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover panics from within cmds goroutines #234

Closed
brooks-connor-a opened this issue Feb 18, 2022 · 3 comments · Fixed by #1134
Closed

Recover panics from within cmds goroutines #234

brooks-connor-a opened this issue Feb 18, 2022 · 3 comments · Fixed by #1134
Labels
bug Something isn't working

Comments

@brooks-connor-a
Copy link

Panics are handled by recovering the panic and gracefully tearing down the Program. However goroutines started within the Program are outside the scope of the recover, and as such can end in the program terminating without properly tearing down. This can result in the terminal getting stuck in a non-interactive state until it is reset.

Here's a minimal reproduction:

package main
​
import ( 
    tea "github.com/charmbracelet/bubbletea"
)
​
type model struct {}

func (m model) Init() tea.Cmd {
    return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { 
    return m, func() tea.Msg { 
        if true { 
            panic("")
        } 
        return tea.KeyEnter
    } 
}
​
func (m model) View() string { 
    return ""
}
​
func main() { 
    p := tea.NewProgram(model{})
    p.Start()
}
@decentral1se
Copy link

decentral1se commented Jun 20, 2023

Thanks for reporting @brooks-connor-a, ran into this also.

(Firing up background Tor process, then panic, then it isn't cleaned up)

@joerdav
Copy link

joerdav commented Oct 18, 2023

I took a look at this, it seems like the best way to solve was to recover the panic and write to the errs channel. Happy for some feedback if it's not in the spirit of how bubbletea works.

aymanbagabas added a commit that referenced this issue Sep 10, 2024
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.

Supersedes: #846
Fixes: #234
@meowgorithm
Copy link
Member

Hi! Just a note that this is now fixed and available in Bubble Tea v1.1.1. You can use tea.Kill to reset the terminal from panics externally.

p := tea.NewProgram(model{})

go func() {
	time.Sleep(3 * time.Second)
	defer p.Kill()
	panic("Oh no!")
}()
	
if _, err := p.Run() {
	log.Fatal(err)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants