From 760d8ce94ffcb944bccb4b9bf6b8e87169269f36 Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Tue, 30 Apr 2024 09:00:02 +0300 Subject: [PATCH 1/2] Drop ping envelopes when disconnecting the rmb client --- packages/rmb_direct_client/lib/client.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/rmb_direct_client/lib/client.ts b/packages/rmb_direct_client/lib/client.ts index c73079b1b7..e7d7b3f06f 100644 --- a/packages/rmb_direct_client/lib/client.ts +++ b/packages/rmb_direct_client/lib/client.ts @@ -185,9 +185,17 @@ class Client { } } + private dropPingEnvelope() { + for (const id of this.responses.keys()) { + const envelope = this.responses.get(id); + if (envelope?.has_ping) this.responses.delete(id); + } + } + private async waitForResponses(timeoutInSeconds = 2 * 60): Promise { const start = new Date().getTime(); while (new Date().getTime() < start + timeoutInSeconds * 1000) { + this.dropPingEnvelope(); if (this.responses.size === 0) return; this.logPendingResponses(); await new Promise(f => setTimeout(f, 1000)); From b012532c761e1fbff09f6ef964bd150788f05a1a Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Tue, 30 Apr 2024 09:02:35 +0300 Subject: [PATCH 2/2] Wait for the connection to be opened in case it's in opening state before closing it --- packages/rmb_direct_client/lib/client.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/rmb_direct_client/lib/client.ts b/packages/rmb_direct_client/lib/client.ts index e7d7b3f06f..4add51ff9d 100644 --- a/packages/rmb_direct_client/lib/client.ts +++ b/packages/rmb_direct_client/lib/client.ts @@ -204,6 +204,7 @@ class Client { } async disconnect() { + if (this.con.readyState === this.con.CONNECTING) await this.waitForOpenConnection(); if (this.__pingPongTimeout) clearTimeout(this.__pingPongTimeout); this.con.removeAllListeners(); await this.waitForResponses();