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

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

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