diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-13 14:31:59 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-13 14:31:59 +0300 |
| commit | ed7cf2505d92e05411d476b445bda45cab9aaf89 (patch) | |
| tree | 9de12631cda85c4336c0d8abf6266351fb0eb6fc /internal/eventloop_runtime.go | |
| parent | 42645a4889c1e45ad2ab85e0a371ef8e1054062e (diff) | |
feat(eventloop): add panic recovery to events() goroutine for resilience
Wrap processRawEvent calls in a new processRawEventSafe() helper that
uses defer/recover to catch any panic from a callback and convert it into
a warning notification via warningCb, preventing a single bad event from
crashing the whole process. Added TestEventsPanicInCallbackIsRecoveredAndNotified
to verify the recovery behaviour end-to-end.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/eventloop_runtime.go')
| -rw-r--r-- | internal/eventloop_runtime.go | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/internal/eventloop_runtime.go b/internal/eventloop_runtime.go index 85a90a1..01bc798 100644 --- a/internal/eventloop_runtime.go +++ b/internal/eventloop_runtime.go @@ -3,6 +3,7 @@ package internal import ( "context" "fmt" + "runtime/debug" "time" "ior/internal/event" @@ -85,7 +86,9 @@ func (e *eventLoop) events(ctx context.Context, rawCh <-chan []byte) <-chan *eve if len(raw) == 0 { continue } - e.processRawEvent(raw, ch) + // Recover from any panic inside a callback so a single + // bad event cannot crash the entire process. + e.processRawEventSafe(raw, ch) case <-ctx.Done(): fmt.Println("Stopping event loop") return @@ -96,6 +99,19 @@ func (e *eventLoop) events(ctx context.Context, rawCh <-chan []byte) <-chan *eve return ch } +// processRawEventSafe calls processRawEvent and recovers from any panic, +// converting it into a warning notification so that one misbehaving event +// does not crash the whole process. +func (e *eventLoop) processRawEventSafe(raw []byte, ch chan<- *event.Pair) { + defer func() { + if r := recover(); r != nil { + stack := debug.Stack() + e.notifyWarning(fmt.Sprintf("Recovered panic in processRawEvent: %v\n%s", r, stack)) + } + }() + e.processRawEvent(raw, ch) +} + func (e *eventLoop) processRawEvent(raw []byte, ch chan<- *event.Pair) { if len(raw) == 0 { return |
