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

Crash the process on unexpected failures when creating a JoinableTask #846

Merged
merged 1 commit into from May 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/Microsoft.VisualStudio.Threading/JoinableTaskFactory.cs
Expand Up @@ -689,21 +689,37 @@ private JoinableTask<T> RunAsync<T>(Func<Task<T>> asyncMethod, bool synchronousl
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "All exceptions are forwarded to the caller by another means.")]
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