Skip to content

gh-159117: Preserve sys.setprofile callbacks when destroying a tracing profiler - #159664

Open
Cha-Imaa wants to merge 3 commits into
python:mainfrom
Cha-Imaa:gh-159117-profiler-dealloc
Open

Cha-Imaa wants to merge 3 commits into
python:mainfrom
Cha-Imaa:gh-159117-profiler-dealloc

Conversation

@Cha-Imaa

@Cha-Imaa Cha-Imaa commented Oct 18, 2026 •

Copy link
Copy Markdown

Fixes #159117.

profiler_dealloc() still called _PyEval_SetProfile(tstate, NULL, NULL), although _lsprof now uses sys.monitoring. Destroying an enabled profiler after its monitoring callbacks have been released therefore cleared an unrelated sys.setprofile() callback.

When a cyclic profiler is collected during ProfileTest.do_profiling(), this can stop the pure Python profiler before it has recorded all calls, producing empty or partial statistics.

Remove the obsolete cleanup and its unused headers. Monitoring callbacks hold references to the profiler while registered, so deallocation does not need to unregister them. Add a deterministic regression test that releases an enabled profiler's monitoring callbacks, destroys it, and verifies that an independent sys.setprofile() callback remains installed. Include a NEWS entry.

Validation on a Windows AMD64 CPython 3.16 debug build:

  • The new regression test fails before the C change (sys.getprofile() returns None) and passes afterward.
  • PCbuild\amd64\python_d.exe -m test -j2 test_profile test_profiling test_sys_setprofile test_monitoring test_pstats passes (629 tests reported, 16 skipped).
  • PCbuild\amd64\python_d.exe -m test -j1 -R3:3 test_profile test.test_profiling.test_tracing_profiler passes with no detected reference leaks.
  • Ruff, sphinx-lint, git diff --cached --check, and CPython patchcheck pass.

The original Windows PGO tail-call and s390x buildbot configurations were not tested locally.

@Cha-Imaa
Cha-Imaa requested a review from pablogsal as a code owner October 18, 2026 09:43
@python-cla-bot

python-cla-bot Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

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.

test_profile: ProfileTest.test_cprofile() failed on AMD64 Windows PGO Tailcall 3.x

1 participant