diff --git a/src/lib/gramjs/client/TelegramClient.js b/src/lib/gramjs/client/TelegramClient.js index 08512e2dd..59f07b555 100644 --- a/src/lib/gramjs/client/TelegramClient.js +++ b/src/lib/gramjs/client/TelegramClient.js @@ -22,7 +22,10 @@ const { } = require('./auth'); const { downloadFile } = require('./downloadFile'); const { uploadFile } = require('./uploadFile'); -const { updateTwoFaSettings, getTmpPassword } = require('./2fa'); +const { + updateTwoFaSettings, + getTmpPassword, +} = require('./2fa'); const DEFAULT_DC_ID = 2; const WEBDOCUMENT_DC_ID = 4; @@ -372,6 +375,24 @@ class TelegramClient { await sender.disconnect(); } + async _cleanupExportedSenders(dcId) { + const promises = Object.values(this._exportedSenderPromises[dcId]); + if (!promises.length) { + return; + } + + if (this.session.dcId !== dcId) { + this.session.setAuthKey(undefined, dcId); + } + + this._exportedSenderPromises[dcId] = {}; + + await Promise.all(promises.map(async (promise) => { + const sender = await promise; + await sender.disconnect(); + })); + } + async _connectSender(sender, dcId, isPremium = false) { // if we don't already have an auth key we want to use normal DCs not -1 let hasAuthKey = Boolean(sender.authKey.getKey()); diff --git a/src/lib/gramjs/client/downloadFile.ts b/src/lib/gramjs/client/downloadFile.ts index ae16525ba..ee2261c06 100644 --- a/src/lib/gramjs/client/downloadFile.ts +++ b/src/lib/gramjs/client/downloadFile.ts @@ -101,10 +101,14 @@ export async function downloadFile( try { return await downloadFile2(client, inputLocation, fileParams); } catch (err: any) { - if (i === SENDER_RETRIES - 1 || !err.message.startsWith('SESSION_REVOKED')) { + if ( + (err.message.startsWith('SESSION_REVOKED') || err.message.startsWith('CONNECTION_NOT_INITED')) + && i < SENDER_RETRIES - 1 + ) { + await client._cleanupExportedSenders(dcId); + } else { throw err; } - await client._cleanupExportedSender(dcId); } } @@ -215,7 +219,7 @@ async function downloadFile2( precise: isPrecise || undefined, })), sleep(SENDER_TIMEOUT).then(() => { - // if we're on the main DC we just cancel the download and let the user retry later. + // If we're on the main DC we just cancel the download and let the user retry later if (dcId === client.session.dcId) { return Promise.reject(new Error('USER_CANCELED')); } else {