Repository navigation
Add cleanup_before_exit plugin hook, CLI kills entire app tree #14443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
047a684
6cef7c4
130dad2
5b6c549
74c437b
f079400
171c0e5
32384e0
336f273
3279eb5
59342dc
7e38113
b5af4e7
871a0ac
10e1330
44c4a71
a2a62d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "tauri-cli": patch:bug | ||
| "@tauri-apps/cli": patch:bug | ||
| --- | ||
|
|
||
| `tauri dev` now kills the whole process tree of the running app when it restarts or exits, so processes spawned by the app (e.g. sidecars) are no longer left orphaned. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "tauri": minor:feat | ||
| --- | ||
|
|
||
| Added the `Plugin::cleanup_before_exit` hook and `plugin::Builder::on_cleanup_before_exit`, invoked by `App::cleanup_before_exit` right before the process exits so plugins can release resources such as sidecar processes. Unlike `RunEvent::Exit`, it also runs on exit paths that bypass the event loop such as `AppHandle::restart` on the main thread. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,22 @@ pub trait Plugin<R: Runtime>: Send { | |
| #[allow(unused_variables)] | ||
| fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {} | ||
|
|
||
| /// Callback invoked by [`App::cleanup_before_exit`](crate::App::cleanup_before_exit) right before the process exits. | ||
| /// | ||
| /// Use it to release resources the OS does not reclaim on its own, such as child processes (sidecars). | ||
| /// | ||
| /// Unlike [`RunEvent::Exit`], this hook also runs on exit paths that bypass the event loop, | ||
| /// e.g. [`AppHandle::restart`] when called on the main thread. On a regular exit, | ||
| /// [`Plugin::on_event`] receives [`RunEvent::Exit`] first and then this hook is called. | ||
| /// It does **not** run when the process is killed (e.g. by `tauri dev` on rebuild) | ||
| /// or when `std::process::exit` is called directly. | ||
| /// | ||
| /// The plugin store is locked while this hook runs, so it must not call APIs that access | ||
| /// other plugins such as [`AppHandle::remove_plugin`] or [`AppHandle::plugin`]. | ||
| /// No Tauri API should be used after it returns. | ||
| #[allow(unused_variables)] | ||
| fn cleanup_before_exit(&mut self, app: &AppHandle<R>) {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would also want to implement this for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for sure! I’ll add this to the TauriPlugin trait as well so it’s accessible via tauri::plugin::Builder. |
||
|
|
||
| // TODO: Change this to `run_invoke_handler` in v3 | ||
| /// Extend commands to [`crate::Builder::invoke_handler`]. | ||
| #[allow(unused_variables)] | ||
|
|
@@ -117,6 +133,7 @@ type SetupHook<R, C> = | |
| type OnWindowReady<R> = dyn FnMut(Window<R>) + Send; | ||
| type OnWebviewReady<R> = dyn FnMut(Webview<R>) + Send; | ||
| type OnEvent<R> = dyn FnMut(&AppHandle<R>, &RunEvent) + Send; | ||
| type OnCleanupBeforeExit<R> = dyn FnMut(&AppHandle<R>) + Send; | ||
| type OnNavigation<R> = dyn Fn(&Webview<R>, &Url) -> bool + Send; | ||
| type OnPageLoad<R> = dyn FnMut(&Webview<R>, &PageLoadPayload<'_>) + Send; | ||
| type OnDrop<R> = dyn FnOnce(AppHandle<R>) + Send; | ||
|
|
@@ -272,6 +289,7 @@ pub struct Builder<R: Runtime, C: DeserializeOwned = ()> { | |
| on_window_ready: Box<OnWindowReady<R>>, | ||
| on_webview_ready: Box<OnWebviewReady<R>>, | ||
| on_event: Box<OnEvent<R>>, | ||
| on_cleanup_before_exit: Box<OnCleanupBeforeExit<R>>, | ||
| on_drop: Option<Box<OnDrop<R>>>, | ||
| uri_scheme_protocols: HashMap<String, Arc<UriSchemeProtocol<R>>>, | ||
| } | ||
|
|
@@ -289,6 +307,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> { | |
| on_window_ready: Box::new(|_| ()), | ||
| on_webview_ready: Box::new(|_| ()), | ||
| on_event: Box::new(|_, _| ()), | ||
| on_cleanup_before_exit: Box::new(|_| ()), | ||
| on_drop: None, | ||
| uri_scheme_protocols: Default::default(), | ||
| } | ||
|
|
@@ -568,6 +587,32 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> { | |
| self | ||
| } | ||
|
|
||
| /// Callback invoked when the application is performing cleanup before exit. | ||
| /// | ||
| /// See [`Plugin::cleanup_before_exit`] for details. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// use tauri::{plugin::{Builder, TauriPlugin}, Runtime}; | ||
| /// | ||
| /// fn init<R: Runtime>() -> TauriPlugin<R> { | ||
| /// Builder::new("example") | ||
| /// .on_cleanup_before_exit(|app| { | ||
| /// // release OS resources such as child processes here | ||
| /// }) | ||
| /// .build() | ||
| /// } | ||
| /// ``` | ||
| #[must_use] | ||
| pub fn on_cleanup_before_exit<F>(mut self, on_cleanup_before_exit: F) -> Self | ||
| where | ||
| F: FnMut(&AppHandle<R>) + Send + 'static, | ||
| { | ||
| self.on_cleanup_before_exit = Box::new(on_cleanup_before_exit); | ||
| self | ||
| } | ||
|
|
||
| /// Callback invoked when the plugin is dropped. | ||
| /// | ||
| /// # Examples | ||
|
|
@@ -741,6 +786,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> { | |
| on_window_ready: self.on_window_ready, | ||
| on_webview_ready: self.on_webview_ready, | ||
| on_event: self.on_event, | ||
| on_cleanup_before_exit: self.on_cleanup_before_exit, | ||
| on_drop: self.on_drop, | ||
| uri_scheme_protocols: self.uri_scheme_protocols, | ||
| }) | ||
|
|
@@ -768,6 +814,7 @@ pub struct TauriPlugin<R: Runtime, C: DeserializeOwned = ()> { | |
| on_window_ready: Box<OnWindowReady<R>>, | ||
| on_webview_ready: Box<OnWebviewReady<R>>, | ||
| on_event: Box<OnEvent<R>>, | ||
| on_cleanup_before_exit: Box<OnCleanupBeforeExit<R>>, | ||
| on_drop: Option<Box<OnDrop<R>>>, | ||
| uri_scheme_protocols: HashMap<String, Arc<UriSchemeProtocol<R>>>, | ||
| } | ||
|
|
@@ -848,6 +895,10 @@ impl<R: Runtime, C: DeserializeOwned> Plugin<R> for TauriPlugin<R, C> { | |
| (self.on_event)(app, event) | ||
| } | ||
|
|
||
| fn cleanup_before_exit(&mut self, app: &AppHandle<R>) { | ||
| (self.on_cleanup_before_exit)(app) | ||
| } | ||
|
|
||
| fn extend_api(&mut self, invoke: Invoke<R>) -> bool { | ||
| (self.invoke_handler)(invoke) | ||
| } | ||
|
|
@@ -980,6 +1031,16 @@ impl<R: Runtime> PluginStore<R> { | |
| .for_each(|plugin| plugin.on_event(app, event)) | ||
| } | ||
|
|
||
| /// Runs the cleanup_before_exit hook for all plugins in the store. | ||
| pub(crate) fn cleanup_before_exit(&mut self, app: &AppHandle<R>) { | ||
| self.store.iter_mut().for_each(|plugin| { | ||
| #[cfg(feature = "tracing")] | ||
| let _span = | ||
| tracing::trace_span!("plugin::hooks::cleanup_before_exit", name = plugin.name()).entered(); | ||
| plugin.cleanup_before_exit(app) | ||
| }) | ||
| } | ||
|
|
||
| /// Runs the plugin [`Plugin::extend_api`] hook if it exists. Returns whether the invoke message was handled or not. | ||
| /// | ||
| /// The message is not handled when the plugin exists **and** the command does not. | ||
|
|
@@ -1060,3 +1121,31 @@ impl<'de> Deserialize<'de> for PermissionState { | |
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{Builder, TauriPlugin}; | ||
| use crate::test::{MockRuntime, mock_builder, mock_context, noop_assets}; | ||
| use std::sync::{ | ||
| Arc, | ||
| atomic::{AtomicBool, Ordering}, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn builder_cleanup_before_exit_hook_runs_on_app_cleanup() { | ||
| let called = Arc::new(AtomicBool::new(false)); | ||
| let called_ = called.clone(); | ||
| let plugin: TauriPlugin<MockRuntime> = Builder::new("cleanup-test") | ||
| .on_cleanup_before_exit(move |_app| called_.store(true, Ordering::SeqCst)) | ||
| .build(); | ||
|
|
||
| let app = mock_builder() | ||
| .plugin(plugin) | ||
| .build(mock_context(noop_assets())) | ||
| .unwrap(); | ||
|
|
||
| app.cleanup_before_exit(); | ||
|
|
||
| assert!(called.load(Ordering::SeqCst)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.