-
Notifications
You must be signed in to change notification settings - Fork 197
Add trigger_exception_handler() for post-mortem debugging of caught exceptions #1996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
5d2e8ad
cfb2695
835e93c
e643277
da7c378
2444836
ce5949d
33bd8dc
64d1a3f
4cc8eb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| "listen", | ||
| "log_to", | ||
| "trace_this_thread", | ||
| "trigger_exception_handler", | ||
| "wait_for_client", | ||
| ] | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1866,6 +1866,51 @@ def stop_monitoring(all_threads=False): | |
| thread_info.trace = False | ||
|
|
||
|
|
||
| # fmt: off | ||
| # IFDEF CYTHON | ||
| # cpdef bint suspend_current_thread_tracing(): | ||
| # cdef ThreadInfo thread_info | ||
| # ELSE | ||
| def suspend_current_thread_tracing(): | ||
| # ENDIF | ||
| # fmt: on | ||
| """ | ||
| Suspends tracing for the current thread and returns the previous state, | ||
| to be restored with resume_current_thread_tracing(). | ||
| """ | ||
| try: | ||
| thread_info = _thread_local_info.thread_info | ||
| except: | ||
| # Create the ThreadInfo if missing; monitoring events create it with | ||
| # tracing enabled by default, which would defeat the suspension. | ||
| thread_info = _get_thread_info(True, 1) | ||
| if thread_info is None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📍 src/debugpy/_vendored/pydevd/_pydevd_sys_monitoring/_pydevd_sys_monitoring.py:1875
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think how it is currently is actually correct. True is the default state (for everything except pydevd-daemon threads), in the sense that every other monitoring event sets thread_info like that if it doesn't exist. So that's what we should restore.
nshepperd marked this conversation as resolved.
|
||
| return False | ||
| previous_state = thread_info.trace | ||
|
nshepperd marked this conversation as resolved.
|
||
| thread_info.trace = False | ||
| return previous_state | ||
|
|
||
|
|
||
| # fmt: off | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📍 src/debugpy/_vendored/pydevd/_pydevd_sys_monitoring/_pydevd_sys_monitoring.py:1887 [verified] |
||
| # IFDEF CYTHON | ||
| # cpdef resume_current_thread_tracing(): | ||
| # cdef ThreadInfo thread_info | ||
| # ELSE | ||
| def resume_current_thread_tracing(): | ||
| # ENDIF | ||
| # fmt: on | ||
| """ | ||
| Resumes tracing for the current thread. | ||
| """ | ||
| try: | ||
| thread_info = _thread_local_info.thread_info | ||
| except: | ||
| thread_info = _get_thread_info(True, 1) | ||
| if thread_info is None: | ||
| return | ||
| thread_info.trace = True | ||
|
|
||
|
|
||
| def update_monitor_events(suspend_requested: Optional[bool]=None) -> None: | ||
| """ | ||
| This should be called when breakpoints change. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Python 3.14, an existing
__exception__local makesremove_exception_from_frame()raiseValueError, aborting the default stop and potentially masking the caught exception after an unconditional stop. Preserve and restore any previous binding rather than unconditionally removing it, with regression tests for bothas_uncaughtmodes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you're right. It does fail on 3.14. I've fixed this with a context manager that restores the previous state, using it in both places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, Claude says this is also a problem on HEAD. It shows me this repro testcase for the userUnhandled exception handler, which seemingly hangs the unit test unless the context manager/restore method is used there:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests do run in 3.14 as far as I know, so it should be working in main
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but you didn't have a unit test that puts an existing local named
__exception__, as far as i can see.