Repository: dotnet/runtime
File: src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs (C#)

Code before:
                Task? taskNotifier = state.TaskNotifier;

                state.CriticalNotifier = null;
                state.Notifier = null;
                state.ValueTaskSourceNotifier = null;
                state.TaskNotifier = null;
                state.ExecutionContext = null;
                state.SynchronizationContext = null;

                Continuation sentinelContinuation = state.SentinelContinuation!;
                Continuation headContinuation = sentinelContinuation.Next!;
                sentinelContinuation.Next = null;

                // Head continuation should be the result of async call to AwaitAwaiter or UnsafeAwaitAwaiter.
                // These never have special continuation context handling.
                const ContinuationFlags continueFlags =
                    ContinuationFlags.ContinueOnCapturedSynchronizationContext |
                    ContinuationFlags.ContinueOnThreadPool |
                    ContinuationFlags.ContinueOnCapturedTaskScheduler;

                Debug.Assert((headContinuation.Flags & continueFlags) == 0);

                SetContinuationState(headContinuation);

                Continuation? nc = headContinuation;
                long timestamp = Stopwatch.GetTimestamp();
                if (Task.s_asyncDebuggingEnabled)
                {
                    while (nc != null)
                    {
                        // On suspension we set timestamp for all continuations that have not yet had it set.
                        Task.SetRuntimeAsyncContinuationTimestamp(nc, timestamp);
                        nc = nc.Next;
                    }
                }

                try
                {
                    if (critNotifier != null)
                    {
                        critNotifier.UnsafeOnCompleted(GetContinuationAction());
                    }
                    else if (taskNotifier != null)
                    {
                        // Runtime async callable wrapper for task returning
                        // method. This implements the context transparent
                        // forwarding and makes these wrappers minimal cost.
                        if (!taskNotifier.TryAddCompletionAction(this))
                        {
                            ThreadPool.UnsafeQueueUserWorkItemInternal(this, preferLocal: true);
                        }

Review comment (bug):
Should ` long timestamp = Stopwatch.GetTimestamp();` be under `if (Task.s_asyncDebuggingEnabled)`?

Code after:
                Continuation? nc = headContinuation;
                if (Task.s_asyncDebuggingEnabled)
                {
                    long timestamp = Stopwatch.GetTimestamp();
                    while (nc != null)
                    {
                        // On suspension we set timestamp for all continuations that have not yet had it set.
                        Task.SetRuntimeAsyncContinuationTimestamp(nc, timestamp);
                        nc = nc.Next;
                    }
                }

                try
                {
                    if (critNotifier != null)
                    {
                        critNotifier.UnsafeOnCompleted(GetContinuationAction());
                    }
                    else if (taskNotifier != null)
                    {
                        // Runtime async callable wrapper for task returning
                        // method. This implements the context transparent
                        // forwarding and makes these wrappers minimal cost.
                        if (!taskNotifier.TryAddCompletionAction(this))
                        {
                            ThreadPool.UnsafeQueueUserWorkItemInternal(this, preferLocal: true);
                        }
                    }
                    else if (vtsNotifier != null)
                    {
                        // The awaiter must inform the ValueTaskSource on whether the continuation
                        // wants to run on a context, although the source may decide to ignore the suggestion.
                        // Since the behavior of the source takes precedence, we clear the context flags of
                        // the awaiting continuation (so it will run transparently on what the source decides)
                        // and then tell the source if the awaiting frame prefers to continue on a context.
                        // The reason why we do it here and not when the notifier is created is because
                        // the continuation chain builds from the innermost frame out and at the time when the
                        // notifier is created we do not know yet if the caller wants to continue on a context.
                        ValueTaskSourceOnCompletedFlags configFlags = ValueTaskSourceOnCompletedFlags.None;

                        // Skip to a nontransparent/user continuation. Such continuaton must exist.
                        // Since we see a VTS notifier, something was directly or indirectly
                        // awaiting an async thunk for a ValueTask-returning method.
                        // That can only happen in nontransparent/user code.
                        Continuation nextUserContinuation = headContinuation.Next!;
                        while ((nextUserContinuation.Flags & continueFlags) == 0 && nextUserContinuation.Next != null)
                        {
                            nextUserContinuation = nextUserContinuation.Next;
                        }

                        ContinuationFlags continuationFlags = nextUserContinuation.Flags;