Skip to content

fix: preserve frame timing until exception unwind - #449

Open
manav4u wants to merge 4 commits into
pyutils:mainfrom
manav4u:fix-exception-unwind-445
Open

manav4u wants to merge 4 commits into
pyutils:mainfrom
manav4u:fix-exception-unwind-445

Conversation

@manav4u

@manav4u manav4u commented Nov 10, 2026

Copy link
Copy Markdown

Fixes #445.

Use PY_UNWIND, rather than RAISE and RERAISE, to finish a frame's timing when an exception leaves it. A raise may be handled without leaving the frame or producing another line event. Clearing its timing record at that point loses work done before the next line.

A concrete case is with Context(): raise ValueError() on one source line, with an unprofiled Context.__exit__ that does work. With a 30ms sleep in __exit__, the current monitoring backend charges almost no time to that line; legacy tracing charges the sleep there. The change retains that time and still clears the record when the exception actually unwinds the frame.

Add eight context-exit cases covering direct raises, exceptions from an unprofiled callee, explicit reraises and exceptions during finally, each with suppression on/off. Run them under both tracing backends. Check exit-time attribution, continuation, frame-state cleanup and exclusion of later idle time. Add two tests for existing unwind-callback forwarding and callback/event-mask restoration. Update the private callback documentation and changelog; the shared line-timing algorithm and legacy backend are unchanged.

Local validation on Linux:

  • The eight monitoring cases fail before the fix and pass afterward on CPython 3.13.17, 3.14.9 and 3.15.1; all eight legacy controls pass. On 3.12.15, four monitoring cases fail before the fix and all eight pass afterward. The other four already receive a new line event on that interpreter.
  • Full tests directory with the candidate: 287 passed / 44 skipped on 3.10.12; 327 passed / 3 skipped / 1 xfailed on 3.12.15; 331 passed on 3.13.17; 330 passed / 1 skipped on both 3.14.9 and 3.15.1. This includes the existing exception-line, recursive-call, generator/coroutine and trace-coexistence tests, plus the applicable Cython tests.
  • Configured critical flake8 checks and git diff --check pass. Ruff reports the same six existing warnings in the touched line-profiler test module as on the base.

This is local validation, not a remote-CI or all-platform result. Each full run reports an existing unclosed-file warning in test_docs_conf. Type and documentation checks, other operating systems and minimum-Cython compatibility were not run. No performance claim is made. Investigation, patch and tests were AI-assisted; independent human review is pending.

@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.56%. Comparing base (0c932ab) to head (ed4a90c).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #449      +/-   ##
==========================================
- Coverage   85.12%   81.56%   -3.57%     
==========================================
  Files          21       21              
  Lines        2413     2484      +71     
  Branches      376      410      +34     
==========================================
- Hits         2054     2026      -28     
- Misses        259      343      +84     
- Partials      100      115      +15     

see 9 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d3400d7...ed4a90c. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Erotemic

Copy link
Copy Markdown
Member

This will need to be rebased on main.

This branch has not been deployed

No deployments
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.

Investigate exception-event handling in sys.monitoring profiling

2 participants