gh-152356: Fix Windows blocking sampling after target process exit#152471
Merged
pablogsal merged 1 commit intoJun 28, 2026
Merged
Conversation
pablogsal
reviewed
Jun 28, 2026
c972652 to
42f0941
Compare
pablogsal
approved these changes
Jun 28, 2026
Member
|
LGTm thanks for the pr! |
|
Thanks @zainnadeem786 for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-152512 is a backport of this pull request to the 3.15 branch. |
pablogsal
pushed a commit
that referenced
this pull request
Jun 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes gh-152356.
On Windows,
profiling.sampling run --blockingcould hang indefinitely after the target process had already exited, leaving an empty binary profile that could not be replayed.The non-blocking sampling path already terminates correctly when the target exits, but the Windows blocking implementation classified the same process-exit condition differently.
Root Cause
During blocking sampling, the profiler pauses the target through the Windows remote debugging layer.
If the target process has already exited,
NtSuspendProcess()fails. The Windows implementation currently reports this as aRuntimeError.The sampling loop treats
RuntimeErroras a recoverable sampling failure and continues retrying, so profiling never finishes and the binary profile writer is never finalized.Other platforms already report terminated processes as
ProcessLookupError, which the sampling loop correctly treats as a terminal condition.Changes
This patch updates the Windows implementation of
_Py_RemoteDebug_StopAllThreads().When
NtSuspendProcess()fails because the target process has already terminated, the remote debugging layer now raisesProcessLookupErrorinstead ofRuntimeError.Existing
RuntimeErrorbehaviour is preserved for genuineNtSuspendProcess()failures while alive processes are unaffected.No other profiling behaviour changes.
Tests
Added a Windows-only regression test that:
profiling.sampling run --binary --mode=cpu --blockingagainst a short-lived Python script,Validation
Validated with:
PCbuild\build.bat -p x64 -dPCbuild\amd64\python_d.exe -m unittest -v test.test_profiling.test_sampling_profiler.test_blocking.TestBlockingModeCLIPCbuild\amd64\python_d.exe -m test test_profilingPCbuild\amd64\python_d.exe -m test test_importlib test_subprocessprofiling.sampling run --blockingandprofiling.sampling replaygit diff --checkAfter this change, Windows blocking sampling terminates correctly once the target process exits and produces a valid replayable binary profile.