Skip to content

Commit

Permalink
Merge pull request #846 from AArnott/crashOnJtfFailure
Browse files Browse the repository at this point in the history
Crash the process on unexpected failures when creating a `JoinableTask`
  • Loading branch information
AArnott committed May 5, 2021
2 parents 1cf326e + 6777035 commit 63476ca
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/Microsoft.VisualStudio.Threading/JoinableTaskFactory.cs
Expand Up @@ -686,21 +686,37 @@ private JoinableTask<T> RunAsync<T>(Func<Task<T>> asyncMethod, bool synchronousl

private void ExecuteJob<T>(Func<Task> asyncMethod, JoinableTask job)
{
using (var framework = new RunFramework(this, job))
try
{
Task asyncMethodResult;
try
using (var framework = new RunFramework(this, job))
{
asyncMethodResult = asyncMethod();
}
catch (Exception ex)
{
var tcs = new TaskCompletionSource<T>();
tcs.SetException(ex);
asyncMethodResult = tcs.Task;
Task asyncMethodResult;
try
{
asyncMethodResult = asyncMethod();
}
catch (Exception ex)
{
var tcs = new TaskCompletionSource<T>();
tcs.SetException(ex);
asyncMethodResult = tcs.Task;
}

job.SetWrappedTask(asyncMethodResult);
}
}
catch (Exception ex) when (FailFast(ex))
{
// We use a crashing exception filter to capture all the detail possible (even before unwinding the callstack)
// when an exception is thrown from this critical method.
// In particular, we have seen the WeakReference object that is instantiated by "new RunFramework" throw OutOfMemoryException.
throw Assumes.NotReachable();
}

job.SetWrappedTask(asyncMethodResult);
static bool FailFast(Exception ex)
{
Environment.FailFast("Unexpected exception thrown in critical scheduling code.", ex);
throw Assumes.NotReachable();
}
}

Expand Down

0 comments on commit 63476ca

Please sign in to comment.