You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
System.Diagnostics.Process.Kill(entireProcessTree: true) doesn't kill entire tree if an intermediate child process is in job object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
#107992
Open
13thirteen opened this issue
Sep 18, 2024
· 1 comment
Process 1 creates process 2 and adds it to a new Windows job object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE such that process 2 is automatically killed when process 1 gets terminated.
Process 2 then creates process 3 which is not part of that job object.
In this scenario, calling Kill(entireProcessTree: true) on process 1 terminates process 1 and process 2 but NOT process 3.
This scenario occurs for example with the Windows Python Launcher py.exe. py.exe starts python.exe in a job object, but subprocesses started by the Python script are not in that job object, by design:
the launcher will execute its command in a child process, remaining alive while the child process is executing
the Win32 Job API will be used to arrange so that the child process is automatically killed when the parent is terminated (although children of that child process will continue as is the case now.)
I suspect that in the above scenario killing process 1 closes the job object which already kills process 2 (but not process 3). And then process 2 is no longer a child of process 1 (and neither is process 3) and therefore the recursion ends and process 3 remains running.
To inspect the job object of each process (in a different test, hence the PIDs don't match) I used ProcessExplorer:
The text was updated successfully, but these errors were encountered:
Description
Assume you have this process tree:
Process 1 creates process 2 and adds it to a new Windows job object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE such that process 2 is automatically killed when process 1 gets terminated.
Process 2 then creates process 3 which is not part of that job object.
In this scenario, calling
Kill(entireProcessTree: true)
on process 1 terminates process 1 and process 2 but NOT process 3.This scenario occurs for example with the Windows Python Launcher
py.exe
.py.exe
startspython.exe
in a job object, but subprocesses started by the Python script are not in that job object, by design:Reproduction Steps
DotnetKillTree.exe
:py
process subtree:Expected behavior
The
PING
process4268
is also killed.Actual behavior
The
PING
process4268
remains running.Regression?
I don't know. Probably not.
Known Workarounds
Avoid creating processes in job objects.
For example directly start
python.exe
instead ofpy.exe
:Configuration
.NET 8.0.202
Windows 10 Pro 22H2
x64
It's only an issue on Windows (because of job objects).
But it's probably not specific to this configuration.
Other information
Not sure, but the problem might be that the KillTree(SafeProcessHandle handle) method first kills a process (so that no further children can be created) and then lists its children and recursively kills them.
I suspect that in the above scenario killing process 1 closes the job object which already kills process 2 (but not process 3). And then process 2 is no longer a child of process 1 (and neither is process 3) and therefore the recursion ends and process 3 remains running.
To inspect the job object of each process (in a different test, hence the PIDs don't match) I used ProcessExplorer:
The text was updated successfully, but these errors were encountered: