From 34b7f4e1f840b9c47072d8e11fe24f5e0ecf25b8 Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Mon, 18 Aug 2025 06:04:26 -0700 Subject: [PATCH] Chore: Sync fuzzer: Fix "DecryptionWorker: Cannot start because..." warning (#12925) --- packages/lib/services/DecryptionWorker.ts | 38 +++++++++++++---------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/lib/services/DecryptionWorker.ts b/packages/lib/services/DecryptionWorker.ts index 530eccd4f4..41e9100cf5 100644 --- a/packages/lib/services/DecryptionWorker.ts +++ b/packages/lib/services/DecryptionWorker.ts @@ -8,6 +8,7 @@ import shim from '../shim'; import KvStore from './KvStore'; import EncryptionService from './e2ee/EncryptionService'; import PerformanceLogger from '../PerformanceLogger'; +import AsyncActionQueue from '../AsyncActionQueue'; const EventEmitter = require('events'); const perfLogger = PerformanceLogger.create(); @@ -44,7 +45,7 @@ export default class DecryptionWorker { private eventEmitter_: any; private kvStore_: KvStore = null; private maxDecryptionAttempts_ = 2; - private startCalls_: boolean[] = []; + private taskQueue_: AsyncActionQueue = new AsyncActionQueue(); private encryptionService_: EncryptionService = null; public constructor() { @@ -328,15 +329,25 @@ export default class DecryptionWorker { } // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - public async start(options: any = {}) { - this.startCalls_.push(true); - const startTask = perfLogger.taskStart('DecryptionWorker/start'); + public async start(options: any = {}): Promise { let output = null; - try { - output = await this.start_(options); - } finally { - this.startCalls_.pop(); - startTask.onEnd(); + let lastError: Error; + + // Use taskQueue_ to ensure that only one decryption task is running at a time. + this.taskQueue_.push(async () => { + const startTask = perfLogger.taskStart('DecryptionWorker/start'); + try { + output = await this.start_(options); + } catch (error) { + lastError = error; + } finally { + startTask.onEnd(); + } + }); + await this.taskQueue_.processAllNow(); + + if (lastError) { + throw lastError; } return output; } @@ -350,13 +361,6 @@ export default class DecryptionWorker { this.eventEmitter_ = null; DecryptionWorker.instance_ = null; - return new Promise((resolve) => { - const iid = shim.setInterval(() => { - if (!this.startCalls_.length) { - shim.clearInterval(iid); - resolve(null); - } - }, 100); - }); + await this.taskQueue_.waitForAllDone(); } }