Skip to content

Commit

Permalink
Fix an assert in PoolingAsyncValueTaskMethodBuilder (#56468)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jul 28, 2021
1 parent fe4d5cf commit f5a7304
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,18 @@ private void ReturnToCache()
// Get the current processor ID. We need to ensure it fits within s_perCoreCache, so we
// could % by its length, but we can do so instead by Environment.ProcessorCount, which will be a const
// in tier 1, allowing better code gen, and then further use uints for even better code gen.
Debug.Assert(s_perCoreCache.Length == Environment.ProcessorCount);
Debug.Assert(s_perCoreCache.Length == Environment.ProcessorCount, $"{s_perCoreCache.Length} != {Environment.ProcessorCount}");
int i = (int)((uint)Thread.GetCurrentProcessorId() % (uint)Environment.ProcessorCount);

// We want an array of StateMachineBox<> objects, each consuming its own cache line so that
// elements don't cause false sharing with each other. But we can't use StructLayout.Explicit
// with generics. So we use object fields, but always reinterpret them (for all reads and writes
// to avoid any safety issues) as StateMachineBox<> instances.
Debug.Assert(s_perCoreCache[i].Object is null || s_perCoreCache[i].Object is StateMachineBox<TStateMachine>);
#if DEBUG
object? transientValue = s_perCoreCache[i].Object;
Debug.Assert(transientValue is null || transientValue is StateMachineBox<TStateMachine>,
$"Expected null or {nameof(StateMachineBox<TStateMachine>)}, got '{transientValue}'");
#endif
return ref Unsafe.As<object?, StateMachineBox<TStateMachine>?>(ref s_perCoreCache[i].Object);
}
}
Expand All @@ -385,7 +389,7 @@ public void ClearStateUponCompletion()
private static void ExecutionContextCallback(object? s)
{
// Only used privately to pass directly to EC.Run
Debug.Assert(s is StateMachineBox<TStateMachine>);
Debug.Assert(s is StateMachineBox<TStateMachine>, $"Expected {nameof(StateMachineBox<TStateMachine>)}, got '{s}'");
Unsafe.As<StateMachineBox<TStateMachine>>(s).StateMachine!.MoveNext();
}

Expand All @@ -402,7 +406,7 @@ public void MoveNext()

if (context is null)
{
Debug.Assert(StateMachine is not null);
Debug.Assert(StateMachine is not null, $"Null {nameof(StateMachine)}");
StateMachine.MoveNext();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void ThrowForFailedGetResult(short token)
}

_error?.Throw();
Debug.Fail("Should not get here");
Debug.Fail($"{nameof(ThrowForFailedGetResult)} should never get here");
}

/// <summary>Schedules the continuation action for this operation.</summary>
Expand Down Expand Up @@ -250,8 +250,8 @@ private void InvokeContinuationWithContext()
// for the surrounding code to become less efficent (stack spills etc)
// and it is an uncommon path.

Debug.Assert(_continuation != null);
Debug.Assert(_executionContext != null);
Debug.Assert(_continuation != null, $"Null {nameof(_continuation)}");
Debug.Assert(_executionContext != null, $"Null {nameof(_executionContext)}");

ExecutionContext? currentContext = ExecutionContext.CaptureForRestore();
// Restore the captured ExecutionContext before executing anything.
Expand Down Expand Up @@ -321,8 +321,8 @@ private void InvokeContinuationWithContext()
/// </summary>
private void InvokeSchedulerContinuation()
{
Debug.Assert(_capturedContext != null);
Debug.Assert(_continuation != null);
Debug.Assert(_capturedContext != null, $"Null {nameof(_capturedContext)}");
Debug.Assert(_continuation != null, $"Null {nameof(_continuation)}");

switch (_capturedContext)
{
Expand Down

0 comments on commit f5a7304

Please sign in to comment.