From a0a13b3a1b5b654ad36a25c4785cab539555840a Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Wed, 8 Jun 2022 17:52:23 +0200 Subject: [PATCH] fix(http/upgradewebsocket): check for open state for idle timeout (#14813) --- ext/websocket/01_websocket.js | 48 ++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index 3585256fc6..8c407dff8d 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -481,28 +481,36 @@ if (this[_idleTimeoutDuration]) { clearTimeout(this[_idleTimeoutTimeout]); this[_idleTimeoutTimeout] = setTimeout(async () => { - await core.opAsync("op_ws_send", this[_rid], { - kind: "ping", - }); - this[_idleTimeoutTimeout] = setTimeout(async () => { - this[_readyState] = CLOSING; - const reason = "No response from ping frame."; - await core.opAsync("op_ws_close", this[_rid], 1001, reason); - this[_readyState] = CLOSED; - - const errEvent = new ErrorEvent("error", { - message: reason, + if (this[_readyState] === OPEN) { + await core.opAsync("op_ws_send", this[_rid], { + kind: "ping", }); - this.dispatchEvent(errEvent); + this[_idleTimeoutTimeout] = setTimeout(async () => { + if (this[_readyState] === OPEN) { + this[_readyState] = CLOSING; + const reason = "No response from ping frame."; + await core.opAsync("op_ws_close", this[_rid], 1001, reason); + this[_readyState] = CLOSED; - const event = new CloseEvent("close", { - wasClean: false, - code: 1001, - reason, - }); - this.dispatchEvent(event); - core.tryClose(this[_rid]); - }, (this[_idleTimeoutDuration] / 2) * 1000); + const errEvent = new ErrorEvent("error", { + message: reason, + }); + this.dispatchEvent(errEvent); + + const event = new CloseEvent("close", { + wasClean: false, + code: 1001, + reason, + }); + this.dispatchEvent(event); + core.tryClose(this[_rid]); + } else { + clearTimeout(this[_idleTimeoutTimeout]); + } + }, (this[_idleTimeoutDuration] / 2) * 1000); + } else { + clearTimeout(this[_idleTimeoutTimeout]); + } }, (this[_idleTimeoutDuration] / 2) * 1000); } }