@@ -72,6 +72,7 @@ use harp::routines::r_register_routines;
7272use harp:: session:: r_traceback;
7373use harp:: utils:: r_get_option;
7474use harp:: utils:: r_is_data_frame;
75+ use harp:: utils:: r_pairlist_any;
7576use harp:: utils:: r_poke_option_show_error_messages;
7677use harp:: R_MAIN_THREAD_ID ;
7778use libr:: R_BaseNamespace ;
@@ -745,15 +746,19 @@ impl RMain {
745746 let prompt_slice = unsafe { CStr :: from_ptr ( prompt_c) } ;
746747 let prompt = prompt_slice. to_string_lossy ( ) . into_owned ( ) ;
747748
748- // Detect browser prompts by inspecting the `RDEBUG` flag of the
749- // last frame on the stack. This is not 100% infallible, for
750- // instance `debug(readline)` followed by `n` will instantiate a
751- // user request prompt that will look like a browser prompt
752- // according to this heuristic. However it has the advantage of
753- // correctly detecting that continue prompts are top-level browser
754- // prompts in case of incomplete inputs within `browser()`.
755- let frame = harp:: session:: r_sys_frame ( n_frame) . unwrap ( ) ;
756- let browser = harp:: session:: r_env_is_browsed ( frame) . unwrap ( ) ;
749+ // Detect browser prompts by inspecting the `RDEBUG` flag of each
750+ // frame on the stack. If ANY of the frames are marked with `RDEBUG`,
751+ // then we assume we are in a debug state. We can't just check the
752+ // last frame, as sometimes frames are pushed onto the stack by lazy
753+ // evaluation of arguments or `tryCatch()` that aren't debug frames,
754+ // but we don't want to exit the debugger when we hit these, as R is
755+ // still inside a browser state. Should also handle cases like `debug(readline)`
756+ // followed by `n`.
757+ // https://github.com/posit-dev/positron/issues/2310
758+ let frames = RObject :: from ( harp:: session:: r_sys_frames ( ) . unwrap ( ) ) ;
759+ let browser = r_pairlist_any ( frames. sexp , |frame| {
760+ harp:: session:: r_env_is_browsed ( frame) . unwrap ( )
761+ } ) ;
757762
758763 // If there are frames on the stack and we're not in a browser prompt,
759764 // this means some user code is requesting input, e.g. via `readline()`
0 commit comments