From bb54746ccf578b487ed3ce49f2df6aabdc467ea5 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Tue, 27 Jun 2023 15:12:16 +0200 Subject: [PATCH] fix(backend): prevent realtime connections getting prepared for closed websocket The setAdapter function checks if the websocket is closed. If this is the case then an error is thrown and the whole process will be canceled. If the adapter isn't set before the realtime connection object is prepared then the connection will subscribe to all the events and THEN the process will be canceled. Because the MessageTransporter has no adapter (and won't get one), the connection will never get a disconnect event and clean up. This causes the flood of "cant send message over closed websocket" messages. Signed-off-by: Tilman Vatteroth --- backend/src/realtime/websocket/websocket.gateway.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/src/realtime/websocket/websocket.gateway.ts b/backend/src/realtime/websocket/websocket.gateway.ts index 8fc5491c0..8a72bac9e 100644 --- a/backend/src/realtime/websocket/websocket.gateway.ts +++ b/backend/src/realtime/websocket/websocket.gateway.ts @@ -87,20 +87,22 @@ export class WebsocketGateway implements OnGatewayConnection { await this.realtimeNoteService.getOrCreateRealtimeNote(note); const websocketTransporter = new MessageTransporter(); + websocketTransporter.setAdapter( + new BackendWebsocketAdapter(clientSocket), + ); + const permissions = await this.noteService.toNotePermissionsDto(note); const acceptEdits: boolean = userCanEdit( permissions as NotePermissions, user?.username, ); + const connection = new RealtimeConnection( websocketTransporter, user, realtimeNote, acceptEdits, ); - websocketTransporter.setAdapter( - new BackendWebsocketAdapter(clientSocket), - ); realtimeNote.addClient(connection);