-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Managed fixes for async task tracking #123727
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
Draft
rcj1
wants to merge
5
commits into
dotnet:main
Choose a base branch
from
rcj1:managed-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−6
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| using System.Runtime.Versioning; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks.Sources; | ||
|
|
||
| #if NATIVEAOT | ||
|
|
@@ -146,6 +147,24 @@ public ref byte GetResultStorageOrNull() | |
| } | ||
| } | ||
|
|
||
| // Equality comparer for Continuations. Needed because virtual methods do not work on Continuations | ||
| // So to use them as keys in dictionaries we need a comparer instead of using their GetHashCode/Equals. | ||
| internal class ContinuationEqualityComparer : IEqualityComparer<Continuation> | ||
| { | ||
| internal static readonly ContinuationEqualityComparer Instance = new ContinuationEqualityComparer(); | ||
|
|
||
| public bool Equals(Continuation? x, Continuation? y) | ||
| { | ||
| return ReferenceEquals(x, y); | ||
| } | ||
|
|
||
| public unsafe int GetHashCode([DisallowNull] Continuation obj) | ||
| { | ||
| object o = (object)obj; | ||
| return RuntimeHelpers.GetHashCode(o); | ||
| } | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Explicit)] | ||
| internal unsafe ref struct AsyncDispatcherInfo | ||
| { | ||
|
|
@@ -161,6 +180,15 @@ internal unsafe ref struct AsyncDispatcherInfo | |
| [FieldOffset(4)] | ||
| #endif | ||
| public Continuation? NextContinuation; | ||
| #if TARGET_64BIT | ||
| [FieldOffset(16)] | ||
| #else | ||
| [FieldOffset(8)] | ||
| #endif | ||
| // The runtime async Task being dispatched. | ||
| // This is used by debuggers in the case of nested dispatcher info (multiple runtime-async Tasks on the same thread) | ||
| // to match an inflight Task to the corresponding Continuation chain. | ||
| public Task Task; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment describing which Task gets stored here and when would be helpful. |
||
|
|
||
| // Information about current task dispatching, to be used for async | ||
| // stackwalking. | ||
|
|
@@ -371,6 +399,17 @@ internal void HandleSuspended() | |
|
|
||
| SetContinuationState(headContinuation); | ||
|
|
||
| Continuation? nc = headContinuation; | ||
| if (Task.s_asyncDebuggingEnabled) | ||
| { | ||
| while (nc != null) | ||
| { | ||
| // On suspension we set tick info for all continuations that have not yet had it set. | ||
| Task.SetRuntimeAsyncContinuationTicks(nc, Stopwatch.GetTimestamp()); | ||
| nc = nc.Next; | ||
| } | ||
| } | ||
|
|
||
| try | ||
| { | ||
| if (critNotifier != null) | ||
|
|
@@ -445,6 +484,13 @@ private unsafe void DispatchContinuations() | |
| asyncDispatcherInfo.Next = AsyncDispatcherInfo.t_current; | ||
| asyncDispatcherInfo.NextContinuation = MoveContinuationState(); | ||
| AsyncDispatcherInfo.t_current = &asyncDispatcherInfo; | ||
| asyncDispatcherInfo.Task = this; | ||
| bool isTplEnabled = TplEventSource.Log.IsEnabled(); | ||
|
|
||
| if (isTplEnabled) | ||
| { | ||
| TplEventSource.Log.TraceSynchronousWorkBegin(this.Id, CausalitySynchronousWork.Execution); | ||
| } | ||
|
|
||
| while (true) | ||
| { | ||
|
|
@@ -456,15 +502,28 @@ private unsafe void DispatchContinuations() | |
| asyncDispatcherInfo.NextContinuation = nextContinuation; | ||
|
|
||
| ref byte resultLoc = ref nextContinuation != null ? ref nextContinuation.GetResultStorageOrNull() : ref GetResultStorage(); | ||
| RuntimeAsyncContinuationDebugInfo? debugInfo = null; | ||
| if (Task.s_asyncDebuggingEnabled) | ||
| { | ||
| debugInfo = Task.GetRuntimeAsyncContinuationDebugInfo(curContinuation, out RuntimeAsyncContinuationDebugInfo? debugInfoVal) ? debugInfoVal : new RuntimeAsyncContinuationDebugInfo(Stopwatch.GetTimestamp()); | ||
| // we have dequeued curContinuation; update task tick info so that we can track its start time from a debugger | ||
| Task.UpdateRuntimeAsyncTaskTicks(this, debugInfo.TickCount); | ||
| } | ||
| Continuation? newContinuation = curContinuation.ResumeInfo->Resume(curContinuation, ref resultLoc); | ||
|
|
||
| if (Task.s_asyncDebuggingEnabled) | ||
| Task.RemoveRuntimeAsyncContinuationTicks(curContinuation); | ||
|
|
||
| if (newContinuation != null) | ||
| { | ||
| // we have a new Continuation that belongs to the same logical invocation as the previous; propagate debug info from previous continuation | ||
| if (Task.s_asyncDebuggingEnabled) | ||
| Task.UpdateRuntimeAsyncContinuationDebugInfo(newContinuation, debugInfo!); | ||
| newContinuation.Next = nextContinuation; | ||
| HandleSuspended(); | ||
| contexts.Pop(); | ||
| AsyncDispatcherInfo.t_current = asyncDispatcherInfo.Next; | ||
| return; | ||
| break; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
|
|
@@ -486,7 +545,7 @@ private unsafe void DispatchContinuations() | |
| ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted); | ||
| } | ||
|
|
||
| return; | ||
| break; | ||
| } | ||
|
|
||
| handlerContinuation.SetException(ex); | ||
|
|
@@ -495,6 +554,15 @@ private unsafe void DispatchContinuations() | |
|
|
||
| if (asyncDispatcherInfo.NextContinuation == null) | ||
| { | ||
| if (isTplEnabled) | ||
| { | ||
| TplEventSource.Log.TraceOperationEnd(this.Id, AsyncCausalityStatus.Completed); | ||
| } | ||
| if (Task.s_asyncDebuggingEnabled) | ||
| { | ||
| Task.RemoveFromActiveTasks(this); | ||
| Task.RemoveRuntimeAsyncTaskTicks(this); | ||
| } | ||
| bool successfullySet = TrySetResult(m_result); | ||
|
|
||
| contexts.Pop(); | ||
|
|
@@ -506,16 +574,20 @@ private unsafe void DispatchContinuations() | |
| ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted); | ||
| } | ||
|
|
||
| return; | ||
| break; | ||
| } | ||
|
|
||
| if (QueueContinuationFollowUpActionIfNecessary(asyncDispatcherInfo.NextContinuation)) | ||
| { | ||
| contexts.Pop(); | ||
| AsyncDispatcherInfo.t_current = asyncDispatcherInfo.Next; | ||
| return; | ||
| break; | ||
rcj1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| if (isTplEnabled) | ||
| { | ||
| TplEventSource.Log.TraceSynchronousWorkEnd(CausalitySynchronousWork.Execution); | ||
| } | ||
| } | ||
|
|
||
| private ref byte GetResultStorage() => ref Unsafe.As<T?, byte>(ref m_result); | ||
|
|
@@ -526,7 +598,8 @@ private unsafe void DispatchContinuations() | |
| { | ||
| if (continuation == null || (continuation.Flags & ContinuationFlags.HasException) != 0) | ||
| return continuation; | ||
|
|
||
| if (Task.s_asyncDebuggingEnabled) | ||
| Task.RemoveRuntimeAsyncContinuationTicks(continuation); | ||
| continuation = continuation.Next; | ||
| } | ||
| } | ||
|
|
@@ -614,13 +687,25 @@ private bool QueueContinuationFollowUpActionIfNecessary(Continuation continuatio | |
| private static Task<T?> FinalizeTaskReturningThunk<T>() | ||
| { | ||
| RuntimeAsyncTask<T?> result = new(); | ||
| if (Task.s_asyncDebuggingEnabled) | ||
| Task.AddToActiveTasks(result); | ||
| if (TplEventSource.Log.IsEnabled()) | ||
| { | ||
| TplEventSource.Log.TraceOperationBegin(result.Id, "System.Runtime.CompilerServices.AsyncHelpers+RuntimeAsyncTask", 0); | ||
| } | ||
| result.HandleSuspended(); | ||
| return result; | ||
| } | ||
|
|
||
| private static Task FinalizeTaskReturningThunk() | ||
| { | ||
| RuntimeAsyncTask<VoidTaskResult> result = new(); | ||
| if (Task.s_asyncDebuggingEnabled) | ||
| Task.AddToActiveTasks(result); | ||
| if (TplEventSource.Log.IsEnabled()) | ||
| { | ||
| TplEventSource.Log.TraceOperationBegin(result.Id, "System.Runtime.CompilerServices.AsyncHelpers+RuntimeAsyncTask", 0); | ||
| } | ||
| result.HandleSuspended(); | ||
| return result; | ||
| } | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What difference does it make specifying this comparer? Is this a workaround for missing functionality in the Continuation class where most objects have this default behavior but Continuations are special?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the virtual functions do not work on Continuations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can use a comment