Fix crash from exceptions in handlers (#771)

* Fix crash from exceptions in handlers

* Fix withScope usage

* Add changelog
This commit is contained in:
Justin Carlson 2023-06-05 12:25:19 -04:00 committed by GitHub
parent 6ca2b51893
commit 96c398ce74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 17 deletions

1
changelog.d/771.bugfix Normal file
View File

@ -0,0 +1 @@
Fix crash when failing to handle events, typically due to lacking permissions to send messages in a room.

View File

@ -786,22 +786,24 @@ export class Bridge {
this.ready = true; this.ready = true;
} }
private handleHookshotEvent<EventType, ConnType extends IConnection>(msg: MessageQueueMessageOut<EventType>, connection: ConnType, handler: (c: ConnType, data: EventType) => Promise<unknown>|unknown) { private async handleHookshotEvent<EventType, ConnType extends IConnection>(msg: MessageQueueMessageOut<EventType>, connection: ConnType, handler: (c: ConnType, data: EventType) => Promise<unknown>|unknown) {
Sentry.withScope((scope) => { try {
scope.setTransactionName('handleHookshotEvent'); await handler(connection, msg.data);
scope.setTags({ } catch (e) {
eventType: msg.eventName, Sentry.withScope((scope) => {
roomId: connection.roomId, scope.setTransactionName('handleHookshotEvent');
}); scope.setTags({
scope.setContext("connection", { eventType: msg.eventName,
id: connection.connectionId, roomId: connection.roomId,
}); });
new Promise(() => handler(connection, msg.data)).catch((ex) => { scope.setContext("connection", {
Sentry.captureException(ex, scope); id: connection.connectionId,
});
log.warn(`Connection ${connection.toString()} failed to handle ${msg.eventName}:`, e);
Metrics.connectionsEventFailed.inc({ event: msg.eventName, connectionId: connection.connectionId }); Metrics.connectionsEventFailed.inc({ event: msg.eventName, connectionId: connection.connectionId });
log.warn(`Connection ${connection.toString()} failed to handle ${msg.eventName}:`, ex); Sentry.captureException(e, scope);
}); });
}); }
} }
private async bindHandlerToQueue<EventType, ConnType extends IConnection>(event: string, connectionFetcher: (data: EventType) => ConnType[], handler: (c: ConnType, data: EventType) => Promise<unknown>|unknown) { private async bindHandlerToQueue<EventType, ConnType extends IConnection>(event: string, connectionFetcher: (data: EventType) => ConnType[], handler: (c: ConnType, data: EventType) => Promise<unknown>|unknown) {
@ -810,8 +812,8 @@ export class Bridge {
const connections = connectionFetcherBound(msg.data); const connections = connectionFetcherBound(msg.data);
log.debug(`${event} for ${connections.map(c => c.toString()).join(', ') || '[empty]'}`); log.debug(`${event} for ${connections.map(c => c.toString()).join(', ') || '[empty]'}`);
connections.forEach((connection) => { connections.forEach((connection) => {
this.handleHookshotEvent(msg, connection, handler); void this.handleHookshotEvent(msg, connection, handler);
}) });
}); });
} }
@ -1167,7 +1169,7 @@ export class Bridge {
} }
for (const connection of this.connectionManager.getAllConnectionsForRoom(roomId)) { for (const connection of this.connectionManager.getAllConnectionsForRoom(roomId)) {
if (!connection.onEvent) { if (!connection.onEvent) {
continue; continue;
} }
const scope = new Sentry.Scope(); const scope = new Sentry.Scope();