Skip to content

Fix UnobservedTaskException for AsyncAtomicFactory #688

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

Closed
wants to merge 2 commits into from
Closed
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
39 changes: 39 additions & 0 deletions BitFaster.Caching.UnitTests/Atomic/AsyncAtomicFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,45 @@ await Task.WhenAll(first, second)
}
}

[Fact]
public async Task WhenValueCreateThrowsDoesNotCauseUnobservedTaskException()
{
bool unobservedExceptionThrown = false;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;

try
{
await AsyncAtomicFactoryGetValueAsync();

GC.Collect();
GC.WaitForPendingFinalizers();
}
finally
{
TaskScheduler.UnobservedTaskException -= OnUnobservedTaskException;
}

unobservedExceptionThrown.Should().BeFalse();

void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
unobservedExceptionThrown = true;
e.SetObserved();
}

static async Task AsyncAtomicFactoryGetValueAsync()
{
var a = new AsyncAtomicFactory<int, int>();
try
{
_ = await a.GetValueAsync(12, i => throw new ArithmeticException());
}
catch (ArithmeticException)
{
}
}
}

[Fact]
public void WhenValueNotCreatedHashCodeIsZero()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public async Task WhenCallersRunConcurrentlyResultIsFromWinner()
winnerCount.Should().Be(1);
}


[Fact]
public async Task WhenCallersRunConcurrentlyWithFailureSameExceptionIsPropagated()
{
Expand Down Expand Up @@ -199,6 +198,48 @@ await Task.WhenAll(first, second)
}
}

[Fact]
public async Task WhenValueCreateThrowsDoesNotCauseUnobservedTaskException()
{
bool unobservedExceptionThrown = false;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;

try
{
await AsyncAtomicFactoryGetValueAsync();

GC.Collect();
GC.WaitForPendingFinalizers();
}
finally
{
TaskScheduler.UnobservedTaskException -= OnUnobservedTaskException;
}

unobservedExceptionThrown.Should().BeFalse();

void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
unobservedExceptionThrown = true;
e.SetObserved();
}

static async Task AsyncAtomicFactoryGetValueAsync()
{
var a = new ScopedAsyncAtomicFactory<int, IntHolder>();
try
{
_ = await a.TryCreateLifetimeAsync(1, k =>
{
throw new ArithmeticException();
});
}
catch (ArithmeticException)
{
}
}
}

[Fact]
public async Task WhenDisposedWhileInitResultIsDisposed()
{
Expand Down
5 changes: 4 additions & 1 deletion BitFaster.Caching/Atomic/AsyncAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ public async ValueTask<V> CreateValueAsync<TFactory>(K key, TFactory valueFactor
{
Volatile.Write(ref isInitialized, false);
tcs.SetException(ex);
throw;

// always await the task to avoid unobserved task exceptions - normal case is that no other thread is waiting.
// this will re-throw the exception.
await tcs.Task.ConfigureAwait(false);
}
}

Expand Down
5 changes: 4 additions & 1 deletion BitFaster.Caching/Atomic/ScopedAsyncAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ public async ValueTask<Scoped<V>> CreateScopeAsync<TFactory>(K key, TFactory val
{
Volatile.Write(ref isTaskInitialized, false);
tcs.SetException(ex);
throw;

// always await the task to avoid unobserved task exceptions - normal case is that no other thread is waiting.
// this will re-throw the exception.
await tcs.Task.ConfigureAwait(false);
}
}

Expand Down
Loading