Add timeouts for portal event handlers

This commit is contained in:
Tulir Asokan 2024-03-20 13:05:25 +02:00
parent dad3dbc7e5
commit 473c9d59f9

View file

@ -307,7 +307,22 @@ func (portal *Portal) handleMessageLoopItem(msg PortalMessage) {
defer portal.forwardBackfillLock.Unlock() defer portal.forwardBackfillLock.Unlock()
switch { switch {
case msg.evt != nil: case msg.evt != nil:
doneChan := make(chan struct{})
go func() {
defer close(doneChan)
portal.handleMessage(msg.source, msg.evt, msg.raw) portal.handleMessage(msg.source, msg.evt, msg.raw)
}()
timer := time.NewTimer(1 * time.Minute)
select {
case <-doneChan:
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
portal.zlog.Error().
Str("message_id", msg.evt.MessageID).
Msg("Google Messages event handling is taking over a minute, unblocking loop")
}
default: default:
portal.zlog.Warn().Interface("portal_message", msg).Msg("Unexpected PortalMessage with no message") portal.zlog.Warn().Interface("portal_message", msg).Msg("Unexpected PortalMessage with no message")
} }
@ -330,6 +345,9 @@ func (portal *Portal) handleMatrixMessageLoopItem(msg PortalMatrixMessage) {
portalQueue: time.Since(msg.receivedAt), portalQueue: time.Since(msg.receivedAt),
totalReceive: time.Since(evtTS), totalReceive: time.Since(evtTS),
} }
doneChan := make(chan struct{})
go func() {
defer close(doneChan)
switch msg.evt.Type { switch msg.evt.Type {
case event.EventMessage, event.EventSticker: case event.EventMessage, event.EventSticker:
portal.HandleMatrixMessage(msg.user, msg.evt, timings) portal.HandleMatrixMessage(msg.user, msg.evt, timings)
@ -342,6 +360,18 @@ func (portal *Portal) handleMatrixMessageLoopItem(msg PortalMatrixMessage) {
Str("event_type", msg.evt.Type.Type). Str("event_type", msg.evt.Type.Type).
Msg("Unsupported event type in portal message channel") Msg("Unsupported event type in portal message channel")
} }
}()
timer := time.NewTimer(1 * time.Minute)
select {
case <-doneChan:
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
portal.zlog.Error().
Stringer("event_id", msg.evt.ID).
Msg("Matrix event handling is taking over a minute, unblocking loop")
}
} }
func (portal *Portal) handleMessageLoop() { func (portal *Portal) handleMessageLoop() {