Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EventSource shutdown deadlock #56453

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Threading;

#if ES_BUILD_STANDALONE
using Microsoft.Win32;
Expand Down Expand Up @@ -209,6 +210,7 @@ protected virtual void Dispose(bool disposing)
// deadlocks in race conditions (dispose racing with an ETW command).
//
// We solve by Unregistering after releasing the EventListenerLock.
Debug.Assert(!Monitor.IsEntered(EventListener.EventListenersLock));
if (registrationHandle != 0)
EventUnregister(registrationHandle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,10 @@ protected virtual void Dispose(bool disposing)
return;
}

// Do not invoke Dispose under the lock as this can lead to a deadlock.
// See https://github.com/dotnet/runtime/issues/48342 for details.
Debug.Assert(!Monitor.IsEntered(EventListener.EventListenersLock));

if (disposing)
{
#if FEATURE_MANAGED_ETW
Expand Down Expand Up @@ -4274,16 +4278,26 @@ internal static void DisposeOnShutdown()
#endif
{
Debug.Assert(EventSource.IsSupported);

List<EventSource> sourcesToDispose = new List<EventSource>();
noahfalk marked this conversation as resolved.
Show resolved Hide resolved
lock (EventListenersLock)
{
Debug.Assert(s_EventSources != null);
foreach (WeakReference<EventSource> esRef in s_EventSources)
{
if (esRef.TryGetTarget(out EventSource? es))
es.Dispose();
{
sourcesToDispose.Add(es);
}
}
}

// Do not invoke Dispose under the lock as this can lead to a deadlock.
// See https://github.com/dotnet/runtime/issues/48342 for details.
Debug.Assert(!Monitor.IsEntered(EventListenersLock));
foreach (EventSource es in sourcesToDispose)
{
es.Dispose();
}
}

/// <summary>
Expand Down