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
This commit is contained in:
Simon Ser 2021-11-15 21:40:17 +01:00
parent 37c1b3e29c
commit b9e06e498e

View File

@ -9,6 +9,7 @@ import (
"mime" "mime"
"net" "net"
"net/http" "net/http"
"runtime/debug"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -163,6 +164,12 @@ func (s *Server) addUserLocked(user *User) *user {
s.stopWG.Add(1) s.stopWG.Add(1)
go func() { 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() u.run()
s.lock.Lock() s.lock.Lock()
@ -178,6 +185,12 @@ func (s *Server) addUserLocked(user *User) *user {
var lastDownstreamID uint64 = 0 var lastDownstreamID uint64 = 0
func (s *Server) handle(ic ircConn) { 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) atomic.AddInt64(&s.connCount, 1)
id := atomic.AddUint64(&lastDownstreamID, 1) id := atomic.AddUint64(&lastDownstreamID, 1)
dc := newDownstreamConn(s, ic, id) dc := newDownstreamConn(s, ic, id)