Skip to content

Commit a1626f9

Browse files
wesbillmanPinky
andauthored
fix(desktop): preserve relaunch through mesh shutdown (#1966)
Signed-off-by: Wes <[email protected]> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
1 parent 19dc33b commit a1626f9

4 files changed

Lines changed: 87 additions & 17 deletions

File tree

‎desktop/src-tauri/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎desktop/src-tauri/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ keyring = { version = "3.6.3", default-features = false, features = ["apple-nati
4747
security-framework = { version = "3.7.0", features = ["OSX_10_15"] }
4848
window-vibrancy = "0.6"
4949
user-idle = { version = "0.6", default-features = false }
50+
plist = "1"
5051

5152
[target.'cfg(windows)'.dependencies]
5253
windows-sys = { version = "0.61", features = ["Win32_Security", "Win32_Storage_FileSystem", "Win32_System_JobObjects", "Win32_System_Registry", "Win32_System_Threading", "Win32_Foundation"] }

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ use huddle::{
5151
};
5252
use managed_agents::{backfill_persona_snapshots, ensure_nest, try_regenerate_nest};
5353
#[cfg(all(feature = "mesh-llm", target_os = "macos"))]
54-
use shutdown::hard_exit_after_mesh_shutdown;
55-
use shutdown::shutdown_managed_agents;
56-
#[cfg(feature = "mesh-llm")]
57-
use shutdown::shutdown_mesh_runtime;
54+
use shutdown::{hard_exit_after_mesh_shutdown, relaunch_after_mesh_shutdown};
55+
use shutdown::{is_restart_request, shut_down_app};
5856
use std::sync::{
5957
atomic::{AtomicBool, Ordering},
6058
Arc,
@@ -967,19 +965,20 @@ pub fn run() {
967965
shutdown::install_signal_handler(app.handle().clone(), Arc::clone(&shutdown_done));
968966

969967
let run_shutdown_done = Arc::clone(&shutdown_done);
968+
let restart_requested = Arc::new(AtomicBool::new(false));
970969
app.run(move |app_handle, event| match event {
971-
RunEvent::ExitRequested { .. } | RunEvent::Exit => {
972-
app_handle
973-
.state::<AppState>()
974-
.shutdown_started
975-
.store(true, Ordering::SeqCst);
976-
if !run_shutdown_done.swap(true, Ordering::SeqCst) {
977-
prevent_sleep::release(&app_handle.state::<AppState>().prevent_sleep);
978-
if let Err(error) = shutdown_managed_agents(app_handle) {
979-
eprintln!("buzz-desktop: failed to stop managed agents: {error}");
980-
}
981-
#[cfg(feature = "mesh-llm")]
982-
shutdown_mesh_runtime(app_handle);
970+
RunEvent::ExitRequested { code, .. } => {
971+
if is_restart_request(code) {
972+
restart_requested.store(true, Ordering::SeqCst);
973+
}
974+
shut_down_app(app_handle, &run_shutdown_done);
975+
}
976+
RunEvent::Exit => {
977+
shut_down_app(app_handle, &run_shutdown_done);
978+
979+
#[cfg(all(feature = "mesh-llm", target_os = "macos"))]
980+
if restart_requested.load(Ordering::SeqCst) {
981+
relaunch_after_mesh_shutdown(app_handle);
983982
}
984983

985984
// AppKit terminates through libc exit(), which runs C++ static

‎desktop/src-tauri/src/shutdown.rs‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,27 @@ use crate::managed_agents::{
55
self, kill_stale_tracked_processes, load_managed_agents, save_managed_agents,
66
sync_managed_agent_processes, BackendKind, ManagedAgentProcess,
77
};
8-
use crate::util;
8+
use crate::{prevent_sleep, util};
9+
10+
pub(crate) fn is_restart_request(code: Option<i32>) -> bool {
11+
code == Some(tauri::RESTART_EXIT_CODE)
12+
}
13+
14+
pub(crate) fn shut_down_app(app: &tauri::AppHandle, shutdown_done: &std::sync::atomic::AtomicBool) {
15+
use std::sync::atomic::Ordering;
16+
17+
app.state::<AppState>()
18+
.shutdown_started
19+
.store(true, Ordering::SeqCst);
20+
if !shutdown_done.swap(true, Ordering::SeqCst) {
21+
prevent_sleep::release(&app.state::<AppState>().prevent_sleep);
22+
if let Err(error) = shutdown_managed_agents(app) {
23+
eprintln!("buzz-desktop: failed to stop managed agents: {error}");
24+
}
25+
#[cfg(feature = "mesh-llm")]
26+
shutdown_mesh_runtime(app);
27+
}
28+
}
929

1030
/// Install SIGINT/SIGTERM/SIGHUP cleanup on ctrlc's dedicated handler thread.
1131
#[cfg(unix)]
@@ -33,6 +53,43 @@ pub(crate) fn install_signal_handler(
3353
}
3454
}
3555

56+
#[cfg(all(feature = "mesh-llm", target_os = "macos"))]
57+
fn updated_macos_binary(current_binary: &std::path::Path) -> Option<std::path::PathBuf> {
58+
let macos_directory = current_binary.parent()?;
59+
if macos_directory.file_name()? != "MacOS" {
60+
return None;
61+
}
62+
let contents_directory = macos_directory.parent()?;
63+
if contents_directory.file_name()? != "Contents" {
64+
return None;
65+
}
66+
let info_plist =
67+
plist::from_file::<_, plist::Dictionary>(contents_directory.join("Info.plist")).ok()?;
68+
let binary_name = info_plist.get("CFBundleExecutable")?.as_string()?;
69+
Some(macos_directory.join(binary_name))
70+
}
71+
72+
#[cfg(all(feature = "mesh-llm", target_os = "macos"))]
73+
pub(crate) fn relaunch_after_mesh_shutdown(app: &tauri::AppHandle) -> ! {
74+
use std::process::Command;
75+
76+
tauri_plugin_single_instance::destroy(app);
77+
let env = app.env();
78+
match tauri::process::current_binary(&env) {
79+
Ok(current_binary) => {
80+
let binary = updated_macos_binary(&current_binary).unwrap_or(current_binary);
81+
if let Err(error) = Command::new(binary)
82+
.args(env.args_os.iter().skip(1))
83+
.spawn()
84+
{
85+
eprintln!("buzz-desktop: failed to relaunch app: {error}");
86+
}
87+
}
88+
Err(error) => eprintln!("buzz-desktop: failed to locate app for relaunch: {error}"),
89+
}
90+
hard_exit_after_mesh_shutdown();
91+
}
92+
3693
#[cfg(all(feature = "mesh-llm", target_os = "macos"))]
3794
pub(crate) fn hard_exit_after_mesh_shutdown() -> ! {
3895
// SAFETY: all Buzz-managed subprocesses and the embedded Mesh runtime have
@@ -201,3 +258,15 @@ pub(crate) fn shutdown_managed_agents(app: &tauri::AppHandle) -> Result<(), Stri
201258

202259
Ok(())
203260
}
261+
262+
#[cfg(test)]
263+
mod tests {
264+
use super::is_restart_request;
265+
266+
#[test]
267+
fn only_tauri_restart_exit_code_requests_a_relaunch() {
268+
assert!(is_restart_request(Some(tauri::RESTART_EXIT_CODE)));
269+
assert!(!is_restart_request(None));
270+
assert!(!is_restart_request(Some(0)));
271+
}
272+
}

0 commit comments

Comments
 (0)