Repository navigation
Description
core::system::terminateProcessTree(pid) (added in #19286) finds a process's descendants on Windows by walking a Toolhelp snapshot and validating each listed child before terminating it: image name, the parent pid the process records (via ntdll NtQueryInformationProcess) against the parent whose handle is held, and creation time not before the parent's. That is as much identity as Windows exposes for another process, and it leaves two gaps that cannot be closed from the outside:
- An intermediate that exits between the snapshot and being opened takes its subtree out of reach. Its descendants record only its pid, which may already have gone to a new process, so nothing can establish that they belong to the tree, and the walk deliberately does not guess (terminating an unrelated process would be the worse failure).
- The orphan heuristic (a child created before its recorded parent is an orphan whose parent pid was recycled) is a comparison of two recorded creation times, so a sufficiently large backward clock step between them can make a stale orphan look like a descendant. This is the same heuristic
getSubprocesses()has always used.
The deterministic answer is a job object per launch: have the child-process launcher (Win32ChildProcess) create a job object, assign the child to it while still suspended so nothing can be spawned outside it, keep the handle with the child, and expose a termination that calls TerminateJobObject. Membership is then tracked by the kernel, independent of pids and clocks, and includes grandchildren spawned at any time. Things to work out:
- The session already runs inside one job with
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | BREAKAWAY_OK(initJobObjectinWin32System.cpp), so this needs nested jobs (Windows 8+), which RStudio's supported platforms have. - Children that legitimately break away (Chrome-style,
CREATE_BREAKAWAY_FROM_JOB) would escape the per-launch job as they escape the session's today; that should be documented rather than fought. - Which launches opt in. The Quarto job (
SessionQuartoJob.hpp) is the known consumer;terminateProcessTree(pid)should stay for callers that only have a pid. - A test on the Windows CI job with a
cmd.exe -> cmd.exe -> ping.exetree where the middle process exits before the stop, which the current walk cannot handle by design (OpenProcessTreeStopsAtExitedIntermediateinWin32SystemTests.cpppins that behavior today).
Activity