From 7efd72851ed6bbaa4c94ace7fc31fddf6d3fe9a2 Mon Sep 17 00:00:00 2001 From: Dmytro <8546158+DrachovDmytro@users.noreply.github.com> Date: Mon, 2 Oct 2023 11:49:05 +0200 Subject: [PATCH] fix(connection): close underlying socket (#588) --- gramjs/Helpers.ts | 7 +++++-- gramjs/client/updates.ts | 3 ++- gramjs/network/connection/Connection.ts | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/gramjs/Helpers.ts b/gramjs/Helpers.ts index 6c4c4c49..703be679 100644 --- a/gramjs/Helpers.ts +++ b/gramjs/Helpers.ts @@ -426,10 +426,13 @@ export function getRandomInt(min: number, max: number): number { /** * Sleeps a specified amount of time * @param ms time in milliseconds + * @param isUnref make a timer unref'ed * @returns {Promise} */ -export const sleep = (ms: number) => - new Promise((resolve) => setTimeout(resolve, ms)); +export const sleep = (ms: number, isUnref: boolean = false) => + new Promise((resolve) => + isUnref ? setTimeout(resolve, ms).unref() : setTimeout(resolve, ms) + ); /** * Helper to export two buffers of same length diff --git a/gramjs/client/updates.ts b/gramjs/client/updates.ts index fb40a9fa..c7a544c7 100644 --- a/gramjs/client/updates.ts +++ b/gramjs/client/updates.ts @@ -187,7 +187,8 @@ export async function _dispatchUpdate( export async function _updateLoop(client: TelegramClient) { let lastPongAt; while (!client._destroyed) { - await sleep(PING_INTERVAL); + await sleep(PING_INTERVAL, true); + if (client._destroyed) break; if (client._sender!.isReconnecting || client._isSwitchingDc) { lastPongAt = undefined; continue; diff --git a/gramjs/network/connection/Connection.ts b/gramjs/network/connection/Connection.ts index bc5d526a..6d51f341 100644 --- a/gramjs/network/connection/Connection.ts +++ b/gramjs/network/connection/Connection.ts @@ -103,6 +103,7 @@ class Connection { this._connected = false; void this._recvArray.push(undefined); + await this.socket.close(); } async send(data: Buffer) {