Catch panics in portal message handler

This commit is contained in:
Tulir Asokan 2023-09-04 11:51:52 +03:00
parent 53e992c25d
commit bbcfff623d

View file

@ -24,6 +24,7 @@ import (
"fmt"
"image"
_ "image/gif"
"runtime/debug"
"strconv"
"strings"
"sync"
@ -312,13 +313,30 @@ func (portal *Portal) handleMatrixMessageLoopItem(msg PortalMatrixMessage) {
func (portal *Portal) handleMessageLoop() {
for {
portal.handleOneMessageLoopItem()
}
}
func (portal *Portal) handleOneMessageLoopItem() {
defer func() {
if err := recover(); err != nil {
logEvt := portal.zlog.WithLevel(zerolog.FatalLevel).
Str(zerolog.ErrorStackFieldName, string(debug.Stack()))
actualErr, ok := err.(error)
if ok {
logEvt = logEvt.Err(actualErr)
} else {
logEvt = logEvt.Any(zerolog.ErrorFieldName, err)
}
logEvt.Msg("Portal message handler panicked")
}
}()
select {
case msg := <-portal.messages:
portal.handleMessageLoopItem(msg)
case msg := <-portal.matrixMessages:
portal.handleMatrixMessageLoopItem(msg)
}
}
}
func (portal *Portal) isOutgoingMessage(msg *gmproto.Message) *database.Message {