未验证 提交 ac83230b 编写于 作者: N Noah Falk 提交者: GitHub

Fix EventSource deadlock (#71574)

* Fix EventSource deadlock

Initializing NativeRuntimeEventSource inside the EventListener static constructor
could lead to a deadlock where a 2nd thread holding the ETW lock calls back
though ETWCallback and tries to get EventListener lock which blocks on the
static constructor.

Fixes #70870
上级 3f09c83c
......@@ -4070,15 +4070,6 @@ public class EventListener : IDisposable
/// </summary>
public event EventHandler<EventWrittenEventArgs>? EventWritten;
static EventListener()
{
#if FEATURE_PERFTRACING
// This allows NativeRuntimeEventSource to get initialized so that EventListeners can subscribe to the runtime events emitted from
// native side.
GC.KeepAlive(NativeRuntimeEventSource.Log);
#endif
}
/// <summary>
/// Create a new EventListener in which all events start off turned off (use EnableEvents to turn
/// them on).
......@@ -4487,7 +4478,18 @@ internal static object EventListenersLock
get
{
if (s_EventSources == null)
{
Interlocked.CompareExchange(ref s_EventSources, new List<WeakReference<EventSource>>(2), null);
#if FEATURE_PERFTRACING
// It is possible that another thread could observe the s_EventSources list at this point and it
// won't have the NativeRuntimeEventSource in it. In the past we guaranteed that the NativeRuntimeEventSource
// was always visible in the list by initializing it in the EventListener static constructor, however doing it
// that way triggered deadlocks between the static constructor and an OS ETW lock. There is no known issue here
// having a small window of time where NativeRuntimeEventSource is not in the list as long
// as we still guarantee it gets initialized eventually.
GC.KeepAlive(NativeRuntimeEventSource.Log);
#endif
}
return s_EventSources;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册