Skip to content

fix(sio-client): disconnect on the server side when disconnect() is called during the handshake - #5632

Open
vladosskins178 wants to merge 1 commit into
socketio:mainfrom
vladosskins178:fix/disconnect-during-handshake
Open

vladosskins178 wants to merge 1 commit into
socketio:mainfrom
vladosskins178:fix/disconnect-during-handshake

Conversation

@vladosskins178

Copy link
Copy Markdown

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

If disconnect() is called after the client sent the CONNECT packet of a namespace, but before the server answered, the client sends nothing. destroy() removes the packet listener, so the CONNECT reply is ignored. The server completes the handshake and keeps that socket (rooms, handlers) until the whole connection closes (#5617).

New behavior

This implements the approach suggested in the issue. The DISCONNECT packet can't be sent right away, because the server has no socket for the namespace yet and would close the whole connection. So when disconnect() is called during the handshake, the socket adds a one-off listener on the Manager:

  • on the CONNECT reply of this namespace, it sends a DISCONNECT packet;
  • on CONNECT_ERROR, or when the Manager closes, it only removes itself.

If connect() is called again before the reply arrives, the listener is removed. The socket then waits for the reply to the CONNECT packet it already sent, instead of sending a second one. Today that case sends a second CONNECT: the server creates two sockets and connect fires twice.

If the socket was the only one, the Manager closes the connection as before (nothing changes there).

Tests (in test/socket.ts, with a /slow-handshake namespace whose middleware answers after 100 ms):

  • "should disconnect on the server side once the server answers the CONNECT packet": fails on main (the server still has the socket), passes with this change.
  • "should connect once when reconnected before the server answers the CONNECT packet": fails on main (connect fires 2 times), passes with this change.
  • "should close the connection when the socket was the only one": passes on both (guards the existing behaviour).

Run locally (Node.js 24.14, Linux):

  • socket.io-client test:node: 118 passing. format:check and compile pass.
  • socket.io (server) test/index.ts, which uses this client: 219 passing.
  • The repro script from the issue (delays 200 and 0 ms): server /foo sockets goes from 1 to 0, and the root namespace stays connected.

Both suites were rerun from a fresh clone of the branch with the same results. I didn't run the browser tests.

Other information (e.g. related issues)

Closes #5617

Related: #5623 (open) stops connect() from sending a second CONNECT while the socket is still active. This PR covers disconnect() during the handshake, and disconnect() followed by connect(), where the socket is no longer active. They touch connect() in different branches.

…alled during the handshake

When disconnect() was called after the CONNECT packet was sent but before
the server answered, nothing was sent to the server, and the reply was
ignored since the socket was no longer active. The server then kept a
socket that the client had closed.

The DISCONNECT packet cannot be sent right away (the server has no socket
for the namespace yet and would close the whole connection), so it is now
sent once the server has answered the CONNECT packet. Calling connect()
again before that answer waits for it instead of sending a second CONNECT
packet.

Related: socketio#5617
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[socket.io-client] disconnect() during the namespace handshake leaves the socket connected on the server

1 participant