Skip to content

Commit

Permalink
fix(connection): close underlying socket (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
xdmytrox authored Oct 2, 2023
1 parent 52d7997 commit 7efd728
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
7 changes: 5 additions & 2 deletions gramjs/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion gramjs/client/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions gramjs/network/connection/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Connection {

this._connected = false;
void this._recvArray.push(undefined);
await this.socket.close();
}

async send(data: Buffer) {
Expand Down

0 comments on commit 7efd728

Please sign in to comment.