Skip to content

Fix feature factory deadlock during lazy selection-set compilation - #10168

Merged
michaelstaib merged 2 commits into
ChilliCream:mainfrom
apereiratl:fix/operation-feature-collection-deadlock
Aug 3, 2026
Merged

michaelstaib merged 2 commits into
ChilliCream:mainfrom
apereiratl:fix/operation-feature-collection-deadlock

Conversation

@apereiratl

@apereiratl apereiratl commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the permanent deadlock reported in #10116 between OperationFeatureCollection._writeLock and Operation._sync.

GetOrSetSafe invoked feature factories while holding the collection's write lock. Feature factories reach back into the operation: building a projection selector via AsSelector, or computing ConnectionFlags, calls Operation.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 GetOrSetSafe overloads 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GetOrSetSafe factories outside _writeLock for 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
michaelstaib self-requested a review August 3, 2026 14:32
@michaelstaib
michaelstaib force-pushed the fix/operation-feature-collection-deadlock branch from 84ceb8a to 52d93e5 Compare August 3, 2026 14:33
@michaelstaib michaelstaib changed the title Run feature factories outside the OperationFeatureCollection write lock Fix feature factory deadlock during lazy selection-set compilation Aug 3, 2026
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
michaelstaib force-pushed the fix/operation-feature-collection-deadlock branch from 52d93e5 to ef89fb8 Compare August 3, 2026 14:44
@michaelstaib
michaelstaib merged commit 96f7f69 into ChilliCream:main Aug 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deadlock between projection selector creation and lazy selection-set compilation

3 participants