Reduce thread pool depletion issues on modern .NET#13
Merged
centeredgebot[bot] merged 2 commits intomainfrom Sep 29, 2025
Merged
Reduce thread pool depletion issues on modern .NET#13centeredgebot[bot] merged 2 commits intomainfrom
centeredgebot[bot] merged 2 commits intomainfrom
Conversation
Motivation ---------- .NET 6 and later includes special logic to recognize when a thread pool thread is blocked waiting on a Task to complete, which is generally also waiting on a thread pool thread. When safe to do so, we should block on the task directly rather than using ExclusiveSynchronizationContext so that .NET can recognize the case and grow the thread pool more aggressively. This also reduces other overhead as well, improving overall performance. Modifications ------------- When RunSync is called with no or the default SynchronizationContext and no custom TaskScheduler, just block directly on the returned awaiter rather than constructing an ExclusiveSynchronizationContext.
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the AsyncHelper.RunSync methods to avoid thread pool depletion issues in .NET 6+ by detecting when it's safe to block directly on tasks without using ExclusiveSynchronizationContext.
- Added a
IsDeadlockSafemethod to determine when direct task blocking is safe - Modified all
RunSyncoverloads to use direct awaiter blocking when deadlock-safe conditions are met - Added comprehensive unit tests to verify the thread pool depletion improvements
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/CenterEdge.Async/AsyncHelper.cs | Implements the core optimization logic with deadlock safety checks and direct task blocking |
| src/CenterEdge.Async.UnitTests/ThreadPoolDepletionTests.cs | Adds test coverage for thread pool depletion scenarios with custom synchronization context |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
RobRobertsCE
approved these changes
Sep 29, 2025
Contributor
Author
|
/merge |
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.
Motivation
.NET 6 and later includes special logic to recognize when a thread pool thread is blocked waiting on a Task to complete, which is generally also waiting on a thread pool thread. When safe to do so, we should block on the task directly rather than using
ExclusiveSynchronizationContext so that .NET can recognize the case and grow the thread pool more aggressively. This also reduces other overhead as well, improving overall performance.
Modifications
When RunSync is called with no or the default SynchronizationContext and no custom TaskScheduler, just block directly on the returned awaiter rather than constructing an ExclusiveSynchronizationContext.