From bd837a638310693984a008ad24dbe8910192fa19 Mon Sep 17 00:00:00 2001 From: Zhihe Li Date: Tue, 5 May 2026 10:25:40 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Fix=20race=20condition=20?= =?UTF-8?q?from=20sqlite=20initialization=20(#3253)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/workers/utils/shared-service.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/addons/api/addon/workers/utils/shared-service.js b/addons/api/addon/workers/utils/shared-service.js index fd6b43917..8cc12e1c4 100644 --- a/addons/api/addon/workers/utils/shared-service.js +++ b/addons/api/addon/workers/utils/shared-service.js @@ -169,6 +169,12 @@ const onBecomeProvider = async ({ target, status, }) => { + // Create a gate that prevents request handling until the provider is fully initialized. + // This prevents a race condition where a client registers and sends requests before + // the provider has finished initialization. + const { promise: providerReady, resolve: providerReadyResolve } = + Promise.withResolvers(); + // Set an event listener for other tabs to register with the provider tab commonChannel.addEventListener('message', async (event) => { const { clientId, type } = event.data; @@ -194,6 +200,9 @@ const onBecomeProvider = async ({ } const { method, args, id } = data; + // Wait for the provider to finish initialization before handling requests + await providerReady; + let result, error; try { result = await target[method](...args); @@ -221,6 +230,7 @@ const onBecomeProvider = async ({ }); await onProviderChange?.(status.isServiceProvider); + providerReadyResolve(); commonChannel.postMessage({ type: 'providerChange', serviceName }); // If we became the new provider and there were requests in flight, request it directly again.