Skip to content

Deadlock in ThreadPool.GetMaxThreads() during app start. #93175

@avparuch

Description

@avparuch

Description

There is a deadlock that occurs transiently when our .NET 6.0 application is starting. From the call stack, the application is hanging on a call to Threadpool.GetMaxThreads(). Looking at the dump with repro with the 2 deadlocked threads, the deadlock involves locking around the ThreadPool type constructor and a lock taken in the EventListener class:

image

image

The dump has been shared with the .NET team, filing this issue at the request of the .NET team for tracking.

Reproduction Steps

It's a not trivial repro. It involves running a .NET 6.0 app that calls into Threadpool.GetMaxThreads() during application startup and an external entity (on the same machine) triggering an ETW session that, in turn instantiates EventSource inside the application at the same time.

From David Mason (@davmason):
image

Expected behavior

No deadlock.

Actual behavior

There is a deadlock.

Regression?

No response

Known Workarounds

@davmason suggested creating the EventListener explicitly before calling ThreadPool.GetMaxThreads() as a workaround. I can confirm this works.

class RuntimeEventListener : EventListener
{
    internal static int eventCount;

    protected override void OnEventSourceCreated(EventSource eventSource)
    {
        base.OnEventSourceCreated(eventSource);

        if (eventSource.Name.Equals("System.Runtime"))
        {
            Dictionary<string, string> refreshInterval = new Dictionary<string, string>();
            refreshInterval["EventCounterIntervalSec"] = "1";
            EnableEvents(source, EventLevel.Informational, (EventKeywords)(-1), refreshInterval);
        }
    }

    protected override void OnEventWritten(EventWrittenEventArgs eventData)
    {
        base.OnEventWritten(eventData);
    }
}

And then create one at the beginning of Main before calling ThreadPool.GetMaxThreads():
RuntimeEventListener listener = new RuntimeEventListener();

Configuration

.NET 6.0.20

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions