dotnet / dotnet/aspnetcore

Provide mechanism to make socket IO operations be initiated on IO Thread

Open
#13,723 5 comments 0 reactions 0 assignees View on GitHub
affected-very-few area-networking enhancement feature-kestrel Needs: Design Perf severity-nice-to-have
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is your feature request related to a problem? Please describe.
On Windows, when an async IO operation is initiated, the initiating thread owns the operation. If the thread is exited, any pending IO operations initiated on that thread are cancelled. When the .NET Threadpool is pruning idle threads from the thread pool, it checks to see if there's any pending IO outstanding that belongs to the thread and will wait for that IO to complete before pruning the thread. By calling receive on a random worker thread, you can potentially cause that thread to live a long time.
By calling the IO socket operation on an IO threadpool thread, you limit the threads being kept alive to IO threads which don't have the work queue issues that a large worker thread pool suffers from.

CoreWCF with it's NetTcp transport maintains an outstanding receive for each client which only completes when the client sends another request or our Receive timeout is hit. The default Receive timeout for WCF is 10 minutes although this can be set a lot higher. Under the right (or wrong depending on your viewpoint) conditions, this can cause the worker threadpool to be artificially larger than necessary.
A pure AspNetCore scenario which could be affected by this is an EventSource endpoint. The socket could be open for an extended period of time and the pending receive to detect the FIN/RST on the socket when the client is completed could cause extra threads to be kept alive.

### Describe the solution you'd like
Because thread hopping is more expensive, this should be opt-in so only those affected would need to enable this. I don't have a preference for what the solution should look like.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.