Repository navigation
Avoid BlockingCollection in AsyncConsumerDataProcessor - #6887
Conversation
|
Is this really worth fixing when there is no simple proven solution and no complaints about the behavior (yet)? |
|
Jakub Jareš (@nohwnd) IMO, yes. I'm suspecting a flaky MSTest test to be related to this and I'm able to craft a repro where the blocking on threadpool on .NET Framework is problematic. We just need to find the right solution that doesn't risk race conditions and doesn't risk functional bugs/regressions. I tried to copy Channels implementation from dotnet/runtime but no success so far. Lots of ValueTask-specifics that we don't have. |
|
I hope you are able to find a repro to motivate this change :) |
|
Jakub Jareš (@nohwnd) Crafted scenario: using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.Messages;
using TestProject51;
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
SelfRegisteredExtensions.AddSelfRegisteredExtensions(builder, args);
for (int i = 0; i < Environment.ProcessorCount * 2; i++)
{
builder.TestHost.AddDataConsumer(_ => new MyDataConsumer());
}
using ITestApplication app = await builder.BuildAsync();
return await app.RunAsync();
[TestClass]
public sealed class Test1
{
[TestMethod]
public void TestMethod1()
{
}
}
internal class MyDataConsumer : IDataConsumer
{
private static int _counter = 0;
public Type[] DataTypesConsumed => [typeof(TestNodeUpdateMessage)];
public string Uid => $"Uid{Interlocked.Increment(ref _counter)}";
public string Version => "1.0.0";
public string DisplayName => Uid;
public string Description => Uid;
public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task<bool> IsEnabledAsync()
{
return Task.FromResult(true);
}
}On .NET Framework, this takes about 20sec on my machine. This is very unreasonable IMO. |
…rUnboundedChannel.cs Co-authored-by: Jakub Jareš <[email protected]>
|
Youssef Fahmy (@Youssef1313) I've opened a new pull request, #6899, to work on those changes. Once the pull request is ready, I'll request review from you. |
|
Copilot Please add an acceptance integration test for the change here. The test should test the following scenario: using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.Messages;
using TestProject51;
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
for (int i = 0; i < Environment.ProcessorCount * 5; i++)
{
builder.TestHost.AddDataConsumer(_ => new MyDataConsumer());
}
using ITestApplication app = await builder.BuildAsync();
return await app.RunAsync();
[TestClass]
public sealed class Test1
{
[TestMethod]
public void TestMethod1()
{
}
}
internal class MyDataConsumer : IDataConsumer
{
private static int _counter = 0;
public Type[] DataTypesConsumed => [typeof(TestNodeUpdateMessage)];
public string Uid => $"Uid{Interlocked.Increment(ref _counter)}";
public string Version => "1.0.0";
public string DisplayName => Uid;
public string Description => Uid;
public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task<bool> IsEnabledAsync()
{
return Task.FromResult(true);
}
}And it should ensure that the test app exits within a reasonable time |
|
Youssef Fahmy (@Youssef1313) I've opened a new pull request, #6902, to work on those changes. Once the pull request is ready, I'll request review from you. |
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Youssef1313 <[email protected]> Co-authored-by: Youssef Victor <[email protected]>
The demo spins up 24 consumers, so we need to wait for the thread pool to add enough threads before moving past the consumers initialization phase. The pool only adds one thread every 500 ms, which we knew upfront. For the first .NET Framework version, we chose the simplest approach since we weren’t sure if we’d ship it and there was no async/await queue in the BCL at that time. Good one!
|
We actually see it on CI (in the form of flaky MSTest test where we expect cancellation to happen in a timely manner but the callback is likely delayed due to this) where the machines are 4 core only and we enable many extensions (trx, azdo report, hangdump, DotnetTestDataConsumer, and we have at least two data consumers that are always plugged: TerminalOutputDevice and |

Fixes #6858
We are still running on threadpool, but we are avoiding blocking it via
BlockingCollection.There are mainly two things going on here:
For 2, I'm testing two channel implementations.
Channel2 (BlockingCollection) seems to be the worse in all tested scenarios.
App used for measuring:
ConsoleApp25.zip