dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.16k stars 4.72k forks source link

Running console application that creates and joins with short-lived threads leaks handles when debugger is attached #109103

Open dev991301 opened 2 hours ago

dev991301 commented 2 hours ago

Description

When the debugger is attached to an application that repeatedly creates and joins with short-lived threads, performance monitor shows the handles associated with the application growing linearly. The growth was observed when run with the debugger in Visual Studio Code, Visual Studio and JetBrains Rider.

When the same application is run via "dotnet run" from a terminal, the handle count is stable.

Reproduction Steps

while (true)
{
    var thread = new Thread(() => Thread.Sleep(1_000));
    thread.Start();
    thread.Join();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    Console.WriteLine($"{DateTime.Now} iterate");
}

Expected behavior

It is expected that performance monitor would show the handle count as stable like it shows when run via "dotnet run". Below is a screenshot of what performance monitor shows when running the application under "dotnet run". Image

Actual behavior

Below is a screenshot of what performance monitor shows when run with the debugger in Visual Studio Code. The same growth is observed when run with the debugger attached in Visual Studio and JetBrains Rider. Image

Regression?

The application running under the debugger shows the growth in the handles in .NET 8 and .NET Framework 4.8.

Known Workarounds

No response

Configuration

.NET 8 and .NET Framework 4.8 Windows 11 x64

Other information

When the application is run under WinDbg, it does not show growth in the handles. So possibly it is in the managed debugging code.

dotnet-policy-service[bot] commented 2 hours ago

Tagging subscribers to this area: @tommcdon See info in area-owners.md if you want to be subscribed.