From 456b01c88a896507372bf8ae0c1e85f6b4b515ae Mon Sep 17 00:00:00 2001 From: kassem Date: Thu, 15 Feb 2024 13:47:09 +0200 Subject: [PATCH] fix: disconnect the provider in catch block fixs #2092 --- packages/tfchain_client/src/client.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/tfchain_client/src/client.ts b/packages/tfchain_client/src/client.ts index ce139f1eb9..406792f7e6 100644 --- a/packages/tfchain_client/src/client.ts +++ b/packages/tfchain_client/src/client.ts @@ -81,6 +81,7 @@ class QueryClient { } async newProvider() { + let provider: WsProvider; try { await QueryClient.connectingLock.acquireAsync(); if (QueryClient.connections.has(this.url)) { @@ -89,12 +90,13 @@ class QueryClient { } await this.disconnect(); - const provider = new WsProvider(this.url); + provider = new WsProvider(this.url); this.api = await ApiPromise.create({ provider, throwOnConnect: true }); await this.wait(); QueryClient.connections.set(this.url, { api: this.api, disconnectHandler: this.__disconnectHandler }); this.api.on("disconnected", this.__disconnectHandler); } catch (e) { + if (provider) provider.disconnect(); const message = `Unable to establish a connection with the chain ${this.url} \n`; if (e instanceof BaseError) { e.message = message + e.message;