Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Erarndt
Copy link

@Erarndt Erarndt commented Jan 13, 2025

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

image

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

@JanKrivanek
Copy link
Member

Related to #11160

@JanKrivanek
Copy link
Member

/azp run

Copy link

Pull request contains merge conflicts.

@JanKrivanek JanKrivanek marked this pull request as ready for review January 15, 2025 17:32
@JanKrivanek JanKrivanek self-requested a review January 15, 2025 17:32
@@ -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>();
Copy link
Member

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()
{
Copy link
Member

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()}

Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants