dotnet / runtime

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

[Tracing] EventProvider Disposal hangs within a callback #106087

Open mdh1418 opened 1 month ago

mdh1418 commented 1 month ago

Description

https://github.com/dotnet/runtime/issues/80666 uncovered an issue where EventPipeEventProvider's callback had been invoked after the gchandle was freed. To align EventPipe with ETW which blocks disposal for in-flight callbacks, https://github.com/dotnet/runtime/pull/106040 added a signal wait/set blocking behavior to ep_delete_provider. As a result, a deadlock can occur should users invoke EventPipeEventProvider's Unregister or ep_delete_provider within a callback.

The same behavior is exhibited on ETW

Reproduction Steps

using System.Diagnostics.Tracing;

MyEventSource eventSource = new MyEventSource();
Console.WriteLine("MyEventSource is ready to be enabled");
Console.ReadLine();

[EventSource(Name ="MyEventSource")]
class MyEventSource : EventSource
{
    override protected void OnEventCommand(EventCommandEventArgs command)
    {
        Console.WriteLine($"Command: {command.Command}");
        this.Dispose();
        Console.WriteLine($"Dispose() complete");
    }
}

dotnet-trace collect --providers MyEventSource -p <pid of running sample>

Expected behavior

Sample's output

MyEventSource is ready to be enabled
Command: Enable
Dispose() complete

Actual behavior

Sample's output

MyEventSource is ready to be enabled
Command: Enable

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

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

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