Skip to content

mingw: avoid terminating unrelated processes during cleanup - #6493

Merged
dscho merged 2 commits into
git-for-windows:mainfrom
dscho:safer-process-tree-termination
Nov 25, 2026
Merged

dscho merged 2 commits into
git-for-windows:mainfrom
dscho:safer-process-tree-termination

Conversation

@dscho

@dscho dscho commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

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.

@mjcheetham mjcheetham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread compat/win32/exit-process.h Outdated
}
for (;;) {
needed = 0;
GetSystemTimePreciseAsFileTime(snapshot_time);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +117 to +123
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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread t/unit-tests/u-win32-process.c Outdated
Comment on lines +4 to +5
void test_win32_process__cleanup(void) {}
void test_win32_process__identity(void) {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On non-Windows this will mean these tests silently succeed rather than cl_skip() like happens in u-mingw.c (example).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! The non-Windows identity stub now calls cl_skip(); the cleanup stub remains empty and the single outer platform conditional is unchanged.

Comment thread t/unit-tests/u-win32-process.c Outdated
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));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dscho
dscho force-pushed the safer-process-tree-termination branch from 52eee28 to a6ee525 Compare November 23, 2026 23:04
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]>
@dscho
dscho force-pushed the safer-process-tree-termination branch from a6ee525 to f373f67 Compare November 24, 2026 20:59
@dscho dscho added this to the Next release milestone Nov 25, 2026
@dscho
dscho merged commit 01d4ffc into git-for-windows:main Nov 25, 2026
167 of 174 checks passed
@dscho
dscho deleted the safer-process-tree-termination branch November 25, 2026 07:32
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 26, 2026
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.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 26, 2026
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.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 26, 2026
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.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 27, 2026
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.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 29, 2026
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.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 30, 2026
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.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 30, 2026
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.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 30, 2026
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.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 30, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

terminate_process_tree() kills unrelated processes when a PID has been reused (no creation-time check)

2 participants