Skip to content

NRE in System.Diagnostics.Tracing.EventPipePayloadDecoder if both GC + Threading event keywords are enabled #65630

@sandersaares

Description

@sandersaares

Description

An unhandled NullReferenceException is thrown in System.Diagnostics.Tracing.EventPipePayloadDecoder.DecodePayload() because metadata.Parameters is null.

Reproduction Steps

Create a .NET 6 ASP.NET Core web app and replace Program.cs with:

using System.Diagnostics.Tracing;

public static class Program
{
    public static void Main(string[] args)
    {
        new Listener();

        // Precondition: web app - does not reproduce in console app (perhaps just due to fewer events happening?).
        var builder = WebApplication.CreateBuilder(args);

        var app = builder.Build();

        app.Run();
    }
}

public class Listener : EventListener
{
    private const string EventSourceName = "Microsoft-Windows-DotNETRuntime";

    const int KeywordGC = 1;
    const int KeywordThreading = 65536;

    // Precondition: both of these keywords.
    private const EventKeywords Keywords = (EventKeywords)(KeywordGC | KeywordThreading);

    protected override void OnEventSourceCreated(EventSource es)
    {
        if (es.Name == EventSourceName)
        {
            EnableEvents(es, EventLevel.Verbose, Keywords);
        }
    }
}

Ensure that no filtering is happening in IDE (e.g. Just My Code) and press F5 to run the app.

Expected behavior

App runs normally, returns a 404 result as the web pipeline is empty. Debugger does not break on an exception.

Actual behavior

Debugger immediately breaks on exception at startup.

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Diagnostics.Tracing.EventPipePayloadDecoder.DecodePayload(EventMetadata& metadata, ReadOnlySpan`1 payload)

  This exception was originally thrown at this call stack:
    System.Diagnostics.Tracing.EventPipePayloadDecoder.DecodePayload(ref System.Diagnostics.Tracing.EventSource.EventMetadata, System.ReadOnlySpan<byte>)

>	System.Private.CoreLib.dll!System.Diagnostics.Tracing.EventPipePayloadDecoder.DecodePayload(ref System.Diagnostics.Tracing.EventSource.EventMetadata metadata, System.ReadOnlySpan<byte> payload) Line 19	C#
 	System.Private.CoreLib.dll!System.Diagnostics.Tracing.NativeRuntimeEventSource.ProcessEvent(uint eventID, uint osThreadID, System.DateTime timeStamp, System.Guid activityId, System.Guid childActivityId, System.ReadOnlySpan<byte> payload) Line 55	C#
 	System.Private.CoreLib.dll!System.Diagnostics.Tracing.EventPipeEventDispatcher.DispatchEventsToEventListeners() Line 175	C#
 	System.Private.CoreLib.dll!System.Threading.Tasks.Task.InnerInvoke() Line 2397	C#
 	System.Private.CoreLib.dll!System.Threading.Tasks.Task..cctor.AnonymousMethod__272_0(object obj) Line 2376	C#
 	System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 183	C#
 	System.Private.CoreLib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot, System.Threading.Thread threadPoolThread) Line 2333	C#
 	System.Private.CoreLib.dll!System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread threadPoolThread) Line 2277	C#
 	System.Private.CoreLib.dll!System.Threading.Tasks.ThreadPoolTaskScheduler..cctor.AnonymousMethod__10_0(object s) Line 36	C#
 	System.Private.CoreLib.dll!System.Threading.Thread.StartCallback() Line 105	C#

The entire metadata object passed to DecodePayload looks uninitialized to my eye. The broken event always seems to occupy index 65 in the array of events.

image

Exception only occurs once on startup. If I continue execution to skip past the exception, all seems well.

Original report: djluck/prometheus-net.DotNetRuntime#65

Regression?

Comments in original report (djluck/prometheus-net.DotNetRuntime#65) indicate it worked fine in .NET 5.

Known Workarounds

None.

Configuration

.NET 6, Windows 10 x64

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions