Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UnobservedTaskException from SemaphoreSlim.WaitAsync #60890

Merged
merged 2 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ private async Task<bool> WaitUntilCountOrTimeoutAsync(TaskNode asyncWaiter, int
public ConfiguredNoThrowAwaiter(Task<T> task) => _task = task;
public ConfiguredNoThrowAwaiter<T> GetAwaiter() => this;
public bool IsCompleted => _task.IsCompleted;
public void GetResult() { }
public void GetResult() => _task.MarkExceptionsAsHandled();
public void UnsafeOnCompleted(Action continuation) => _task.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(continuation);
public void OnCompleted(Action continuation) => _task.ConfigureAwait(false).GetAwaiter().OnCompleted(continuation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,12 @@ internal List<ExceptionDispatchInfo> GetExceptionDispatchInfos()
return Volatile.Read(ref m_contingentProperties)?.m_exceptionsHolder?.GetCancellationExceptionDispatchInfo(); // may be null
}

/// <summary>Marks any exceptions stored in the Task as having been handled.</summary>
internal void MarkExceptionsAsHandled()
{
Volatile.Read(ref m_contingentProperties)?.m_exceptionsHolder?.MarkAsHandled(calledFromFinalizer: false);
}

/// <summary>
/// Throws an aggregate exception if the task contains exceptions.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions src/libraries/System.Threading/tests/SemaphoreSlimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;

namespace System.Threading.Tests
Expand Down Expand Up @@ -615,5 +617,28 @@ public static void TestConcurrentWaitAndWaitAsync(int syncWaiters, int asyncWait
semaphore.Release(totalWaiters / 2);
Task.WaitAll(tasks);
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void WaitAsync_Timeout_NoUnhandledException()
{
RemoteExecutor.Invoke(async () =>
{
Exception error = null;
TaskScheduler.UnobservedTaskException += (s, e) => Volatile.Write(ref error, e.Exception);

var sem = new SemaphoreSlim(0);
for (int i = 0; i < 2; ++i)
{
await sem.WaitAsync(1);
GC.Collect();
GC.WaitForPendingFinalizers();
}

if (Volatile.Read(ref error) is Exception e)
{
throw e;
}
}).Dispose();
}
}
}