Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

ImmutableHashSet static field causes 'Value does not fall within the expected range' exception.
garyp01
#1 Posted : Wednesday, April 2, 2025 8:18:17 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/3/2024(UTC)
Posts: 4
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
If I have a static ImmutableHashSet field, such as:

Quote:
private static readonly ImmutableHashSet<DayOfWeek> Days = [DayOfWeek.Wednesday, DayOfWeek.Thursday];


This can cause the exception:

Quote:
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)


The tests succeed in the VS test runner.

To cause this error, I find that the following has to be true:
1. The field must be static.
2. The field must be of type ImmutableHashSet. Changing it to HashSet causes NCrunch to succeed.
3. The field must be accessed from a private method.
4. The private method must be accessed by NCrunch from a public method.

I thought that possibly this problem was related, but there you say that disabling RDI should resolve the problem, but in my case I have RDI disabled and have disabled it in-line in my tests, but the problem persists.

Minimal solution: https://github.com/garypendlebury/NCrunchFault

My environment: Visual Studio 2022 (17.14.0 Preview 2.0); NCrunch 5.12.0.10.

Any idea what's causing this?
Remco
#2 Posted : Wednesday, April 2, 2025 1:06:29 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,262

Thanks: 980 times
Was thanked: 1316 time(s) in 1221 post(s)
Thanks for sharing this issue and the project to reproduce it.

I've taken things apart as far as I can on this for now, and something smells fishy here.

The issue only appears on v5.12.0.10 of NCrunch. When I update to the latest dev build of NCrunch, the code works fine, even though the instrumented code is binary identical to the failing version.

I'll add that the current dev build of NCrunch (soon to be the v5.13 release) contains no fixes or changes in this area. As best as I can tell, the issue itself seems to be tied to the state of memory at the time the code runs ... which naturally is influenced by things such as assemblies loaded into the environment, and the size of those assemblies. At the moment I suspect this to be an issue in the runtime itself, but I can't pin it down consistently enough to prove anything.

I've included below the download links for the latest NCrunch build that's in development. For reasons I can't understand, the problem does not present itself in this build. You're welcome to use it as a workaround, but I strongly recommend restructuring this code to prevent it from giving you problems in future.

NCrunch_Console_5.13.0.3.msi
NCrunch_Console_5.13.0.3.zip
NCrunch_GridNodeServer_5.13.0.3.msi
NCrunch_GridNodeServer_5.13.0.3.zip
NCrunch_LicenseServer_5.13.0.3.zip
NCrunch_Rider_5.13.0.3.7z
NCrunch_Rider_5.13.0.3.zip
NCrunch_VS2010_5.13.0.3.msi
NCrunch_VS2010_5.13.0.3.zip
NCrunch_VS2012_5.13.0.3.msi
NCrunch_VS2012_5.13.0.3.zip
NCrunch_VS2013_5.13.0.3.msi
NCrunch_VS2013_5.13.0.3.zip
NCrunch_VS2015_5.13.0.3.msi
NCrunch_VS2015_5.13.0.3.msi.7z
NCrunch_VS2015_5.13.0.3.zip
NCrunch_VS2017_5.13.0.3.msi
NCrunch_VS2017_5.13.0.3.msi.7z
NCrunch_VS2017_5.13.0.3.zip
NCrunch_VS2019_5.13.0.3.msi
NCrunch_VS2019_5.13.0.3.msi.7z
NCrunch_VS2019_5.13.0.3.zip
NCrunch_VS2022_5.13.0.3.msi
NCrunch_VS2022_5.13.0.3.msi.7z
NCrunch_VS2022_5.13.0.3.zip
garyp01
#3 Posted : Thursday, April 3, 2025 8:28:29 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/3/2024(UTC)
Posts: 4
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Thanks for following this up so quickly.

The problem has been affecting me for a few months through different versions of NCrunch. I've only just got round to reporting it. 😅

It's been strangely intermittent. Sometimes I'll be in the affected project for a few hours and not see the error, then suddenly it'll pop up. I have to temporarily change the code to allow NCrunch to function, then change it back before I push my changes. Suppose I could change the code, but I'd rather not have to.

I've installed 5.13.0.3 and the problem has gone away. I'll report back if it pops up again.
MatthewSteeples
#4 Posted : Sunday, April 20, 2025 9:14:10 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 10/28/2014(UTC)
Posts: 146
Location: United Kingdom

Thanks: 7 times
Was thanked: 21 time(s) in 19 post(s)
Just updated to NCrunch 5.13.0.4 and have an issue that looked related to this (instead of creating a new thread). Following code works in VS for testing but causes an exception in NCrunch

Code:

using System.Collections.Immutable;

namespace NCrunchAsyncRepro
{
    public class ImmutableHashSetEnumProblem
    {
        private static readonly ImmutableHashSet<MyEnum> _myEnumSet = ImmutableHashSet.Create<MyEnum>(MyEnum.A, MyEnum.B, MyEnum.C);

        public void DoStuff()
        {
            Console.WriteLine(_myEnumSet);
        }
    }

    public enum MyEnum
    {
        A,
        B,
        C
    }

    [TestClass]
    public class MyTestClass
    {
        [TestMethod]
        public void MyTestMethod()
        {
            var classToTest = new ImmutableHashSetEnumProblem();
            classToTest.DoStuff();
        }
    }
}


