Skip to content

Investigate exception-event handling in sys.monitoring profiling #445

Open

Description

@Erotemic

🤖 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:

  1. Verify whether clearing the timing record on RAISE or RERAISE causes missing or incorrectly attributed timing when exceptions are handled locally.
  2. Determine whether PY_UNWIND should be handled explicitly for exception-driven frame termination.
  3. Ensure that timing on the line raising the exception is still recorded correctly.
  4. 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/finally execution 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

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions