Skip to content

Commit

Permalink
Locate panic line
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Dec 13, 2024
1 parent 6a8c135 commit e3c3587
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
21 changes: 19 additions & 2 deletions pkg/graceful/graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ package graceful

import (
"context"
"fmt"
"os"
"os/signal"
"runtime/debug"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -60,7 +63,7 @@ func (p *Process) AddStarter(ctx context.Context, fn func(ctx context.Context) e
go func() {
defer func() {
if r := recover(); r != nil {
p.logger.Error().Interface("panic", r).Msg("panic in service")
p.logger.Error().Err(panicToErr(r, 10)).Msg("panic in service")
p.ShutdownNow()
}
}()
Expand Down Expand Up @@ -123,7 +126,7 @@ func (p *Process) ShutdownNow() {
go func() {
defer func() {
if r := recover(); r != nil {
p.logger.Error().Interface("panic", r).Msg("panic during shutdown")
p.logger.Error().Err(panicToErr(r, 10)).Msg("panic during shutdown")
}

// complete shutdown
Expand All @@ -144,6 +147,20 @@ func (p *Process) ShutdownNow() {
}
}

// panicToErr converts panic to error WITH exact line of panic.
// Note the offset should be determined empirically.
func panicToErr(panic any, offset int) error {
stack := string(debug.Stack())
lines := strings.Split(stack, "\n")
line := ""

if len(lines) > offset {
line = strings.TrimSpace(lines[offset])
}

return fmt.Errorf("panic: %v at %s", panic, line)
}

// NewSigChan creates a new signal channel.
func NewSigChan(signals ...os.Signal) chan os.Signal {
out := make(chan os.Signal, 1)
Expand Down
27 changes: 26 additions & 1 deletion pkg/graceful/graceful_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestProcess(t *testing.T) {
assert.Contains(t, ts.logBuffer.String(), "failed to start service")
})

t.Run("Panic handling", func(t *testing.T) {
t.Run("Panic handling during startup", func(t *testing.T) {
t.Parallel()

// ARRANGE
Expand All @@ -153,6 +153,31 @@ func TestProcess(t *testing.T) {
// Check that service had errors and was stopped
assert.Contains(t, ts.logBuffer.String(), "Shutdown completed")
assert.Contains(t, ts.logBuffer.String(), "panic in service")

// Check that error contains exact line of panic
assert.Contains(t, ts.logBuffer.String(), "graceful_test.go:145")
})

t.Run("Panic handling during shutdown", func(t *testing.T) {
t.Parallel()

// ARRANGE
ts := newTestSuite(t, defaultTimeout, false)

ts.process.AddStopper(func() {
panic("bombarda maxima")
})

// ACT
ts.process.ShutdownNow()

// ASSERT
// Check that service had errors and was stopped
assert.Contains(t, ts.logBuffer.String(), "Shutdown completed")
assert.Contains(t, ts.logBuffer.String(), "panic during shutdown")

// Check that error contains exact line of panic
assert.Contains(t, ts.logBuffer.String(), "graceful_test.go:168")
})

t.Run("WaitForShutdown noop", func(t *testing.T) {
Expand Down

0 comments on commit e3c3587

Please sign in to comment.