@@ -178,11 +178,11 @@ def _get_unhandled_exception_frame(exc, depth: int) -> Optional[FrameType]:
178178# fmt: on
179179 try :
180180 # Unhandled frame has to be from the same exception.
181- if _thread_local_info .f_unhandled_exc is exc :
181+ if _thread_local_info .f_unhandled_exc_id is id ( exc ) :
182182 return _thread_local_info .f_unhandled_frame
183183 else :
184184 del _thread_local_info .f_unhandled_frame
185- del _thread_local_info .f_unhandled_exc
185+ del _thread_local_info .f_unhandled_exc_id
186186 raise AttributeError ('Not the same exception' )
187187 except :
188188 f_unhandled = _getframe (depth )
@@ -222,7 +222,7 @@ def _get_unhandled_exception_frame(exc, depth: int) -> Optional[FrameType]:
222222
223223 if f_unhandled is not None :
224224 _thread_local_info .f_unhandled_frame = f_unhandled
225- _thread_local_info .f_unhandled_exc = exc
225+ _thread_local_info .f_unhandled_exc_id = id ( exc )
226226 return _thread_local_info .f_unhandled_frame
227227
228228 return f_unhandled
@@ -966,6 +966,20 @@ def _raise_event(code, instruction, exc):
966966 if func_code_info .always_skip_code :
967967 return
968968
969+ # A new exception is starting. Invalidate the cache to prevent ABA bugs.
970+ try :
971+ del _thread_local_info .f_unhandled_frame
972+ del _thread_local_info .f_unhandled_exc_id
973+ except AttributeError :
974+ pass
975+
976+ has_caught_exception_breakpoint_in_pydb = (
977+ py_db .break_on_caught_exceptions or py_db .break_on_user_uncaught_exceptions or py_db .has_plugin_exception_breaks
978+ )
979+ if has_caught_exception_breakpoint_in_pydb :
980+ return
981+
982+
969983 frame = _getframe (1 )
970984 arg = (type (exc ), exc , exc .__traceback__ )
971985
@@ -1864,18 +1878,13 @@ def update_monitor_events(suspend_requested: Optional[bool]=None) -> None:
18641878
18651879 break_on_uncaught_exceptions = py_db .break_on_uncaught_exceptions
18661880
1867- if has_caught_exception_breakpoint_in_pydb :
1881+ if has_caught_exception_breakpoint_in_pydb or break_on_uncaught_exceptions :
18681882 required_events |= monitor .events .RAISE | monitor .events .PY_UNWIND
1869- # print('track RAISE')
18701883 monitor .register_callback (DEBUGGER_ID , monitor .events .RAISE , _raise_event )
18711884 monitor .register_callback (DEBUGGER_ID , monitor .events .PY_UNWIND , _unwind_event )
18721885 else :
1873- if break_on_uncaught_exceptions :
1874- required_events |= monitor .events .PY_UNWIND
1875- monitor .register_callback (DEBUGGER_ID , monitor .events .PY_UNWIND , _unwind_event )
1876- else :
1877- monitor .register_callback (DEBUGGER_ID , monitor .events .RAISE , None )
1878- monitor .register_callback (DEBUGGER_ID , monitor .events .PY_UNWIND , None )
1886+ monitor .register_callback (DEBUGGER_ID , monitor .events .RAISE , None )
1887+ monitor .register_callback (DEBUGGER_ID , monitor .events .PY_UNWIND , None )
18791888
18801889 has_breaks = py_db .has_plugin_line_breaks
18811890 if not has_breaks :
0 commit comments