From 8c1baabe1c033d2fac86c3d04be29d5eb19b36c5 Mon Sep 17 00:00:00 2001 From: Jonathan McPherson Date: Wed, 1 Nov 2023 08:31:50 -0700 Subject: [PATCH] safety: ensure RMain exists before checking busy state --- crates/ark/src/interface.rs | 12 +++++++++++- crates/ark/src/kernel.rs | 9 ++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/ark/src/interface.rs b/crates/ark/src/interface.rs index a7770ef39..8500a8e8c 100644 --- a/crates/ark/src/interface.rs +++ b/crates/ark/src/interface.rs @@ -293,7 +293,7 @@ pub struct RMain { dap: Arc>, is_debugging: bool, - + /// Whether or not R itself is actively busy. /// This does not represent the busy state of the kernel. pub is_busy: bool, @@ -408,6 +408,16 @@ impl RMain { RMain::get_mut() } + /// Indicate whether RMain has been created and is initialized. + pub fn initialized() -> bool { + unsafe { + match &R_MAIN { + Some(main) => !main.initializing, + None => false, + } + } + } + /// Access a mutable reference to the singleton instance of this struct /// /// SAFETY: Accesses must occur after `start_r()` initializes it, and must diff --git a/crates/ark/src/kernel.rs b/crates/ark/src/kernel.rs index 61852519d..a2445e129 100644 --- a/crates/ark/src/kernel.rs +++ b/crates/ark/src/kernel.rs @@ -1,7 +1,7 @@ // // kernel.rs // -// Copyright (C) 2022 Posit Software, PBC. All rights reserved. +// Copyright (C) 2023 Posit Software, PBC. All rights reserved. // // @@ -73,8 +73,11 @@ impl Kernel { // Get the current busy status let busy = r_task(|| { - let main = RMain::get(); - main.is_busy + if RMain::initialized() { + RMain::get().is_busy + } else { + false + } }); self.send_event(PositronEvent::Busy(BusyEvent { busy })); }