aerc: try to recover from a panic

As of now we crash fairly often. The problem is that we didn't run the cleanup
routine of the ui in this case, leaving the pty in a bad state.
Instead, recover from a panic and at least try to run the ui deinit.
This commit is contained in:
Reto Brunner 2021-02-11 21:02:46 +01:00
parent 8ecf0b73f4
commit c06a2e61fc
1 changed files with 19 additions and 0 deletions

19
aerc.go
View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"runtime/debug"
"sort" "sort"
"time" "time"
@ -153,6 +154,8 @@ func main() {
ui *libui.UI ui *libui.UI
) )
defer PanicTermFix(ui) // recover upon panic and try restoring the pty
aerc = widgets.NewAerc(conf, logger, func(cmd []string) error { aerc = widgets.NewAerc(conf, logger, func(cmd []string) error {
return execCommand(aerc, ui, cmd) return execCommand(aerc, ui, cmd)
}, func(cmd string) []string { }, func(cmd string) []string {
@ -198,3 +201,19 @@ func main() {
} }
aerc.CloseBackends() aerc.CloseBackends()
} }
//FatalTermFix prints the stacktrace upon panic and tries to recover the term
// not doing that leaves the terminal in a broken state
func PanicTermFix(ui *libui.UI) {
var err interface{}
if err = recover(); err == nil {
return
}
debug.PrintStack()
if ui != nil {
ui.Close()
}
fmt.Fprintf(os.Stderr, "aerc crashed: %v\n", err)
os.Exit(1)
}