Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panichandler: process panic handlers in the same go routine #875

Merged
merged 3 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions pkg/panichandler/panichandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,20 @@ func (r *PanicHandlerRegistry) RegisterPanicHandler(ph PanicHandler) {
// Crash invokes each of the registered panic handler and then rethrows panic - shutting down the app.
func (r *PanicHandlerRegistry) Crash(v interface{}) {
stackTrace := Capture()
waitCh := make(chan struct{})

go crashOnce.Do(func() {
crashOnce.Do(func() {
r.mutex.RLock()
defer r.mutex.RUnlock()
wg := sync.WaitGroup{}
wg.Add(len(r.Handlers))

go func() {
// terminate the app after 5 seconds in case the panic handler is stuck
time.Sleep(5 * time.Second)
panic(v)
}()

for _, handler := range r.Handlers {
h := handler
go func() {
defer wg.Done()
h(v, stackTrace)
}()
handler(v, stackTrace)
}
wg.Wait()
close(waitCh)
})

select {
case <-waitCh:
case <-time.After(5 * time.Second):
}
panic(v)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func bytesToStrUnsafe(data []byte) string {
}

// sentryPanicHandler is a panic handler that sends the fatal level event to Sentry with diagnostic information.
func (s *SentryWriter) sentryPanicHandler(e interface{}, stacktrace panichandler.Callstack) {
func (s *SentryWriter) sentryPanicHandler(e interface{}, _ panichandler.Callstack) {
duration, _ := time.ParseDuration(SentryFlushWait)

// Crash Log
Expand Down Expand Up @@ -216,14 +216,6 @@ func (s *SentryWriter) sentryPanicHandler(e interface{}, stacktrace panichandler
})
}

// Stacktrace
sentry.AddBreadcrumb(&sentry.Breadcrumb{
Type: "debug",
Category: "Stacktrace",
Level: sentry.LevelInfo,
Data: stacktrace.GetEntries(),
})

sentry.CurrentHub().Recover(e)
sentry.Flush(duration)
}