-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Take steps to avoid threadpool starvation #11275
base: main
Are you sure you want to change the base?
Conversation
Related to #11160 |
/azp run |
Pull request contains merge conflicts. |
@@ -576,7 +575,7 @@ private enum ExitPacketState | |||
/// <summary> | |||
/// A queue used for enqueuing packets to write to the stream asynchronously. | |||
/// </summary> | |||
private BlockingCollection<INodePacket> _packetWriteQueue = new BlockingCollection<INodePacket>(); | |||
private ConcurrentQueue<INodePacket> _packetWriteQueue = new ConcurrentQueue<INodePacket>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any advantage for a ConcurrentQueue as opposed to using Channel?
I'm not saying that we should use channel - when I was researching this piece of code, I've stumbled onto it and it piqued my interest. Hence the question.
@@ -733,65 +732,63 @@ private void DrainPacketQueue() | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it time to consider having a separate thread for the drain? with the "new" await, it should not even require a dedicated thread. (unless I'm grossly misunderstanding something)
Building on the channel idea from previous comment:
We could set up a multi writer, one reader channel.
Then have one thread to drain the channel
e.g. while (await channelReader.WaitToReadAsync()) {doTheDraining()}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from that, the only place that is ever doing the Draining is this one:
public void SendData(INodePacket packet)
{
if (IsExitPacket(packet))
{
_exitPacketState = ExitPacketState.ExitPacketQueued;
}
_packetWriteQueue.Add(packet);
DrainPacketQueue();
}
it is also the only place that ever places packets into the Queue.
since we're already touching this place, I would like to take it further if possible.
Fixes #
Context
There are a handful of areas where threadpool threads are unnecessarily blocked by synchronous work, or there is additional contention that can be avoided. The most egregious instance is the synchronous read in BuferedReadStream
The outer call to BeginRead() ends up calling BufferedReadStream.Read() which synchronously blocks when _innerStream.Read() is called. This results in a substantial amount of block time for threadpool threads.
Changes Made
Testing
Notes