Fix race conditions in queuePerItem promise lifecycle#67
Open
Fix race conditions in queuePerItem promise lifecycle#67
Conversation
- Race condition 1: register sentinel promise in state.promises before calling task() so concurrent same-key calls coalesce correctly even when they occur synchronously inside task() - Race condition 2: store the forceRefresh chained promise back into state.promises so subsequent callers see the refreshed promise Adds two targeted tests that directly validate each race condition fix. Co-authored-by: NiveditJain <40313233+NiveditJain@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix race conditions in queuePerItem promise lifecycle
Fix race conditions in queuePerItem promise lifecycle
Feb 26, 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.
Two race conditions in
queuePerItemcause promise coalescing to silently fail under concurrent access, producing duplicate task executions and stale results.Race Condition 1 — Registration gap before sentinel
state.promises.set(key, promise)was called aftertask()insiderunTask(). Any same-key call occurring synchronously withintask()would find no entry and spawn a duplicate task.Fix: Create a deferred sentinel promise and register it in
state.promisesbefore invokingtask(), then forward the task result to the sentinel:Race Condition 2 —
forceRefreshchained promise not storedWhen
forceRefresh: trueand a promise is in-flight, a chained promise was created and returned but never written back tostate.promises. The next caller withforceRefresh: falsewould coalesce onto the stale original instead of the refresh chain.Fix: One line — store the chained promise before returning:
Tests
Two new tests directly exercise each race condition, verifying no duplicate task execution and correct promise identity.
Type of Change
Checklist
npm run lintpassesnpx tsc --noEmitpassesnpm run test:runpassesnpm run buildsucceedsOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.