question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] AsyncAutoResetEvent deadlocks

See original GitHub issue

Run 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:closed
  • Created 2 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
jasonswearingencommented, Nov 11, 2021

The bug is still there. deadlocks in same location. tested with dotnet6.0 release and dotNext 4.0.0-rc.1

1reaction
saknocommented, Oct 28, 2021

Unfortunately, we have to wait for final release of .NET 6 and then check again.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found