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