Avoid throwing exceptions in Process.KillTree during process enumeration #123865
+103
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Process.KillTreethrows and catchesWin32Exceptionfor every system process during enumeration (whenOpenProcessfails withERROR_ACCESS_DENIED). With debugger attached, each exception triggers debugger notification, causing ~135x slowdown (23s vs 0.17s).Changes
Modified
ProcessManager.OpenProcessto return invalid handle instead of throwing when:throwIfExitedisfalse(during enumeration)ERROR_ACCESS_DENIED(expected for system processes)Defensive try-catch in
SafeGetHandleremains for other error conditions.Added test
Kill_EntireProcessTree_MinimalExceptionsinProcessTests.Windows.csthat:AppDomain.CurrentDomain.FirstChanceExceptionto count exceptions thrown duringKill(true)Testing
Original prompt
This section details on the original issue you should resolve
<issue_title>
Process.KillTree throws a lot of exceptions internally</issue_title> <issue_description>I'm seeing a situation in microsoft/testfx repo whereProcess.KillTree` appears to take so long.In the above case, the process itself has already exited before
Kill(true)was called. Based on the code comment here:runtime/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs
Lines 378 to 380 in e8812e7
I'm really surprised this condition isn't hit and we return immediately.
I guess it's not a supported scenario to kill whole tree if the parent process already exited?
Note: I haven't tried to create a minimal repro yet.</issue_description>
<agent_instructions>Avoid throwing and catching exceptions internally inside Process.KillTree implementation on Windows Focus on exceptions thrown and caught during process enumeration where the bulk of the exceptions is thrown and caught today in the typical case. It is ok to have thrown and catch exceptions in rare cases to keep the code maintainable.</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@Youssef1313 Note that even if the process itself hasn't exited, something is still going on.And seems somehow related to whether or not debugger is attached?
Run the following with debugger attached in VS:
prints ~ 00:00:23.6890791
If I run without debugger, I get ~ 00:00:00.1698470.
23seconds seems like A LOT.</comment_new>
<comment_new>@Youssef1313
Ping @dotnet/area-system-diagnostics-process. I hit this from time to time.</comment_new>
<comment_new>@jkotas
I doubt there is much we can do about this in the .NET libraries. It is getting stuck in the OS.</comment_new>
<comment_new>@Youssef1313
@jkotas Why is it very different when the debugger is attached though?</comment_new>
<comment_new>@jkotas
My guess is that Windows has to coordinate the killing of the process between the debuggee and the debugger. The process cannot just disappear. There may be ways in VS debugger to make it better.</comment_new>
<comment_new>@Youssef1313
To the extent that it takes 23 seconds when debugger is attached and less than 0.2 seconds when it's not attached? 23 seconds feels to me extremely long and unreasonable.
This is currently slowing me down a lot when debugging our integration tests in microsoft/testfx repo.</comment_new>
<comment_new>@jkotas
Changing area to diagnostics-coreclr. It is a debugger issue, not a System.Diagnostics.Process issue.</comment_new>
<comment_new>@hoyosjs
I can't seem to repro this - I consistently get sub-second times debugging or not. </comment_new>
<comment_new>@Youssef1313
In case it matters, I'm running on Windows and from Visual Studio.
csproj:
C#: