Repository navigation
mingw: avoid terminating unrelated processes during cleanup - #6493
Conversation
a7247b2 to
52eee28
Compare
mjcheetham
left a comment
There was a problem hiding this comment.
I tried to trace though this myself, and had AI look over some of it too. A few questions/comments that may end up being nits.
| } | ||
| for (;;) { | ||
| needed = 0; | ||
| GetSystemTimePreciseAsFileTime(snapshot_time); |
There was a problem hiding this comment.
AI points out that process creation times come from KeQuerySystemTime() (per the docs), which only advances once per tick, while this cutoff uses the precise clock.
So a process created after the cutoff but in the same tick records a time below it and is still accepted. A mid-walk PID reuse will only reliably be caught if we cross a tick boundary.
Should we use GetSystemTimeAsFileTime() instead, so both sides come from the same tick-based clock?
There was a problem hiding this comment.
I had missed this! Comparing tick-based creation times against a precise cutoff can mis-handle processes starte at the same tick. The cutoff now uses GetSystemTimeAsFileTime() with the existing strict comparison, conservatively excluding processes start at the same tick without adding waits or process opens to production cleanup.
| if (!processes[0].pid || | ||
| !GetProcessTimes(main_process, &processes[0].creation_time, | ||
| &exit_time, &kernel_time, &user_time)) | ||
| goto cleanup; | ||
| snapshot = get_process_snapshot(&snapshot_time); | ||
| if (!snapshot) | ||
| goto cleanup; |
There was a problem hiding this comment.
If we cannot get the root process' creation time then we skip getting the snapshot, and then we only kill the root and leave any descendants running.
Before we used to still try and walk the tree and kill off the descendants.
Is that understanding correct? Should we warn if we fail to get the root creation time (like we do if the snapshot creation fails)?
There was a problem hiding this comment.
Yes, query failure intentionally falls back to terminating only the root: without its identity and creation time, ancestry cannot be established safely, and terminate-only root handles remain supported. There is now a warning including the Windows error and explicitly stating that only that process is terminated.
| void test_win32_process__cleanup(void) {} | ||
| void test_win32_process__identity(void) {} |
There was a problem hiding this comment.
On non-Windows this will mean these tests silently succeed rather than cl_skip() like happens in u-mingw.c (example).
There was a problem hiding this comment.
Good point! The non-Windows identity stub now calls cl_skip(); the cleanup stub remains empty and the single outer platform conditional is unchanged.
| cl_assert_equal_i(ret, 0); | ||
| cl_assert_equal_i(process_open_count, ARRAY_SIZE(descendants)); | ||
| cl_assert_equal_i(peak_handle_count, handle_limit); | ||
| cl_assert(GetProcessHandleCount(GetCurrentProcess(), &count)); |
There was a problem hiding this comment.
AI tells me this may be fragile/flaky if something like an anti-virus/threadpool thread tries to open a handle. The handle count is process wide.
Maybe this is a nothing-burger in reality however. The 'solution' offered was to hook CloseHandle() and track only the handles opened via the hook.. which feels overkill?
There was a problem hiding this comment.
The process-wide handle counts are replaced with single-handle tracking through the existing OpenProcess() wrapper and a small CloseHandle() hook. The checks require the previous tracked handle to be closed before another opens and none to remain afterwards, while preserving the exact one-open-per-descendant assertion. Handles opened outside the wrapper are ignored.
52eee28 to
a6ee525
Compare
Process-tree cleanup reserves a 64 KiB PID array on the stack regardless of the tree's size. Move that storage to the heap so memory use follows the collected tree, retaining the existing 16,384-process traversal cap. Assisted-by: GPT-6.1 Sol Signed-off-by: Johannes Schindelin <[email protected]>
Process-tree cleanup can kill an older, unrelated orphan when Windows assigns the cleanup target a PID formerly held by the orphan's parent. Matching a parent PID alone does not prove ancestry; see git-for-windows#6435. Use `NtQuerySystemInformation` with `SystemProcessInformation` to obtain creation times alongside PID and parent PID without opening processes during discovery. Reject candidates that predate their claimed parents. Record a UTC `FILETIME` with `GetSystemTimeAsFileTime()` immediately before each query attempt and use the successful attempt's cutoff. Reject entries whose recorded creation time is at or after that cutoff. Windows records the `KeQuerySystemTime()` value at process creation and leaves it unchanged when system time is adjusted, as documented here: https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/nf-ntddk-psgetprocesscreatetimequadpart `GetSystemTimePreciseAsFileTime()` adds elapsed time since the last tick. A process born after the sample within that tick can therefore appear older than the cutoff. Use the same tick-based clock as process creation; Microsoft documents the distinction here: https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-kequerysystemtimeprecise Rejecting equality also excludes genuine children created in the capture-start tick. This conservative choice needs no waits or extra handles during cleanup. Only terminate a descendant if `GetProcessTimes()` on its termination handle reports the exact creation time captured for that PID. This rejects replacements from PID reuse after capture. The root's held handle pins its PID. Assisted-by: GPT-6.1 Sol Signed-off-by: Johannes Schindelin <[email protected]>
a6ee525 to
f373f67
Compare
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate. Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to `NtQuerySystemInformation()` to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process. This should also fix a frequent flake in VFS for Git's Functional Tests. This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit. This fixes #6435.
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate. Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to `NtQuerySystemInformation()` to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process. This should also fix a frequent flake in VFS for Git's Functional Tests. This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit. This fixes #6435.
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate. Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to `NtQuerySystemInformation()` to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process. This should also fix a frequent flake in VFS for Git's Functional Tests. This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit. This fixes #6435.
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate. Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to `NtQuerySystemInformation()` to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process. This should also fix a frequent flake in VFS for Git's Functional Tests. This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit. This fixes #6435.
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate. Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to `NtQuerySystemInformation()` to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process. This should also fix a frequent flake in VFS for Git's Functional Tests. This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit. This fixes #6435.
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate. Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to `NtQuerySystemInformation()` to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process. This should also fix a frequent flake in VFS for Git's Functional Tests. This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit. This fixes #6435.
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate. Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to `NtQuerySystemInformation()` to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process. This should also fix a frequent flake in VFS for Git's Functional Tests. This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit. This fixes #6435.
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate. Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to `NtQuerySystemInformation()` to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process. This should also fix a frequent flake in VFS for Git's Functional Tests. This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit. This fixes #6435.
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate. Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to `NtQuerySystemInformation()` to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process. This should also fix a frequent flake in VFS for Git's Functional Tests. This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit. This fixes #6435.
Windows can reuse the PID of an orphan's dead parent for a process Git is cleaning up. Our process-tree traversal currently matches only the parent PID, so it can mistake that unrelated orphan and its descendants for processes Git should terminate.
Let's exclude reused PIDs by ensuring that the process creation time matches the one obtained together with the PID when the process tree was enumerated, by switching to
NtQuerySystemInformation()to obtain creation times alongside parent PIDs. Candidates must not predate their claimed parents, and their recorded creation times must precede the snapshot-start cutoff. When opening a descendant for termination, its creation time must match the captured value, so a reused PID does not select a different process.This should also fix a frequent flake in VFS for Git's Functional Tests.
This supersedes the process-tree cleanup proposal in the following PR: #6474. That approach opens candidates during discovery and retains a handle for every accepted descendant. Here, discovery opens no process handles, and each descendant is opened only once, at termination. Handle usage stays bounded by the root plus one temporary handle rather than growing with the tree. The independent growable-storage change is a separate commit.
This fixes #6435.