From 42401df744b606cac73e6c1d3f729a987a96c66b Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Mon, 18 Apr 2022 12:41:17 +0100 Subject: [PATCH] catch panics in handleIncoming and handleOutgoing --- multiplex.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/multiplex.go b/multiplex.go index 7650f1d..90e364b 100644 --- a/multiplex.go +++ b/multiplex.go @@ -8,6 +8,8 @@ import ( "fmt" "io" "net" + "os" + "runtime/debug" "sync" "time" @@ -250,6 +252,12 @@ func (mp *Multiplex) sendMsg(timeout, cancel <-chan struct{}, header uint64, dat } func (mp *Multiplex) handleOutgoing() { + defer func() { + if rerr := recover(); rerr != nil { + fmt.Fprintf(os.Stderr, "caught panic in handleOutgoing: %s\n%s\n", rerr, debug.Stack()) + } + }() + for { select { case <-mp.shutdown: @@ -349,6 +357,12 @@ func (mp *Multiplex) cleanup() { } func (mp *Multiplex) handleIncoming() { + defer func() { + if rerr := recover(); rerr != nil { + fmt.Fprintf(os.Stderr, "caught panic in handleIncoming: %s\n%s\n", rerr, debug.Stack()) + } + }() + defer mp.cleanup() recvTimeout := time.NewTimer(0)