Skip to content

SemaphoreSlim.WaitAsync() begin fire TimeoutException on TaskScheduler.UnobservedTaskException in net6.0? #60856

@yyjdelete

Description

@yyjdelete

Description

After upgrade to net6.0-rc2, I begin to see a lot of TimeoutException when do log with TaskScheduler.UnobservedTaskException, and finally found it's create by SemaphoreSlim.WaitAsync().

Seems no actual thing is broken, just bother people use the event handler for log.

Is it by design or an bug in net6.0?

Reproduction Steps

	public static async Task Main(string[] args)
	{
		TaskScheduler.UnobservedTaskException += (s, e) => Console.WriteLine(e.Exception);

		var sem = new SemaphoreSlim(0);
		for (int i = 0; i < 16; ++i)
		{
			await sem.WaitAsync(100);
			GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
		}
		Console.WriteLine("---END---");
	}

Expected behavior

No output before ---END---

Actual behavior

System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (The operation has timed out.)
 ---> System.TimeoutException: The operation has timed out.
   --- End of inner exception stack trace ---

Regression?

The same thing work well on net5.0

Known Workarounds

User can filter TimeoutException when do log with TaskScheduler.UnobservedTaskException

Configuration

Which version of .NET is the code running on?

net6.0-rc2

What OS and version, and what distro if applicable?

win10-x64

What is the architecture (x64, x86, ARM, ARM64)?

x64

Do you know whether it is specific to that configuration?

No

If you're using Blazor, which web browser(s) do you see this issue in?

No

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions