From a67c8875543633523dc9004f858f28b2aab64803 Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Mon, 12 Aug 2024 21:47:46 +0200 Subject: [PATCH] Include stack trace in panic logs --- m/pager.go | 7 ++++--- m/panicHandler.go | 5 +++-- m/reader.go | 3 ++- m/search.go | 3 ++- twin/panicHandler.go | 5 +++-- twin/screen-setup.go | 3 ++- twin/screen-setup_test.go | 3 ++- twin/screen.go | 3 ++- twin/screen_test.go | 3 ++- 9 files changed, 22 insertions(+), 13 deletions(-) diff --git a/m/pager.go b/m/pager.go index ca1dc98..ff1801d 100644 --- a/m/pager.go +++ b/m/pager.go @@ -3,6 +3,7 @@ package m import ( "fmt" "regexp" + "runtime/debug" "strings" "time" @@ -323,7 +324,7 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom go func() { defer func() { - panicHandler("StartPaging()/moreLinesAvailable", recover()) + panicHandler("StartPaging()/moreLinesAvailable", recover(), debug.Stack()) }() for range p.reader.moreLinesAdded { @@ -342,7 +343,7 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom go func() { defer func() { - panicHandler("StartPaging()/spinner", recover()) + panicHandler("StartPaging()/spinner", recover(), debug.Stack()) }() // Spin the spinner as long as contents is still loading @@ -368,7 +369,7 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom go func() { defer func() { - panicHandler("StartPaging()/maybeDone", recover()) + panicHandler("StartPaging()/maybeDone", recover(), debug.Stack()) }() for range p.reader.maybeDone { diff --git a/m/panicHandler.go b/m/panicHandler.go index 585ba92..945152c 100644 --- a/m/panicHandler.go +++ b/m/panicHandler.go @@ -6,12 +6,13 @@ import ( log "github.com/sirupsen/logrus" ) -func panicHandler(goroutineName string, recoverResult any) { +func panicHandler(goroutineName string, recoverResult any, stackTrace []byte) { if recoverResult == nil { return } log.WithFields(log.Fields{ - "recoverResult": recoverResult, + "panic": recoverResult, + "stackTrace": string(stackTrace), }).Error("Goroutine panicked: " + goroutineName) } diff --git a/m/reader.go b/m/reader.go index 0bfa1df..00ceb91 100644 --- a/m/reader.go +++ b/m/reader.go @@ -7,6 +7,7 @@ import ( "io" "os" "path" + "runtime/debug" "strings" "sync" "sync/atomic" @@ -366,7 +367,7 @@ func newReaderFromStream(reader io.Reader, originalFileName *string, formatter c go func() { defer func() { - panicHandler("newReaderFromStream()/readStream()", recover()) + panicHandler("newReaderFromStream()/readStream()", recover(), debug.Stack()) }() returnMe.readStream(reader, formatter, lexer) diff --git a/m/search.go b/m/search.go index 74be668..cf3071a 100644 --- a/m/search.go +++ b/m/search.go @@ -3,6 +3,7 @@ package m import ( "fmt" "runtime" + "runtime/debug" "time" log "github.com/sirupsen/logrus" @@ -125,7 +126,7 @@ func (p *Pager) findFirstHit(startPosition linenumbers.LineNumber, beforePositio go func(i int, searchStart linenumbers.LineNumber, chunkBefore *linenumbers.LineNumber) { defer func() { - panicHandler("findFirstHit()/chunkSearch", recover()) + panicHandler("findFirstHit()/chunkSearch", recover(), debug.Stack()) }() findings[i] <- p._findFirstHit(searchStart, chunkBefore, backwards) diff --git a/twin/panicHandler.go b/twin/panicHandler.go index 83efb8b..f031be8 100644 --- a/twin/panicHandler.go +++ b/twin/panicHandler.go @@ -6,12 +6,13 @@ import ( log "github.com/sirupsen/logrus" ) -func panicHandler(goroutineName string, recoverResult any) { +func panicHandler(goroutineName string, recoverResult any, stackTrace []byte) { if recoverResult == nil { return } log.WithFields(log.Fields{ - "recoverResult": recoverResult, + "panic": recoverResult, + "stackTrace": string(stackTrace), }).Error("Goroutine panicked: " + goroutineName) } diff --git a/twin/screen-setup.go b/twin/screen-setup.go index ae7077b..4c6bf37 100644 --- a/twin/screen-setup.go +++ b/twin/screen-setup.go @@ -7,6 +7,7 @@ import ( "io" "os" "os/signal" + "runtime/debug" "sync/atomic" "syscall" @@ -118,7 +119,7 @@ func (screen *UnixScreen) setupSigwinchNotification() { signal.Notify(sigwinch, syscall.SIGWINCH) go func() { defer func() { - panicHandler("setupSigwinchNotification()/SIGWINCH", recover()) + panicHandler("setupSigwinchNotification()/SIGWINCH", recover(), debug.Stack()) }() for { diff --git a/twin/screen-setup_test.go b/twin/screen-setup_test.go index 542e289..4f80b2c 100644 --- a/twin/screen-setup_test.go +++ b/twin/screen-setup_test.go @@ -6,6 +6,7 @@ package twin import ( "io" "os" + "runtime/debug" "testing" "time" @@ -32,7 +33,7 @@ func TestInterruptableReader_blockedOnReadImmediate(t *testing.T) { readResultChan := make(chan readResult) go func() { defer func() { - panicHandler("TestInterruptableReader_blockedOnReadImmediate()", recover()) + panicHandler("TestInterruptableReader_blockedOnReadImmediate()", recover(), debug.Stack()) }() buffer := make([]byte, 1) diff --git a/twin/screen.go b/twin/screen.go index 006dbd5..c4501aa 100644 --- a/twin/screen.go +++ b/twin/screen.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "regexp" + "runtime/debug" "strconv" "strings" "unicode/utf8" @@ -181,7 +182,7 @@ func NewScreenWithMouseModeAndColorCount(mouseMode MouseMode, terminalColorCount go func() { defer func() { - panicHandler("NewScreenWithMouseModeAndColorCount()/mainLoop()", recover()) + panicHandler("NewScreenWithMouseModeAndColorCount()/mainLoop()", recover(), debug.Stack()) }() screen.mainLoop() diff --git a/twin/screen_test.go b/twin/screen_test.go index 98ea1c0..e77caae 100644 --- a/twin/screen_test.go +++ b/twin/screen_test.go @@ -3,6 +3,7 @@ package twin import ( "io" "os" + "runtime/debug" "strings" "testing" "time" @@ -270,7 +271,7 @@ func TestInterruptableReader_blockedOnRead(t *testing.T) { readResultChan := make(chan readResult) go func() { defer func() { - panicHandler("TestInterruptableReader_blockedOnRead()", recover()) + panicHandler("TestInterruptableReader_blockedOnRead()", recover(), debug.Stack()) }() buffer := make([]byte, 1)