From eb5d960e81808146a603e3f16f52cb46ad2385d1 Mon Sep 17 00:00:00 2001 From: Sucukdeluxe Date: Fri, 6 Mar 2026 10:57:10 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(megadebrid):=20deduplicate?= =?UTF-8?q?=20parallel=20connectUser=20API=20calls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Multiple parallel downloads calling connectUser simultaneously caused token invalidation races. Only one connectUser request now runs at a time; all parallel callers share the same result. Co-Authored-By: Claude Opus 4.6 --- src/main/debrid.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/debrid.ts b/src/main/debrid.ts index 726f3f1..232960a 100644 --- a/src/main/debrid.ts +++ b/src/main/debrid.ts @@ -679,6 +679,8 @@ class MegaDebridClient { private static cachedApiTokenAt = 0; + private static pendingConnect: Promise | null = null; + public constructor(login: string, password: string, preferApi: boolean, megaWebUnrestrict?: MegaWebUnrestrictor) { this.login = login; this.password = password; @@ -692,6 +694,18 @@ class MegaDebridClient { return MegaDebridClient.cachedApiToken; } + // Deduplicate parallel connectUser calls — only one in-flight request at a time + if (MegaDebridClient.pendingConnect) { + return MegaDebridClient.pendingConnect; + } + + MegaDebridClient.pendingConnect = this.doConnectApi(signal).finally(() => { + MegaDebridClient.pendingConnect = null; + }); + return MegaDebridClient.pendingConnect; + } + + private async doConnectApi(signal?: AbortSignal): Promise { const url = `${MEGA_DEBRID_API_BASE}?action=connectUser&login=${encodeURIComponent(this.login)}&password=${encodeURIComponent(this.password)}`; const response = await fetch(url, { headers: { "User-Agent": DEBRID_USER_AGENT },