From 52d8ddae32ac87988e8dccf3770a7289fbaa5270 Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Thu, 19 Mar 2026 18:06:20 -0300 Subject: [PATCH] fix: prevent double prekey upload when concurrent uploads race on count=0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit uploadPreKeys() waited for a concurrent upload but then proceeded to upload again. With top-up logic and count=0, both handleEncryptNotification and uploadPreKeysToServerIfRequired fire simultaneously, both see count=0, first wins and uploads 800, second waits then uploads another 800 → 1600 prekeys. Fix: return immediately after awaiting the concurrent upload — it already replenished the pool. Co-Authored-By: Claude Sonnet 4.6 --- src/Socket/socket.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Socket/socket.ts b/src/Socket/socket.ts index a4fc06f7..f4080284 100644 --- a/src/Socket/socket.ts +++ b/src/Socket/socket.ts @@ -701,10 +701,12 @@ export const makeSocket = (config: SocketConfig) => { } } - // Prevent multiple concurrent uploads + // Prevent multiple concurrent uploads — if one is already running, wait for it and return: + // the concurrent upload already replenished the pool, so there is nothing left to do. if (uploadPreKeysPromise) { logger.debug('Pre-key upload already in progress, waiting for completion') await uploadPreKeysPromise + return } const uploadLogic = async () => {