Loop API print proper stack traces for panics in go routines

This commit is contained in:
Kovid Goyal
2025-06-01 12:51:59 +05:30
parent ecb9c46b95
commit d1faccdd1c
4 changed files with 23 additions and 4 deletions

View File

@@ -49,6 +49,7 @@ type Loop struct {
timers, timers_temp []*timer
timer_id_counter, write_msg_id_counter IdType
wakeup_channel chan byte
panic_channel chan any
pending_writes []write_msg
tty_write_channel chan write_msg
pending_mouse_events *utils.RingBuffer[MouseEvent]
@@ -328,11 +329,14 @@ func (self *Loop) Run() (err error) {
lines = append(lines, fmt.Sprintf("%s\r\n\t%s:%d\r\n", frame.Function, frame.File, frame.Line))
}
text := strings.Join(lines, "")
os.Stderr.WriteString(text)
tty.DebugPrintln(strings.TrimSpace(text))
if self.terminal_options.Alternate_screen {
term, err := tty.OpenControllingTerm(tty.SetRaw)
if err == nil {
is_terminal := tty.IsTerminal(os.Stderr.Fd())
if is_terminal {
os.Stderr.WriteString("\x1b]\x1b\\\x1bc\x1b[H\x1b[2J") // reset terminal
}
os.Stderr.WriteString(text)
if is_terminal {
if term, err := tty.OpenControllingTerm(tty.SetRaw); err == nil {
defer term.RestoreAndClose()
fmt.Println("Press any key to exit.\r")
buf := make([]byte, 16)
@@ -588,6 +592,12 @@ type SizedText struct {
Width int
}
func (self *Loop) RecoverFromPanicInGoRoutine() {
if r := recover(); r != nil {
self.panic_channel <- r
}
}
func (self *Loop) DrawSizedText(text string, spec SizedText) {
b := strings.Builder{}
b.Grow(len(text) + 24)

View File

@@ -394,6 +394,7 @@ func (self *Loop) run() (err error) {
self.write_msg_id_counter = 0
write_done_channel := make(chan IdType)
self.wakeup_channel = make(chan byte, 256)
self.panic_channel = make(chan any)
self.pending_writes = make([]write_msg, 0, 256)
err_channel := make(chan error, 8)
self.death_signal = SIGNULL
@@ -563,6 +564,8 @@ func (self *Loop) run() (err error) {
}
select {
case <-timeout_chan:
case p := <-self.panic_channel:
panic(p)
case <-self.wakeup_channel:
for len(self.wakeup_channel) > 0 {
<-self.wakeup_channel