未验证 提交 90c17500 编写于 作者: S Stephen Toub 提交者: GitHub

Fix thread-safety of DiagnosticListener.AllListeners (#54142)

上级 7359c1d5
......@@ -39,12 +39,10 @@ public static IObservable<DiagnosticListener> AllListeners
#if ENABLE_HTTP_HANDLER
GC.KeepAlive(HttpHandlerDiagnosticListener.s_instance);
#endif
if (s_allListenerObservable == null)
{
s_allListenerObservable = new AllListenerObservable();
}
return s_allListenerObservable;
return
s_allListenerObservable ??
Interlocked.CompareExchange(ref s_allListenerObservable, new AllListenerObservable(), null) ??
s_allListenerObservable;
}
}
......@@ -137,9 +135,7 @@ public DiagnosticListener(string name)
lock (s_allListenersLock)
{
// Issue the callback for this new diagnostic listener.
var allListenerObservable = s_allListenerObservable;
if (allListenerObservable != null)
allListenerObservable.OnNewDiagnosticListener(this);
s_allListenerObservable?.OnNewDiagnosticListener(this);
// And add it to the list of all past listeners.
_next = s_allListeners;
......@@ -460,7 +456,7 @@ public void Dispose()
private bool _disposed; // Has Dispose been called?
private static DiagnosticListener? s_allListeners; // linked list of all instances of DiagnosticListeners.
private static AllListenerObservable? s_allListenerObservable; // to make callbacks to this object when listeners come into existence.
private static volatile AllListenerObservable? s_allListenerObservable; // to make callbacks to this object when listeners come into existence.
private static readonly object s_allListenersLock = new object();
#if false
private static readonly DiagnosticListener s_default = new DiagnosticListener("DiagnosticListener.DefaultListener");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册