Repository navigation
Description
🤖 Issue Found by GPT 6
Problem
The sys.monitoring backend currently handles RAISE and RERAISE using the same callback as function-exit events:
cpdef handle_raise_event(self, code, instruction_offset, exception):
self._handle_exit_event(
sys.monitoring.events.RAISE, code, instruction_offset, exception)This ultimately calls inner_trace_callback with is_line_event=False, which records elapsed time and removes the current timing record.
However, raising an exception does not necessarily mean that the current frame is exiting. The exception may be caught within the same function.
For example:
import time
from line_profiler import LineProfiler
def example():
try:
raise ValueError("test")
except ValueError:
time.sleep(0.01)
time.sleep(0.01)
prof = LineProfiler(example)
prof.runcall(example)
prof.print_stats()The frame remains active throughout this function, but the RAISE callback removes its timing record.
This raises a question about whether timing continuity and attribution are correct when exceptions are caught and execution resumes.
Proposed investigation
CPython's sys.monitoring distinguishes several relevant events:
RAISE: An exception is raised.RERAISE: An existing exception is re-raised.PY_UNWIND: A Python function exits because of an exception.PY_RETURN: A Python function returns normally.
We should investigate whether the profiler needs to distinguish ending the current line's timing interval from ending the frame's execution.
In particular:
- Verify whether clearing the timing record on
RAISEorRERAISEcauses missing or incorrectly attributed timing when exceptions are handled locally. - Determine whether
PY_UNWINDshould be handled explicitly for exception-driven frame termination. - Ensure that timing on the line raising the exception is still recorded correctly.
- Preserve the existing behavior for exceptions propagating out of a function.
Simply removing RAISE and RERAISE handling is not necessarily correct. These events were added to address missing timing data in #355, so any change needs to preserve that fix.
Suggested regression tests
Cover the following scenarios using both available tracing backends:
- An exception raised and caught within the same function.
- An exception propagated through multiple profiled frames.
- An exception caught and explicitly re-raised.
try/finallyexecution during exception propagation.- A function that catches an exception and continues executing additional lines.
Verify hit counts and timing attribution, especially around the raising line, exception handler, and subsequent statements.
References
- Previous exception-timing regression: BUG (?): line data not reported when exception raised #355
- Related frame-timing changes: Fix bug with recursive calls not being counted correctly #444
- [Python
[sys.monitoring](https://docs.python.org/3/library/sys.monitoring.html)documentation](https://docs.python.org/3/library/sys.monitoring.html)
The immediate goal is to establish whether the current behavior causes incorrect measurements. If it does, we should correct the event handling without regressing exception-line timing.
Activity