Repository navigation
Stop a step whose frame is left by an exception (sys.monitoring) - #2101
Open
Crispier_Tastier (siddesh-nexus) wants to merge 2 commits into
Open
Crispier_Tastier (siddesh-nexus) wants to merge 2 commits into
Crispier_Tastier (siddesh-nexus) wants to merge 2 commits into
Conversation
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>
Crispier_Tastier (siddesh-nexus)
requested a review
from a team
as a code owner
November 26, 2026 13:14
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2100.
Under
sys.monitoring(Python 3.12+), a function left by an exception getsPY_UNWINDand neverPY_RETURN._return_eventis where a step whose frame exits gets finished (Step Out) or handed on (Step Over), but_unwind_eventonly looked at exception breakpoints. The step stayed bound to a frame that would never run again, so Step Over / Step Out from araise(or from afinallywhile an exception propagates) never stopped and the program ran to the end. With no exception filter set,PY_UNWINDwasn'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.pyxand.care regenerated from it):_unwind_eventnow calls a new_unwind_stepping, which mirrors what_return_eventdoes when the stepped frame exits:_stop_on_return, as on a return.except/finallythat handles the exception.Both match the
sys.settracetracer (PYDEVD_USE_SYS_MONITORING=0) on the issue's program: Step Out fromraisestops at the call line in the caller, Step Over stops at thefinally. 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_eventstracksPY_UNWINDwhile a thread is suspended or stepping, also when no exception filter is set.PY_UNWINDcan only be enabled globally, andupdate_monitor_eventsisn't called when a step starts (it runs on suspend), so it's enabled at suspend time. That's the same window in whichPY_START/PY_RESUMEare already enabled._unwind_eventreturns right away when there are no exception breakpoints and no pending step..pyxregenerated withbuild_tools/generate_code.py. The.cwas generated with Cython 3.2.4 throughsetup_pydevd_cython.py --force-cython, only skipping the final compile (no MSVC on my machine). As a check, the same steps on the unmodified.pyxreproduce the committed.cbyte 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 wherei == 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 theraiseline, both from thefinallyline, and two Step Overs from the call. Each runs with exception filters["uncaught"]and[], and withjustMyCodeon 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:
PYDEVD_USE_CYTHON=YES, extension built withsetup_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 withmain's_pydevd_sys_monitoringfolder:main's tracer.test_sys_monitoring.py,test_debugger_json.pyandtest_debugger.pyfiltered on step/exception/monitoring/return_value/justmycode, plustests/debugpy/test_step.py,test_exception.py,test_breakpoints.py,test_justmycode.py,test_threads.pyandtest_stop_on_entry.py) has the same failing set on this branch and onmainfor every combination. The only differences are a fewattach_pidvariants oftest_systemexiton 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.PYDEVD_USE_CYTHON=NO, CPython 3.13.12 and 3.12.4): the same result. Onmainall 20 pydevd cases fail (the step never stops and the program finishes) and all 20 pass with this change. WithPYDEVD_USE_SYS_MONITORING=0(thesys.settracetracer), the same expectations pass too. The failing sets of the broader suites are the same onmainand on the branch.ruff checkis clean on the changed files.🤖 Generated with Claude Code