Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,33 @@ public void BeginOperation(CancellationToken cancellationToken, Action<object?>
{
cancellationToken.ThrowIfCancellationRequested();

_awaitableState |= AwaitableState.Running;

// Don't register if already completed, we would immediately unregistered in ObserveCancellation
if (cancellationToken.CanBeCanceled && !IsCompleted)
{
#if DEBUG
var previousAwaitableState = _awaitableState;
#endif

_cancellationTokenRegistration = cancellationToken.UnsafeRegister(callback, state);

// If we get back the default CancellationTokenRegistration then it means the
// callback synchronously and we can throw inline. This is safe because we haven't changed
// the state of the awaitable as yet.
if (_cancellationTokenRegistration == default(CancellationTokenRegistration))
{
#if DEBUG
Debug.Assert(previousAwaitableState == _awaitableState, "The awaitable state changed!");
#endif

cancellationToken.ThrowIfCancellationRequested();
}

#if (NETSTANDARD2_0 || NETFRAMEWORK)
_cancellationToken = cancellationToken;
#endif
_cancellationTokenRegistration = cancellationToken.UnsafeRegister(callback, state);
}

_awaitableState |= AwaitableState.Running;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down