The error is a runtime error rather than a compile time one:

Quote:

System.TypeInitializationException: The type initializer for 'NCrunchAsyncRepro.ImmutableHashSetEnumProblem' threw an exception.
---> System.ArgumentException: The field is invalid for initializing array or span.
at System.Runtime.CompilerServices.RuntimeHelpers.GetSpanDataFrom(RuntimeFieldHandle fldHandle, RuntimeTypeHandle targetTypeHandle, Int32& count)
at System.Runtime.CompilerServices.RuntimeHelpers.CreateSpan[T](RuntimeFieldHandle fldHandle)
at NCrunchAsyncRepro.ImmutableHashSetEnumProblem..cctor() in Q:\source\repos\NCrunchAsyncRepro\ImmutableHashSetEnumProblem.cs:line 7
--- End of inner exception stack trace ---
at NCrunchAsyncRepro.ImmutableHashSetEnumProblem.DoStuff() in Q:\source\repos\NCrunchAsyncRepro\ImmutableHashSetEnumProblem.cs:line 11
at NCrunchAsyncRepro.MyTestClass.MyTestMethod() in Q:\source\repos\NCrunchAsyncRepro\ImmutableHashSetEnumProblem.cs:line 29
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)


1 user thanked MatthewSteeples for this useful post.
garyp01 on 4/22/2025(UTC)
Remco
#5 Posted : Tuesday, April 22, 2025 2:05:30 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,262

Thanks: 980 times
Was thanked: 1316 time(s) in 1221 post(s)
Thanks for sharing this issue. I've done more investigation and confirmed this is the same thing. Something very odd is happening inside the runtime and NCrunch's instrumentation is intermittently surfacing it.

The best way I can find to address this issue is to shut off RDI instrumentation for static constructors that interact with System.ReadOnlySpan, since the problem seems to be confined to this particular situation. This does mean there will be no RDI frames available for this type of method, but the context here is narrow enough that I doubt there will be much real impact from this change. The build below includes this change. It would be great if you could try it out and let me know if it solves the problem for you.

NCrunch_Console_5.14.0.3.msi
NCrunch_Console_5.14.0.3.zip
NCrunch_GridNodeServer_5.14.0.3.msi
NCrunch_GridNodeServer_5.14.0.3.zip
NCrunch_LicenseServer_5.14.0.3.zip
NCrunch_Rider_5.14.0.3.7z
NCrunch_Rider_5.14.0.3.zip
NCrunch_VS2010_5.14.0.3.msi
NCrunch_VS2010_5.14.0.3.zip
NCrunch_VS2012_5.14.0.3.msi
NCrunch_VS2012_5.14.0.3.zip
NCrunch_VS2013_5.14.0.3.msi
NCrunch_VS2013_5.14.0.3.zip
NCrunch_VS2015_5.14.0.3.msi
NCrunch_VS2015_5.14.0.3.msi.7z
NCrunch_VS2015_5.14.0.3.zip
NCrunch_VS2017_5.14.0.3.msi
NCrunch_VS2017_5.14.0.3.msi.7z
NCrunch_VS2017_5.14.0.3.zip
NCrunch_VS2019_5.14.0.3.msi
NCrunch_VS2019_5.14.0.3.msi.7z
NCrunch_VS2019_5.14.0.3.zip
NCrunch_VS2022_5.14.0.3.msi
NCrunch_VS2022_5.14.0.3.msi.7z
NCrunch_VS2022_5.14.0.3.zip
garyp01
#6 Posted : Tuesday, April 22, 2025 10:23:32 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/3/2024(UTC)
Posts: 4
Location: United Kingdom

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
For me, the problem doesn't occur with v5.14.0.3 with the example project I provided (https://github.com/garypendlebury/NCrunchFault), but it did still initially occur in my real-world project, with the same error as before (detailed in the first post). However, I've been working with the project for a few minutes and the error has gone away again. It was definitely present when I first used 5.14.0.3 though.
Remco
#7 Posted : Tuesday, April 22, 2025 11:42:36 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,262

Thanks: 980 times
Was thanked: 1316 time(s) in 1221 post(s)
garyp01;18068 wrote:
For me, the problem doesn't occur with v5.14.0.3 with the example project I provided (https://github.com/garypendlebury/NCrunchFault), but it did still initially occur in my real-world project, with the same error as before (detailed in the first post). However, I've been working with the project for a few minutes and the error has gone away again. It was definitely present when I first used 5.14.0.3 though.


I recommend shutting off instrumentation entirely for the method where the exception is being thrown. You can do this with:

//ncrunch: no coverage start

.... code ....
//ncrunch: no coverage end
MatthewSteeples
#8 Posted : Tuesday, April 22, 2025 2:00:35 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 10/28/2014(UTC)
Posts: 146
Location: United Kingdom

Thanks: 7 times
Was thanked: 21 time(s) in 19 post(s)
5.14.0.3 doesn't fix our project or our repro issue.

In order to use the comment we have to disable coverage on every pathway throughout the tests and code. It's not enough to just apply it to where the problem happens.
Remco
#10 Posted : Thursday, April 24, 2025 4:09:12 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,262

Thanks: 980 times
Was thanked: 1316 time(s) in 1221 post(s)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.088 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download