From f73b470ec0a7787c6b2e070319ed4e7d117b0b91 Mon Sep 17 00:00:00 2001 From: marty-mcflai <88204159+marty-mcflai@users.noreply.github.com> Date: Fri, 4 Feb 2022 12:21:58 +0100 Subject: [PATCH] fix: swallow error if socket has already been closed (#22934) * Swallow error if socket has already been closed * fix: log error --- web3.js/src/connection.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 7b55f16055..e97db4a06e 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -4110,7 +4110,16 @@ export class Connection { this._rpcWebSocketConnected = false; this._rpcWebSocketIdleTimeout = setTimeout(() => { this._rpcWebSocketIdleTimeout = null; - this._rpcWebSocket.close(); + try { + this._rpcWebSocket.close(); + } catch (err) { + // swallow error if socket has already been closed. + if (err instanceof Error) { + console.log( + `Error when closing socket connection: ${err.message}`, + ); + } + } }, 500); } return;