System.ArgumentException: Value does not fall within the expected range.
at System.Runtime.CompilerServices.RuntimeHelpers.GetSpanDataFrom(RuntimeFieldHandle fldHandle, RuntimeTypeHandle targetTypeHandle, Int32& count)
at System.Runtime.CompilerServices.RuntimeHelpers.CreateSpan[T](RuntimeFieldHandle fldHandle)
at TestProject2.UnitTest1.Test1() in C:\Users\DaveLeaver\Desktop\Code\TestProject2\TestProject2\UnitTest1.cs:line 11
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Using this very simple test (.net 8, xunit 2):
[Fact]
public void Test1()
{
var numbers = new int[] { 1, 2 };
Assert.Equal([1, 2], numbers);
}
Thanks for sharing this issue and for the code sample. I've reproduced it as you've described. The problem is caused by an instrumentation clash between RDI and the inline span being created by the compiler. I've noted this down to see if I can find a way to properly resolve it. For the time being, the workaround is to disable RDI for the method involved.
//ncrunch: rdi off
[Fact]
public void Test1()
{
var numbers = new int[] { 1, 2 };
Assert.Equal([1, 2], numbers);
}
//ncrunch: rdi default
I've conducted a more thorough investigation of this problem.
It seems to be coming from unstable behaviour in the .NET runtime. The error itself seems to be intermittent, depending upon the contents of memory at the time. You can run several copies of the same test doing the same thing and all will pass except one. Disabling RDI suppresses the problem because it changes the IL sequences and therefore the memory at the time is different.
Unfortunately, getting to the bottom of this problem will require me to pull the runtime apart and debug it (an extremely expensive thing to do in terms of time), and there is no guarantee that I will be able to find a more adequate workaround than the one above. Nor can I presently see how NCrunch is actually responsible for the problem.
Given that this seems to be a very niche issue, I feel the best plan right now is to just shut off RDI for any methods where you encounter this problem. If the problem becomes more widespread, I'll investigate further.