diff --git a/src-tauri/tauri-plugin-plugin-runtime/src/lib.rs b/src-tauri/tauri-plugin-plugin-runtime/src/lib.rs index 3281672f..a5798e20 100644 --- a/src-tauri/tauri-plugin-plugin-runtime/src/lib.rs +++ b/src-tauri/tauri-plugin-plugin-runtime/src/lib.rs @@ -2,6 +2,7 @@ extern crate core; use crate::manager::PluginManager; use log::info; +use std::process::exit; use tauri::plugin::{Builder, TauriPlugin}; use tauri::{Manager, RunEvent, Runtime, State}; use tokio::sync::Mutex; @@ -25,11 +26,13 @@ pub fn init() -> TauriPlugin { }) .on_event(|app, e| match e { // TODO: Also exit when app is force-quit (eg. cmd+r in IntelliJ runner) - RunEvent::Exit => { + RunEvent::ExitRequested { api, .. } => { + api.prevent_exit(); tauri::async_runtime::block_on(async move { info!("Exiting plugin runtime due to app exit"); let manager: State> = app.state(); manager.lock().await.cleanup().await; + exit(0); }); } _ => {} diff --git a/src-tauri/tauri-plugin-plugin-runtime/src/manager.rs b/src-tauri/tauri-plugin-plugin-runtime/src/manager.rs index 67a4f7b9..03ee3ee4 100644 --- a/src-tauri/tauri-plugin-plugin-runtime/src/manager.rs +++ b/src-tauri/tauri-plugin-plugin-runtime/src/manager.rs @@ -1,4 +1,5 @@ use log::{debug, info}; +use std::time::Duration; use tauri::{AppHandle, Manager, Runtime}; use tokio::sync::watch::Sender; use tonic::transport::Channel; @@ -32,6 +33,9 @@ impl PluginManager { pub async fn cleanup(&mut self) { self.kill_tx.send_replace(true); + + // Give it a bit of time to kill + tokio::time::sleep(Duration::from_millis(500)).await; } pub async fn run_import(&mut self, data: &str) -> Result { diff --git a/src-tauri/tauri-plugin-plugin-runtime/src/nodejs.rs b/src-tauri/tauri-plugin-plugin-runtime/src/nodejs.rs index c2c678ba..dd88a234 100644 --- a/src-tauri/tauri-plugin-plugin-runtime/src/nodejs.rs +++ b/src-tauri/tauri-plugin-plugin-runtime/src/nodejs.rs @@ -55,15 +55,16 @@ pub async fn node_start( plugin_runtime_main, ); - let (_, child) = app + let cmd = app .shell() .sidecar("yaaknode") .expect("yaaknode not found") .env("YAAK_GRPC_PORT_FILE_PATH", port_file_path.clone()) .env("YAAK_PLUGINS_DIR", plugins_dir) - .args(&[plugin_runtime_main]) - .spawn() - .expect("yaaknode failed to start"); + .args(&[plugin_runtime_main]); + + println!("Waiting on plugin runtime"); + let (_, child) = cmd.spawn().expect("yaaknode failed to start"); let mut kill_rx = kill_rx.clone(); @@ -75,6 +76,7 @@ pub async fn node_start( .expect("Kill channel errored"); info!("Killing plugin runtime"); child.kill().expect("Failed to kill plugin runtime"); + info!("Killed plugin runtime"); return; });