From b9e06e498e50d1ba2dad04f13663d48d515e0c47 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 15 Nov 2021 21:40:17 +0100 Subject: [PATCH] Add panic handlers for user and downstream goroutines This only brings down a single user or downstream on panic, instead or bringing down the whole bouncer. Closes: https://todo.sr.ht/~emersion/soju/139 --- server.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/server.go b/server.go index eaf6bf9..1e08885 100644 --- a/server.go +++ b/server.go @@ -9,6 +9,7 @@ import ( "mime" "net" "net/http" + "runtime/debug" "sync" "sync/atomic" "time" @@ -163,6 +164,12 @@ func (s *Server) addUserLocked(user *User) *user { s.stopWG.Add(1) go func() { + defer func() { + if err := recover(); err != nil { + s.Logger.Printf("panic serving user %q: %v\n%v", user.Username, err, debug.Stack()) + } + }() + u.run() s.lock.Lock() @@ -178,6 +185,12 @@ func (s *Server) addUserLocked(user *User) *user { var lastDownstreamID uint64 = 0 func (s *Server) handle(ic ircConn) { + defer func() { + if err := recover(); err != nil { + s.Logger.Printf("panic serving downstream %q: %v\n%v", ic.RemoteAddr(), err, debug.Stack()) + } + }() + atomic.AddInt64(&s.connCount, 1) id := atomic.AddUint64(&lastDownstreamID, 1) dc := newDownstreamConn(s, ic, id)