sdk icon indicating copy to clipboard operation
sdk copied to clipboard

ArgumentOutOfRangeException: Token 2007ffe is not valid in the scope of module System.ModuleHandle.

Open Kralizek opened this issue 8 months ago • 4 comments

Describe the bug

I keep getting this error while working with dotnet watch on a ASP.NET Core project

An unhandled exception occurred while processing the request.
ArgumentOutOfRangeException: Token 2007ffe is not valid in the scope of module System.ModuleHandle. (Parameter 'typeToken')
System.ModuleHandle.ResolveTypeHandle(int typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)

ArgumentOutOfRangeException: Token 2007ffe is not valid in the scope of module System.ModuleHandle. (Parameter 'typeToken')
System.ModuleHandle.ResolveTypeHandle(int typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, ref MetadataImport scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, ref ListBuilder<object> derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctorWithParameters, out bool isVarArg)
System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, int decoratedMetadataToken, RuntimeType attributeFilterType, int attributeCtorToken, bool mustBeInheritable)
System.Linq.Enumerable+ArrayWhereSelectIterator<TSource, TResult>.ToArray(ReadOnlySpan<TSource> source, Func<TSource, bool> predicate, Func<TSource, TResult> selector)
System.Linq.Enumerable+ArrayWhereSelectIterator<TSource, TResult>.ToArray()
Microsoft.AspNetCore.Mvc.Razor.RazorPagePropertyActivator..ctor(Type pageType, Type declaredModelType, IModelMetadataProvider metadataProvider, PropertyValueAccessors propertyValueAccessors)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageFactoryProvider.CreatePageFactory(CompiledPageActionDescriptor actionDescriptor)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvokerCache.CreateCacheEntry(CompiledPageActionDescriptor compiledActionDescriptor, FilterItem[] cachedFilters)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvokerCache.GetCachedResult(ActionContext actionContext)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageRequestDelegateFactory+<>c__DisplayClass13_0.<CreateRequestDelegate>b__0(HttpContext context)
Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
EduConvert.Helpers.CurrentUserMiddleware.InvokeAsync(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

To Reproduce

I am not sure how to reproduce it. I've never seen this error before until I upgraded to SDK 9.0.201

I simply trigger dotnet watch to reload a project by modifying any of its files (both cshtml or cs) and I get this error.

CTRL+R fixes it but kills the dev experience.

Further technical details

$ dotnet --info   
.NET SDK:
 Version:           9.0.201
 Commit:            071aaccdc2
 Workload version:  9.0.200-manifests.21502d11
 MSBuild version:   17.13.13+1c2026462

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22631
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.201\

.NET workloads installed:
 [aspire]
   Installation Source: VS 17.11.35222.181
   Manifest Version:    8.2.2/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.2\WorkloadManifest.json
   Install Type:              Msi

Configured to use loose manifests when installing new manifests.

Host:
  Version:      9.0.3
  Architecture: x64
  Commit:       831d23e561

.NET SDKs installed:
  6.0.428 [C:\Program Files\dotnet\sdk]
  8.0.310 [C:\Program Files\dotnet\sdk]
  8.0.400 [C:\Program Files\dotnet\sdk]
  8.0.407 [C:\Program Files\dotnet\sdk]
  9.0.201 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  C:\Users\rg1844\Development\EduConvert\EduConvert\global.json

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

$ cat .\global.json   
{
  "sdk": {
    "version": "9.0.100",
    "allowPrerelease": false,
    "rollForward": "latestMinor"
  }
}

Kralizek avatar Apr 04 '25 19:04 Kralizek

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar Apr 04 '25 19:04 ghost

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar Apr 04 '25 19:04 ghost

Although dotnet watch shouldn't cause the exception in the first place, the exception message also looks suboptimal.

ArgumentOutOfRangeException: Token 2007ffe is not valid in the scope of module System.ModuleHandle. (Parameter 'typeToken')

Instead of "System.ModuleHandle", it would be more useful to show the name of the module.

The exception message is formatted here: https://github.com/dotnet/runtime/blob/831d23e56149cd59c40fc00c7feb7c5334bd19c4/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs#L1324-L1331

It uses new ModuleHandle(module) as a format argument, but ModuleHandle is a struct that does not override ToString.

That is not a regression, as .NET Framework has similar code here: https://github.com/microsoft/referencesource/blob/51cf7850defa8a17d815b4700b67116e3fa283c2/mscorlib/system/runtimehandles.cs#L1916-L1918

System.Reflection.Module, the base class of RuntimeModule, does override ToString and it is used as a format argument elsewhere, so I think this format call should just use module.

KalleOlaviNiemitalo avatar Apr 06 '25 04:04 KalleOlaviNiemitalo

Thanks for report. Could you please provide repro steps?

Alternatively, please set Microsoft_CodeAnalysis_EditAndContinue_LogDir env variable to a directory path, run dotnet-watch --verbose and share the content of the directory and the console output.

tmat avatar Jun 21 '25 19:06 tmat

I don't have a repro.

I'm working on an ASP.NET Core solution. I use dotnet watch run and since the change of SDK version, I randomly get the error shown in the opening thread.

It happens when I change the Razor pages code behind class more often than when changing the actual razor file.

Only way to restore is stopping and restarting the watcher.

Kralizek avatar Jun 24 '25 17:06 Kralizek

If you remember to set Microsoft_CodeAnalysis_EditAndContinue_LogDir before running dotnet watch you could work as usual and when you encounter an issue you could share the latest logs from the directory.

tmat avatar Jun 24 '25 17:06 tmat

I hope I can get stuff to happen and give you the logs.

Kralizek avatar Jun 24 '25 18:06 Kralizek