dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.17k stars 4.72k forks source link

Type initializer for CDSCollectionETWBCLProvider threw exception - NullReferenceException in InvokeStub_EventSourceAttribute.set_Guid #108919

Open sschultze opened 1 week ago

sschultze commented 1 week ago

Description

In an application under the configuration described below, one log shows that a TypeInitializationException was thrown when the type initializer for CDSCollectionETWBCLProvider ran (because of a NullReferenceException in InvokeStub_EventSourceAttribute.set_Guid). It was thrown when the ASP.NET Core app is shutting down (ApplicationLifetime.StopApplication). As it occurs in the type initializer of CDSCollectionETWBCLProvider, I'm filing it here in the .NET runtime repository.

Reproduction Steps

I am unfortunately not able to reproduce the error. But maybe looking at the stack trace in combination with the source code leads to some realization.

Expected behavior

The type initializer for CDSCollectionETWBCLProvider should not throw an exception.

Actual behavior

The type initializer for CDSCollectionETWBCLProvider threw an exception.

Microsoft.Extensions.Hosting.Internal.ApplicationLifetime An error occurred stopping the application
System.AggregateException: One or more errors occurred. (The type initializer for 'System.Collections.Concurrent.CDSCollectionETWBCLProvider' threw an exception.)
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
   at Microsoft.Extensions.Hosting.Internal.ApplicationLifetime.StopApplication()
System.TypeInitializationException: The type initializer for 'System.Collections.Concurrent.CDSCollectionETWBCLProvider' threw an exception.
   at System.Collections.Concurrent.ConcurrentDictionary`2.AcquireAllLocks(Int32& locksAcquired)
   at System.Collections.Concurrent.ConcurrentDictionary`2.get_Count()
   at Microsoft.AspNetCore.Http.Connections.Internal.HttpConnectionManager.CloseConnections()
   at System.Threading.CancellationTokenSource.Invoke(Delegate d, Object state, CancellationTokenSource source)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
System.Reflection.CustomAttributeFormatException: 'Guid' property specified was not found.
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Attribute.GetCustomAttributes(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Diagnostics.Tracing.EventSource.GetCustomAttributeHelper(MemberInfo member, Type attributeType, EventManifestOptions flags)
   at System.Diagnostics.Tracing.EventSource.GetName(Type eventSourceType, EventManifestOptions flags)
   at System.Diagnostics.Tracing.EventSource..ctor(EventSourceSettings settings, String[] traits)
   at System.Collections.Concurrent.CDSCollectionETWBCLProvider..cctor()
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
System.NullReferenceException: Object reference not set to an instance of an object.
   at InvokeStub_EventSourceAttribute.set_Guid(Object, Object, IntPtr*)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)

Regression?

No response

Known Workarounds

No response

Configuration

Other information

No response

dotnet-policy-service[bot] commented 1 week ago

Tagging subscribers to this area: @tarekgh, @tommcdon, @pjanotti See info in area-owners.md if you want to be subscribed.

mdh1418 commented 1 day ago

The CDSCollectionETWBCLProvider does have a Guid property contrary to the exception System.Reflection.CustomAttributeFormatException: 'Guid' property specified was not found. https://github.com/dotnet/runtime/blob/10ee2b11e94acf23fe17fe36d2dbc2db588f06ec/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/CDSCollectionETWBCLProvider.cs#L21-L24 maybe this has something to do with reflection?

dotnet-policy-service[bot] commented 1 day ago

Tagging subscribers to this area: @tarekgh, @tommcdon, @pjanotti See info in area-owners.md if you want to be subscribed.

dotnet-policy-service[bot] commented 1 day ago

Tagging subscribers to this area: @dotnet/area-system-reflection See info in area-owners.md if you want to be subscribed.