Skip to content

gh-152356: Fix Windows blocking sampling after target process exit#152471

Merged
pablogsal merged 1 commit into
python:mainfrom
zainnadeem786:fix/profiling-sampling-blocking-windows-exit
Jun 28, 2026
Merged

gh-152356: Fix Windows blocking sampling after target process exit#152471
pablogsal merged 1 commit into
python:mainfrom
zainnadeem786:fix/profiling-sampling-blocking-windows-exit

Conversation

@zainnadeem786

Copy link
Copy Markdown
Contributor

Summary

Fixes gh-152356.

On Windows, profiling.sampling run --blocking could 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 a RuntimeError.

The sampling loop treats RuntimeError as 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 raises ProcessLookupError instead of RuntimeError.

Existing RuntimeError behaviour is preserved for genuine NtSuspendProcess() failures while alive processes are unaffected.

No other profiling behaviour changes.

Tests

Added a Windows-only regression test that:

  • runs profiling.sampling run --binary --mode=cpu --blocking against a short-lived Python script,
  • verifies the command exits successfully,
  • verifies a non-empty binary profile is produced,
  • verifies the generated profile can be replayed successfully.

Validation

Validated with:

  • PCbuild\build.bat -p x64 -d
  • PCbuild\amd64\python_d.exe -m unittest -v test.test_profiling.test_sampling_profiler.test_blocking.TestBlockingModeCLI
  • PCbuild\amd64\python_d.exe -m test test_profiling
  • PCbuild\amd64\python_d.exe -m test test_importlib test_subprocess
  • manual verification of profiling.sampling run --blocking and profiling.sampling replay
  • git diff --check

After this change, Windows blocking sampling terminates correctly once the target process exits and produces a valid replayable binary profile.

Comment thread Lib/test/test_profiling/test_sampling_profiler/test_blocking.py Outdated
@zainnadeem786 zainnadeem786 force-pushed the fix/profiling-sampling-blocking-windows-exit branch from c972652 to 42f0941 Compare June 28, 2026 15:35
@zainnadeem786 zainnadeem786 requested a review from pablogsal June 28, 2026 15:36
@pablogsal

Copy link
Copy Markdown
Member

LGTm thanks for the pr!

@pablogsal pablogsal added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 28, 2026
@pablogsal pablogsal enabled auto-merge (squash) June 28, 2026 16:58
@pablogsal pablogsal merged commit 37b238f into python:main Jun 28, 2026
60 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @zainnadeem786 for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jun 28, 2026

Copy link
Copy Markdown

GH-152512 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 28, 2026
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.

profiling.sampling run --blocking hangs and writes an empty profile on Windows

2 participants