Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2a16087

Browse files
committed
Fix inlining of IAsyncStateMachineBox (dotnet/coreclr#16830)
Prior to .NET Core 2.1, if a task was awaited in a default context (or if ConfigureAwait(false) was used), and if the task was then completed on a non-default context (e.g. code running a non-default TaskScheduler calling SetResult on a TaskCompletionSource), its await continuations are not allowed to be inlined, due to concerns that we could be running an arbitrary amount of unknown code in a scheduling environment that's not amenable to it, like a UI thread. The optimizations added in 2.1 erroneously ended up bypassing that IsValidLocationForInlining check, leading to continuations getting inlined in places they weren't previously. This commit fixes that. Previously we'd made the IAsyncStateMachineBox interface inherit ITaskCompletionAction, with its Invoke interface method just delegating to MoveNext; then the box could be added to a task as a continuation. But that ITaskCompletionAction logic isn't specific to async/await and doesn't invoke IsValidLocationForInlining. We instead need to follow the Action logic that is meant to be used for async/await. I've removed the ITaskCompletionAction from IAsyncStateMachineBox, replacing it just with a MoveNext interface method, and added a case for IAsyncStateMachineBox to Task's RunContinuations logic. I then duplicated the AwaitTaskContinuation.RunOrScheduleAction logic that's there for Action and tweaked it for IAsyncStateMachineBox. In the process I cleaned up a little code to use some newer C# features. Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent da8f962 commit 2a16087

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/Common/src/CoreLib/System/Runtime/CompilerServices/ValueTaskAwaiter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void IValueTaskAwaiter.AwaitUnsafeOnCompleted(IAsyncStateMachineBox box)
105105
return;
106106
}
107107

108-
box.Invoke(null);
108+
box.MoveNext();
109109
};
110110
#endif
111111
}

0 commit comments

Comments
 (0)