Skip to content

gh-159865: Fix profiling.sampling exception mode dropping blocked handlers - #159866

Merged
pablogsal merged 2 commits into
python:mainfrom
MrGreyfun:codex/fix-exception-mode-idle
Nov 24, 2026
Merged

pablogsal merged 2 commits into
python:mainfrom
MrGreyfun:codex/fix-exception-mode-idle

Conversation

@MrGreyfun

@MrGreyfun MrGreyfun commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

In profiling.sampling, exception mode (--mode exception) wrote an empty profile when the thread blocked inside an except block, for example in time.sleep(). The CLI also enabled the collector's skip_idle filter, which keeps only threads that hold the GIL or are on CPU, and a sleeping thread is neither. The unwinder already keeps only threads with an active exception, so exception mode doesn't need that filter.

  • Lib/profiling/sampling/cli.py: new _skip_idle_for_mode() replaces the inline expression at the four places that computed skip_idle (run, attach, live run, live attach). It returns False for wall, all and exception mode and True otherwise, so CPU and GIL modes are unchanged.
  • Lib/test/test_profiling/test_sampling_profiler/test_cli.py: test_skip_idle_mode_mapping checks the mapping. test_exception_mode_captures_blocked_handler profiles a script that sleeps in an except block and checks that the collapsed output contains the script; it fails without the change (empty output).

Tests:

  • PCbuild/amd64/python_d.exe -m test test_profiling.test_sampling_profiler.test_cli (57 tests)
  • PCbuild/amd64/python_d.exe -m test test_profiling.test_sampling_profiler.test_modes (15 tests)

@bedevere-app

bedevere-app Bot commented Oct 20, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@pablogsal pablogsal added the needs backport to 3.15 bugs and security fixes label Nov 24, 2026

@pablogsal pablogsal 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.

LGTM, thanks for fixing exception mode!

@pablogsal
pablogsal enabled auto-merge (squash) November 24, 2026 12:33
@pablogsal
pablogsal merged commit 9fd3a6f into python:main Nov 24, 2026
108 of 110 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @MrGreyfun 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 Nov 24, 2026

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 24, 2026
pablogsal pushed a commit that referenced this pull request Nov 24, 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.

2 participants