Skip to content

Stop a step whose frame is left by an exception (sys.monitoring) - #2101

Open
Crispier_Tastier (siddesh-nexus) wants to merge 2 commits into
microsoft:mainfrom
siddesh-nexus:fix-step-exit-by-exception
Open

Crispier_Tastier (siddesh-nexus) wants to merge 2 commits into
microsoft:mainfrom
siddesh-nexus:fix-step-exit-by-exception

Conversation

@siddesh-nexus

Copy link
Copy Markdown

Fixes #2100.

Under sys.monitoring (Python 3.12+), a function left by an exception gets PY_UNWIND and never PY_RETURN. _return_event is where a step whose frame exits gets finished (Step Out) or handed on (Step Over), but _unwind_event only looked at exception breakpoints. The step stayed bound to a frame that would never run again, so Step Over / Step Out from a raise (or from a finally while an exception propagates) never stopped and the program ran to the end. With no exception filter set, PY_UNWIND wasn't tracked at all.

Thanks to the issue's reporter for the detailed analysis and the proposed patch, which this builds on.

What changed, in _pydevd_sys_monitoring.py (the .pyx and .c are regenerated from it):

  • _unwind_event now calls a new _unwind_stepping, which mirrors what _return_event does when the stepped frame exits:

    • Step Out (and Smart Step Into) stops in the caller, through _stop_on_return, as on a return.
    • Step Over becomes a step into, so it stops in the next line that runs: the except / finally that handles the exception.

    Both match the sys.settrace tracer (PYDEVD_USE_SYS_MONITORING=0) on the issue's program: Step Out from raise stops at the call line in the caller, Step Over stops at the finally. The step is handled after the exception-breakpoint checks, so a step requested while stopped at a user-unhandled exception during the unwind is handed on too.

  • update_monitor_events tracks PY_UNWIND while a thread is suspended or stepping, also when no exception filter is set. PY_UNWIND can only be enabled globally, and update_monitor_events isn't called when a step starts (it runs on suspend), so it's enabled at suspend time. That's the same window in which PY_START / PY_RESUME are already enabled. _unwind_event returns right away when there are no exception breakpoints and no pending step.

  • .pyx regenerated with build_tools/generate_code.py. The .c was generated with Cython 3.2.4 through setup_pydevd_cython.py --force-cython, only skipping the final compile (no MSVC on my machine). As a check, the same steps on the unmodified .pyx reproduce the committed .c byte for byte.

New tests, both using the issue's program with a conditional breakpoint (i == 1), so a lost step can't be hidden by the breakpoint hitting again, and both checking that each stop is still in the call where i == 1:

  • tests_python/test_debugger_json.py::test_step_frame_exited_by_exception (pydevd, _debugger_case_step_exception_exit.py): Step Out and Step Over from the raise line, both from the finally line, and two Step Overs from the call. Each runs with exception filters ["uncaught"] and [], and with justMyCode on and off (20 cases).
  • tests/debugpy/test_step.py::test_step_frame_exited_by_exception (debugpy, over DAP): the same 5 scenarios with both filter settings (10 cases per target).

Testing:

  • Compiled Cython tracer, on GitHub Actions in my fork (PYDEVD_USE_CYTHON=YES, extension built with setup_pydevd_cython.py build_ext --inplace, and the job checks that the compiled module is the one loaded). Matrix: Windows and Ubuntu × CPython 3.12 / 3.13 / 3.14, each run once with this branch and once with main's _pydevd_sys_monitoring folder:
    • Both new tests pass on all 6 OS/Python combinations with this change, and fail on all 6 with main's tracer.
    • The broader selection (test_sys_monitoring.py, test_debugger_json.py and test_debugger.py filtered on step/exception/monitoring/return_value/justmycode, plus tests/debugpy/ test_step.py, test_exception.py, test_breakpoints.py, test_justmycode.py, test_threads.py and test_stop_on_entry.py) has the same failing set on this branch and on main for every combination. The only differences are a few attach_pid variants of test_systemexit on Ubuntu, which fail on either side from run to run (the job doesn't build the attach helpers). The other failures, the same on both sides, are trio, remote debugger and attach-to-pid tests.
  • Pure-Python tracer, locally on Windows 11 (PYDEVD_USE_CYTHON=NO, CPython 3.13.12 and 3.12.4): the same result. On main all 20 pydevd cases fail (the step never stops and the program finishes) and all 20 pass with this change. With PYDEVD_USE_SYS_MONITORING=0 (the sys.settrace tracer), the same expectations pass too. The failing sets of the broader suites are the same on main and on the branch.
  • In one early local full-suite run (6 workers, 400+ tests), one of the new cases failed once. Its output wasn't kept. I couldn't reproduce it in 130+ runs since, locally or in CI.
  • ruff check is clean on the changed files.

🤖 Generated with Claude Code

Under sys.monitoring, a function left by an exception gets PY_UNWIND and
never PY_RETURN. `_return_event` is where a step whose frame exits is
finished (step out) or handed on (step over), but `_unwind_event` only
handled exception breakpoints, so the step stayed bound to a frame that no
longer runs and the program ran to the end without stopping. Also,
PY_UNWIND was only tracked while an exception filter was set.

- Handle the step in `_unwind_event` (new `_unwind_stepping`): a step out
  (or smart step into) stops in the caller, as on a return; a step over
  becomes a step into so it stops in the next line that runs (the
  `except`/`finally` handling the exception). This matches what the
  `sys.settrace` tracer does.
- Track PY_UNWIND while a thread is suspended or stepping even without
  exception breakpoints, with an early exit when there's no step to handle.
- Regenerate `_pydevd_sys_monitoring_cython.pyx` and `.c` (Cython 3.2.4).

Fixes microsoft#2100

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Runs the microsoft#2100 scenarios through the adapter, so they are covered by the
tox test run as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@siddesh-nexus

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

Step Out / Step Over never stops when the stepped function is left by an exception (sys.monitoring tracer, Python 3.12+)

1 participant