Skip to content

Reduce thread pool depletion issues on modern .NET#13

Merged
centeredgebot[bot] merged 2 commits intomainfrom
task-blocking
Sep 29, 2025
Merged

Reduce thread pool depletion issues on modern .NET#13
centeredgebot[bot] merged 2 commits intomainfrom
task-blocking

Conversation

@brantburnett
Copy link
Contributor

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.

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.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 IsDeadlockSafe method to determine when direct task blocking is safe
  • Modified all RunSync overloads 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.

@brantburnett brantburnett marked this pull request as ready for review September 29, 2025 13:47
@brantburnett
Copy link
Contributor Author

/merge

@centeredgebot centeredgebot bot merged commit f84e948 into main Sep 29, 2025
6 checks passed
@centeredgebot centeredgebot bot deleted the task-blocking branch September 29, 2025 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants