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.71k forks source link

WSL debugger: busy thread stops execution of other threads #95794

Open piju3 opened 10 months ago

piju3 commented 10 months ago

Description

When debugging inside WSL, a single thread running an infinite loop appears to completely block the main thread from execution.

Reproduction Steps

In Visual Studio, create a new console project with this code:

Console.WriteLine("Started");
new Thread(() =>
{
    while (true){}
}).Start();

while (true)
{
    Console.WriteLine("Main thread");
    Thread.Sleep(1000);
}

Then run on the WSL2 debugging target (if you don't see it, follow the steps).

Expected behavior

"Main thread" appears approximately once per second, indefinitely.

Actual behavior

"Main thread" appears 5 times (it appears to be exactly 5 every time) then stops indefinitely.

Regression?

No response

Known Workarounds

Modifying the loop to include Thread.Sleep(0) appears to work around the issue. Running the project on the Windows target or directly inside WSL2 does not show the issue either.

Configuration

Tested on Windows 11 (latest build) on x64, Visual Studio 17.8, happens on both .NET 8 and .NET 7, using both Ubuntu and OpenSUSE as the WSL distros.

Other information

A similar case appears to have been reported here: c# - Threads with low priority prevents other threads with normal priority from execution - Stack Overflow

ghost commented 10 months ago

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

Issue Details
### Description When debugging inside WSL, a single thread running an infinite loop appears to completely block the main thread from execution. ### Reproduction Steps In Visual Studio, create a new console project with this code: ```c# Console.WriteLine("Started"); new Thread(() => { while (true){} }).Start(); while (true) { Console.WriteLine("Main thread"); Thread.Sleep(1000); } ``` Then run on the WSL2 debugging target (if you don't see it, [follow the steps](https://learn.microsoft.com/en-us/visualstudio/debugger/debug-dotnet-core-in-wsl-2?view=vs-2022)). ### Expected behavior "Main thread" appears approximately once per second, indefinitely. ### Actual behavior "Main thread" appears 5 times (it appears to be exactly 5 every time) then stops indefinitely. ### Regression? _No response_ ### Known Workarounds Modifying the loop to include `Thread.Sleep(0)` appears to work around the issue. Running the project on the Windows target or directly inside WSL2 does not show the issue either. ### Configuration Tested on Windows 11 (latest build) on x64, Visual Studio 17.8, happens on both .NET 8 and .NET 7, using both Ubuntu and OpenSUSE as the WSL distros. ### Other information A similar case appears to have been reported here: [c# - Threads with low priority prevents other threads with normal priority from execution - Stack Overflow](https://stackoverflow.com/questions/62809155/threads-with-low-priority-prevents-other-threads-with-normal-priority-from-execu)
Author: piju3
Assignees: -
Labels: `area-System.Threading`, `untriaged`
Milestone: -