Fix service worker init race, selection capture deadlock, and post-logout singleton leak#32
Draft
Fix service worker init race, selection capture deadlock, and post-logout singleton leak#32
Conversation
… after logout Co-authored-by: numbers-official <181934381+numbers-official@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] [Security] Fix service worker initialization race condition
Fix service worker init race, selection capture deadlock, and post-logout singleton leak
Mar 13, 2026
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.
Three interconnected state management bugs in the service worker: message handlers could fire before storage was ready, a second selection capture would permanently hang the first, and
NumbersApiManager's lazy singleton persisted stale upload state across user sessions after logout.Changes
src/background/service-worker.tsInit readiness gate: Stored the top-level
Promise.all([assetStorage.init(), metadataStorage.init()])asinitPromise. EveryonMessagecase handler nowawait initPromisebefore touching storage, closing the race window on service worker wake-up:Selection deadlock prevention: In
handleSelectionCapture, any pre-existing pending selection promise is now explicitly rejected before overwriting the callbacks, so a second capture attempt doesn't strand the first caller indefinitely:Error visibility: Added
.catch()on theSELECTION_COMPLETEasync IIFE to surface errors rather than swallowing them silently.src/services/NumbersApiManager.tsSingleton reset on logout: Added
export function resetInstance()and called it fromclearAuth(). After logout the nextgetNumbersApi()call constructs a clean instance with a freshUploadServiceand no leftover callbacks from the prior session.Init-time
clearAuthguard:getNumbersApi()now handles the edge case whereinitialize()invalidates a stale stored token (callingclearAuth()internally, which resetsinstance). Ifinstanceis null afterinitialize(), a fresh unauthenticated manager is created and stored rather than returning a dereferenced null.Original prompt
📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.