GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-152356: Fix Windows blocking sampling after target process exit by zainnadeem786 · Pull Request #152471 · python/cpython

Conversation

@zainnadeem786

@zainnadeem786 zainnadeem786 commented Jun 28, 2026

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@zainnadeem786
zainnadeem786 requested a review from pablogsal as a code owner June 28, 2026 08:28
@bedevere-app bedevere-app Bot mentioned this pull request Jun 28, 2026
profiling.sampling run --blocking hangs and writes an empty profile on Windows #152356
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Jun 28, 2026
pablogsal
pablogsal reviewed Jun 28, 2026
View reviewed changes
Comment thread Lib/test/test_profiling/test_sampling_profiler/test_blocking.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@zainnadeem786
pythongh-152356: Fix Windows blocking sampling after process exit
42f0941
@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
pablogsal approved these changes Jun 28, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Jun 28, 2026
@pablogsal

pablogsal commented Jun 28, 2026

Copy link
Copy Markdown
Member

LGTm thanks for the pr!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal pablogsal added the needs backport to 3.15 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

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Jun 28, 2026

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.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Jun 28, 2026
@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Jun 28, 2026
pablogsal pushed a commit that referenced this pull request Jun 28, 2026
@miss-islington
[3.15] gh-152356: Fix Windows blocking sampling after target process …
f08fc4a
…exit (GH-152471) (#152512)
@chris-eibl chris-eibl mentioned this pull request Jul 18, 2026
_remote_debugging: raising Invalid bytes length on codeobjects with linetable > 4096 #151022
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@pablogsal pablogsal pablogsal approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

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

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@zainnadeem786 @pablogsal
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.