Terminate connection on timeout

If a connection has timed out the most likely cause is the connection
going stale, in which case calling close could take up to 30 seconds
before the ws lib gives up on waiting for the close message.  Terminate
the connection instead on the Node.js side.  Note that Firefox does not
permit applications from closing connections with a code other than
1000, or in the range 3000-4999.
This commit is contained in:
Hornwitser committed 2021-09-28 21:24:42 +02:00
1 parent f529457f96
commit 4a72c14b74
3 files changed
+10 -14

No files matched your search

+6 -1
View File
@@ -73,7 +73,12 @@ class WebSocketBaseConnector extends events.EventEmitter {
this._check("connected");
if (Date.now() - this._lastHeartbeat > 2000 * this._heartbeatInterval) {
logger.verbose("Connector | closing after heartbeat timed out");
this._socket.close(1008, "Heartbeat timeout");
// eslint-disable-next-line node/no-process-env
if (process.env.APP_ENV === "browser") {
this._socket.close(4008, "Heartbeat timeout");
} else {
this._socket.terminate();
}
} else {
this._socket.send(JSON.stringify({
+1 -1
View File
@@ -136,7 +136,7 @@ class WsServer {
let timeoutId = setTimeout(() => {
logger.verbose(`WsServer | closing ${req.socket.remoteAddress} after timing out on handshake`);
wsRejectedConnectionsCounter.inc();
socket.close(1008, "Handshake timeout");
socket.terminate();
this.pendingSockets.delete(socket);
}, 30*1000);
+3 -12
View File
@@ -73,19 +73,10 @@ class WsServerConnector extends libLink.WebSocketBaseConnector {
continue(socket, lastSeq) {
// It's possible the previous connection hasn't closed yet due to a
// stale connection. If this is the case the connector will have to
// wait for the previous socket to close down before it can continue
// from a new socket, or there will be overlapping events from both.
// stale connection. Terminate it if so.
if (this._state === "connected") {
this._socket.close(4003, "Session Hijacked");
// Correctly waiting for the connector to close and then
// handling all the edge cases like multiple continues happening
// in parallel or connections going stale is far too difficult.
// Kill the connection here and let the client reconnect later
// when the connector is not held up by an active connection.
socket.close(1013, "Session Busy");
socket.terminate();
this._socket.terminate();
this._socket.once("close", () => this.continue(socket, lastSeq));
return;
}