hapijs / nes

WebSocket adapter plugin for hapi routes
Other
502 stars 87 forks source link

Prevent beat timeout from disconnecting newly connecting sockets #304

Closed rankida closed 4 years ago

rankida commented 4 years ago

Issue

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.

rankida commented 4 years ago

I don't believe the CI unit test failures are related to my change. Linux Node 12 and node tests passing as is Window's node run.