-
-
Notifications
You must be signed in to change notification settings - Fork 185
Description
Description
Connection send method calls this.close() when the connection is already closing or closed. Because a connection close triggers awareness updates, and awareness updates trigger send messages, close events repeat and call backs (like onDisconnect) get called multiple times. In my server implementation, the attempt to send a message to a closed socket generates an uncaught exception. In dev (localhost) a crash is not observed.
Steps to reproduce the bug
Steps to reproduce the behavior:
- Launch HP
- Connect to a document with 1+ users
- Disconnect a user
- Observe onDisconnect firing twice
Expected behavior
connection close only triggers onDisconnect once and does not attempt to send further messages to the disconnected socket.
Screenshot, video, or GIF
No screenshot but If you place this at the top of the Connection.ts close method you can see the multiple triggers of the close event firing (including awareness update)
console.log("connection close", new Error().stack);
Environment?
- MacOS
- Safari/Chrome
- Desktop:
- Hocuspocus version: 3.4.0
Additional context
/**
- Send the given message
*/
send(message: any): void {
if (
this.webSocket.readyState === WsReadyStates.Closing ||
this.webSocket.readyState === WsReadyStates.Closed
) {
this.close(); // <-- TODO: close on closed socket can cause crashes + trigger unwanted callbacks
return;
}
try {
this.webSocket.send(message, (error: any) => {
if (error != null) this.close();
});
} catch (exception) {
this.close();
}
}