Skip to content

AwarenessUpdate updates triggers close on a closed/closing socket - buggy send message #1017

@udacosta-gp

Description

@udacosta-gp

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:

  1. Launch HP
  2. Connect to a document with 1+ users
  3. Disconnect a user
  4. 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();
    }
    }

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions