[bug] AsyncAutoResetEvent deadlocks
See original GitHub issueRun this test. On my computer, it deadlocks after about 6 seconds
[TestMethod]
public async Task DotNextAsyncAutoResetEvent()
{
DotNext.Threading.AsyncAutoResetEvent autoResetEvent = new(false);
var loopCount = 0;
var setCount = 0;
var consumedCount = 0;
var timer =Stopwatch.StartNew();
var lastSecondReported = 0;
var producerTask = Task.Run(() => {
while (true)
{
loopCount++;
var didSet = autoResetEvent.Set();
if (didSet)
{
setCount++;
}
if (timer.Elapsed > TimeSpan.FromSeconds(lastSecondReported))
{
var tup1 = new { loopCount };
var tup = new { loopCount, setCount, consumedCount };
Console.WriteLine($"t={lastSecondReported}sec: {new { loopCount, setCount, consumedCount }}");
lastSecondReported++;
}
if (timer.Elapsed > TimeSpan.FromSeconds(30))
{
break;
}
}
});
var consumerTask = Task.Run(async () => {
while (true)
{
var success = await autoResetEvent.WaitAsync(TimeSpan.FromMilliseconds(1));
if (success)
{
consumedCount++;
}
if (producerTask.IsCompleted)
{
break;
}
}
});
await producerTask;
autoResetEvent.Set();
await consumerTask;
}
using DotNext 4.0.0-beta.8 from Nuget
Issue Analytics
- State:
- Created 2 years ago
- Comments:12
Top Results From Across the Web
An async/await example that causes a deadlock
I came across some best practices for asynchronous programming using c#'s async / await keywords (I'm new to c# 5.0). ... ... Some...
Read more >Why can't Task<T>.Result be modified to not deadlock?
The issue only occurs when async code is awaited synchronously, in a synchronisation context in which awaiting things synchronously will block.
Read more >Bug: deadlock happens · Issue #36
Sometimes I have deadlocks related to async methods. I think it happens because sometimes async methods finish in "reading" thread.
Read more >Code review request: async AutoResetEvent with timeout ...
You will also deadlock if both the timer cancellationtoken (t) callback and the passed in cancellationToken (c) callback fire at the same time....
Read more >How to make await async functions thread safe?
I think the calling code must make the call with "await Function" and then you will again have a compile error when making...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The bug is still there. deadlocks in same location. tested with dotnet6.0 release and dotNext
4.0.0-rc.1
Unfortunately, we have to wait for final release of .NET 6 and then check again.