bubbletea icon indicating copy to clipboard operation
bubbletea copied to clipboard

Recover panics from within cmds goroutines

Open brooks-connor-a opened this issue 3 years ago • 2 comments

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()
}

brooks-connor-a avatar Feb 18 '22 22:02 brooks-connor-a

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

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

decentral1se avatar Jun 20 '23 13:06 decentral1se

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.

joerdav avatar Oct 18 '23 07:10 joerdav