🐛 fix(megadebrid): deduplicate parallel connectUser API calls

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 <noreply@anthropic.com>
This commit is contained in:
Sucukdeluxe 2026-03-06 10:57:10 +01:00
parent 6d5fb27c99
commit eb5d960e81

View File

@ -679,6 +679,8 @@ class MegaDebridClient {
private static cachedApiTokenAt = 0;
private static pendingConnect: Promise<string | null> | 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<string | null> {
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 },