From 63ba84a641a98690650db252d330bbc5e48602f6 Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 23 Mar 2022 15:09:25 +0100 Subject: [PATCH] Clear connection timeout on disconnect (#4681) --- packages/rpc-provider/src/ws/index.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/rpc-provider/src/ws/index.ts b/packages/rpc-provider/src/ws/index.ts index ac534a477d..b3f84bf59e 100644 --- a/packages/rpc-provider/src/ws/index.ts +++ b/packages/rpc-provider/src/ws/index.ts @@ -107,6 +107,8 @@ export class WsProvider implements ProviderInterface { #subscriptions: Record = {}; + #timeoutId?: NodeJS.Timeout | null = null; + #websocket: WebSocket | null; /** @@ -147,9 +149,6 @@ export class WsProvider implements ProviderInterface { resolve(this); }); }); - - // timeout any handlers that have not had a response - setInterval(() => this.#timeout(), TIMEOUT_INTERVAL); } /** @@ -209,6 +208,9 @@ export class WsProvider implements ProviderInterface { this.#websocket.onerror = this.#onSocketError; this.#websocket.onmessage = this.#onSocketMessage; this.#websocket.onopen = this.#onSocketOpen; + + // timeout any handlers that have not had a response + this.#timeoutId = setInterval(() => this.#timeout(), TIMEOUT_INTERVAL); } catch (error) { l.error(error); @@ -414,6 +416,11 @@ export class WsProvider implements ProviderInterface { this.#websocket = null; } + if (this.#timeoutId) { + clearInterval(this.#timeoutId); + this.#timeoutId = null; + } + this.#emit('disconnected'); // reject all hanging requests