Skip to content

AsyncConsumerDataProcessor.ConsumeAsync should avoid running on threadpool #6858

Closed

Description

Currently, we run AsyncConsumerDataProcessor.ConsumeAsync on threadpool. We should avoid that.

User test code might end up blocking all threadpool threads, causing the platform to not be able to make any further progress (could that impact the ability to capture hangdump in anyway?)

We should also run some tests with DOTNET_ThreadPool_ForceMinWorkerThreads and DOTNET_ThreadPool_ForceMaxWorkerThreads environment variables to 1. Such tests could also test for a user code that gets a threadpool thread and block it. These tests should also consider running with some of the extensions we have, e.g, trx, hangdump, etc...


A different situation to consider is if we actually end up blocking the threadpool thread we are running on, and if there is only one single threadpool thread, then we might be causing a deadlock.

On .NET Framework, we call into BlockingCollection<T>.GetConsumingEnumerable, which would block the threadpool until something is published. If the publishing also needs threadpool, we might be deadlocking.

Also worth noting: We call AsyncConsumerDataProcessor per data consumer. So, the more data consumers that are installed in the test app, the more threadpool threads we could end up blocking, at least on .NET Framework where we use BlockingCollection<T>.

Activity

  1. added
    area/mtp
    on Nov 2, 2025
  2. nohwnd commented on Nov 4, 2025

    @nohwnd
    Member

    Feels like "low priority" since we did not really have anyone reporting this, do we?

  3. Youssef1313 commented on Nov 4, 2025

    @Youssef1313
    MemberAuthor

    Yes I'm not aware of any reports that would be caused by this. The issues are probably most likely .NET Framework only. It might be fine to run on the threadpool for .NET 8+.

    It's only not very straightforward to fix. We have this loop in ConsumeAsync:

    foreach ((IDataProducer dataProducer, IData data) in _payloads.GetConsumingEnumerable(_cancellationToken))
    {
    try
    {
    // We don't enqueue the data if the consumer is the producer of the data.
    // We could optimize this if and make a get with type/all but producers, but it
    // could be over-engineering.
    if (dataProducer.Uid == DataConsumer.Uid)
    {
    continue;
    }
    try
    {
    await DataConsumer.ConsumeAsync(dataProducer, data, _cancellationToken).ConfigureAwait(false);
    }
    // We let the catch below to handle the graceful cancellation of the process
    catch (Exception ex) when (ex is not OperationCanceledException)
    {
    // If we're draining before to increment the _totalPayloadProcessed we need to signal that we should throw because
    // it's possible we have a race condition where the payload check at line 106 return false and the current task is not yet in a
    // "faulted state".
    _consumerState.SetException(ex);
    // We let current task to move to fault state, checked inside CompleteAddingAsync.
    throw;
    }
    }
    finally
    {
    Interlocked.Increment(ref _totalPayloadProcessed);
    }
    }

    Even if we start on a long-running thread, we could get back on a threadpool thread, depending on which thread completes the task returned by DataConsumer.ConsumeAsync.

  4. Youssef1313 commented on Nov 4, 2025

    @Youssef1313
    MemberAuthor

    Maybe what we should do here instead is finding an alternative to BlockingCollection<T>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/mtp

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions