From ef12d02b8360fa7bd6005904f0968e03221c8bcd Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Tue, 14 Feb 2023 14:00:13 -0800 Subject: [PATCH] msghub: Recover and log panics (#336) Signed-off-by: James Hillyerd --- pkg/msghub/hub.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/msghub/hub.go b/pkg/msghub/hub.go index ef98206..fdbb380 100644 --- a/pkg/msghub/hub.go +++ b/pkg/msghub/hub.go @@ -6,6 +6,7 @@ import ( "github.com/inbucket/inbucket/pkg/extension" "github.com/inbucket/inbucket/pkg/extension/event" + "github.com/rs/zerolog/log" ) // Length of msghub operation queue @@ -53,7 +54,7 @@ func (hub *Hub) Start(ctx context.Context) { close(hub.opChan) return case op := <-hub.opChan: - op(hub) + hub.runOp(op) } } } @@ -108,3 +109,17 @@ func (hub *Hub) Sync() { } <-done } + +func (hub *Hub) runOp(op func(*Hub)) { + defer func() { + if r := recover(); r != nil { + if err, ok := r.(error); ok { + log.Error().Str("module", "msghub").Err(err).Msg("Operation panicked") + } else { + log.Error().Str("module", "msghub").Err(err).Msgf("Operation panicked: %s", r) + } + } + }() + + op(hub) +}