Skip to content

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

Open
MrGreyfun wants to merge 1 commit into
python:mainfrom
MrGreyfun:codex/fix-exception-mode-idle
Open

MrGreyfun wants to merge 1 commit into
python:mainfrom
MrGreyfun:codex/fix-exception-mode-idle

Conversation

@MrGreyfun

@MrGreyfun MrGreyfun commented Oct 20, 2026 •

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant