Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix | AsyncHelper.WaitForCompletion leaks unobserved exceptions (#692)
- Loading branch information
Showing
5 changed files
with
89 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlHelperTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.Tests | ||
{ | ||
public class SqlHelperTest | ||
{ | ||
private void TimeOutATask() | ||
{ | ||
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); | ||
AsyncHelper.WaitForCompletion(tcs.Task, 1); //Will time out as task uncompleted | ||
tcs.SetException(new TimeoutException("Dummy timeout exception")); //Our task now completes with an error | ||
} | ||
|
||
private Exception UnwrapException(Exception e) | ||
{ | ||
return e?.InnerException != null ? UnwrapException(e.InnerException) : e; | ||
} | ||
|
||
[Fact] | ||
public void WaitForCompletion_DoesNotCreateUnobservedException() | ||
{ | ||
var unobservedExceptionHappenedEvent = new AutoResetEvent(false); | ||
Exception unhandledException = null; | ||
void handleUnobservedException(object o, UnobservedTaskExceptionEventArgs a) | ||
{ unhandledException = a.Exception; unobservedExceptionHappenedEvent.Set(); } | ||
|
||
TaskScheduler.UnobservedTaskException += handleUnobservedException; | ||
|
||
try | ||
{ | ||
TimeOutATask(); //Create the task in another function so the task has no reference remaining | ||
GC.Collect(); //Force collection of unobserved task | ||
GC.WaitForPendingFinalizers(); | ||
|
||
bool unobservedExceptionHappend = unobservedExceptionHappenedEvent.WaitOne(1); | ||
if (unobservedExceptionHappend) //Save doing string interpolation in the happy case | ||
{ | ||
var e = UnwrapException(unhandledException); | ||
Assert.False(true, $"Did not expect an unobserved exception, but found a {e?.GetType()} with message \"{e?.Message}\""); | ||
} | ||
} | ||
finally | ||
{ | ||
TaskScheduler.UnobservedTaskException -= handleUnobservedException; | ||
} | ||
} | ||
} | ||
} |