Repository navigation
Fix feature factory deadlock during lazy selection-set compilation - #10168
Merged
michaelstaib merged 2 commits intoAug 3, 2026
Merged
michaelstaib merged 2 commits into
michaelstaib merged 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a deadlock risk in OperationFeatureCollection by ensuring GetOrSetSafe executes feature factories outside the collection’s _writeLock, only taking the lock to store the computed value (double-checking for an existing value first). This aligns the behavior with ConcurrentDictionary.GetOrAdd semantics under contention.
Changes:
- Run
GetOrSetSafefactories outside_writeLockfor both operation-level and selection-level feature storage, then lock only to publish the winning instance. - Add concurrency-focused tests to ensure factories aren’t invoked while holding the write lock and that racing callers observe the same stored instance.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/HotChocolate/Core/src/Types/Execution/Processing/OperationFeatureCollection.cs | Updates operation-level GetOrSetSafe to invoke factories outside _writeLock and publish the first-created value. |
| src/HotChocolate/Core/src/Types/Execution/Processing/OperationFeatureCollection.Selections.cs | Applies the same “factory outside lock” pattern for selection-scoped features. |
| src/HotChocolate/Core/test/Execution.Tests/Processing/OperationFeatureCollectionTests.cs | Adds tests intended to validate no lock is held during factory execution and that racing callers return the same stored instance. |
Suppressed comments (1)
src/HotChocolate/Core/test/Execution.Tests/Processing/OperationFeatureCollectionTests.cs:65
- Same issue as the selection-factory test: the waits inside the factory don’t assert success. If the writer only completes after the factory returns (because the write lock was held), the event may still be set before the outer assertions, letting the test pass even though the lock was held during factory execution.
concurrentWriteFinished.Wait(s_timeout);
writer.Wait(s_timeout);
return new Feature();
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
michaelstaib
self-requested a review
August 3, 2026 14:32
michaelstaib
force-pushed
the
fix/operation-feature-collection-deadlock
branch
from
August 3, 2026 14:33
84ceb8a to
52d93e5
Compare
GetOrSetSafe invoked feature factories while holding the operation feature collection's write lock. Feature factories reach back into the operation: building a projection selector or computing connection flags calls Operation.GetSelectionSet, which takes the operation lock for lazy selection set compilation, while a thread compiling a selection set holds the operation lock and writes selection features, which takes the write lock. Taken in opposite orders, the two locks deadlock the request permanently. The factory now runs outside the lock. The result is stored under the lock only if no other caller stored a value first, and every caller observes the stored instance. This makes the write lock a leaf lock, so the lock order inversion is no longer possible. Under contention a factory can run more than once; all current factories are idempotent and the contract is documented on the public overloads. Fixes ChilliCream#10116.
michaelstaib
force-pushed
the
fix/operation-feature-collection-deadlock
branch
from
August 3, 2026 14:44
52d93e5 to
ef89fb8
Compare
This was referenced Aug 6, 2026
Merged
This was referenced Aug 13, 2026
Closed
Closed
This was referenced Sep 7, 2026
Closed
This was referenced Sep 15, 2026
Closed
[nuget][SUI_Matcher]- Bump the sui-package-updates group with 20 updates
DFE-Digital/SUI_Matcher#405
Closed
This was referenced Sep 22, 2026
Closed
Closed
[nuget][SUI_Matcher]- Bump the sui-package-updates group with 20 updates
DFE-Digital/SUI_Matcher#410
Merged
This was referenced Oct 5, 2026
This was referenced Oct 19, 2026
This was referenced Oct 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.
Fixes the permanent deadlock reported in #10116 between
OperationFeatureCollection._writeLockandOperation._sync.GetOrSetSafeinvoked feature factories while holding the collection's write lock. Feature factories reach back into the operation: building a projection selector viaAsSelector, or computingConnectionFlags, callsOperation.GetSelectionSet, which takes the operation lock for lazy selection set compilation. Meanwhile a thread compiling a selection set holds the operation lock and writes selection features through the write lock. Two threads taking the locks in opposite orders deadlock the request permanently, and on a shared executor the wedged threads starve subsequent requests.All four factory-invoking
GetOrSetSafeoverloads now run the factory outside the lock and store the result first-wins under the lock: under contention a factory can run more than once, only one instance is stored, and every caller observes that instance. This makes the write lock a leaf lock, so the lock order inversion is structurally impossible. No new fields and no per-feature gate; a losing result is simply discarded. All current factories (selector expressions, the projection selector cache lookup, connection flags) are idempotent, and the contract is documented on the public overloads.The regression test reproduces the exact two-lock race with a real operation and a sequencing selection set optimizer. It deadlocked into a timeout before the fix and passes now.
Fixes #10116