fix(worker): register leader-now listener early to prevent race condition#890
Open
kaigritun wants to merge 1 commit intoelectric-sql:mainfrom
Open
fix(worker): register leader-now listener early to prevent race condition#890kaigritun wants to merge 1 commit intoelectric-sql:mainfrom
kaigritun wants to merge 1 commit intoelectric-sql:mainfrom
Conversation
…tion Fixes electric-sql#850 The 'leader-now' message from the worker could be lost if the main thread was still blocked (e.g., awaiting acquireLock) when the message arrived. This happened because the listener was registered after several async operations that could yield control. By moving the listener registration to immediately after the worker signals it's ready (and before any other async operations), we ensure the message is never missed, regardless of main thread load.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #850
Problem
There is a race condition in
PGliteWorkerleader election when the main thread is under heavy load (e.g., during React hydration or complex app initialization).The
leader-nowmessage sent by the worker immediately afterreadycould be lost because the main thread was still blocked or had not yet attached themessageevent listener.This resulted in
isLeaderremainingfalseindefinitely, even though the worker successfully acquired the lock and was acting as the leader.Root Cause
The listener for
leader-nowwas registered after several async operations:await acquireLock(tabCloseLockId)- yields the main threadDuring this time, the worker could send
leader-nowbefore the listener was in place.Fix
Move the
leader-nowlistener registration to immediately afterworkerReadyPromiseresolves, before any async operations that could yield control.This ensures the listener is in place before the worker has any chance to send the message.
Testing
awaitthat could allow the worker to send the message