Occasionally client.connect() errors with Request failed - server disconnected milliseconds after initiating the connection.
Root cause
This error occurs because for a short lived time a socket is established on the server but the nes "hello" handshake message has not been processed so socket._active() will return false due to the fact that this._pinged is false. Once the "hello" message is processed the socket is correctly identified as active.
If the beat timeout occurs between the websocket being added to to the sockets collection but before the "hello" is processed the beat timeout logic will disconnect the socket.
Proposed fix
The proposed fix is to allow a grace period in which newly created sockets are not disconnected by the beat timeout.
Testing
Being a race condition it is difficult to test but the unit test added will fail repeatedly if my logic is removed.
Issue
Occasionally
client.connect()
errors withRequest failed - server disconnected
milliseconds after initiating the connection.Root cause
This error occurs because for a short lived time a socket is established on the server but the
nes
"hello" handshake message has not been processed sosocket._active()
will returnfalse
due to the fact thatthis._pinged
is false. Once the "hello" message is processed the socket is correctly identified as active.If the beat timeout occurs between the websocket being added to to the sockets collection but before the "hello" is processed the beat timeout logic will disconnect the socket.
Proposed fix
The proposed fix is to allow a grace period in which newly created sockets are not disconnected by the beat timeout.
Testing
Being a race condition it is difficult to test but the unit test added will fail repeatedly if my logic is removed.