mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-11-15 23:46:55 +01:00
fix(single-instance): release the lock on cleanup_before_exit instead of RunEvent::Exit (#3482)
* add configurable destroy_on_exit to single instance plugin * add change file * document destroy_on_exit * move the destroy to rely on the Resource close * add integration test * fix android/ios --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
1 parent
8dc878fe34
commit
aedd883cf3
8 files changed
+316
-68
No files matched your search
@@ -15,7 +15,7 @@
|
||||
)]
|
||||
#![cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
|
||||
use tauri::{AppHandle, Manager, Runtime, plugin::TauriPlugin};
|
||||
use tauri::{AppHandle, Manager, Resource, ResourceId, Runtime, plugin::TauriPlugin};
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[path = "platform_impl/windows.rs"]
|
||||
@@ -56,12 +56,27 @@ pub fn init<R: Runtime, F: FnMut(&AppHandle<R>, Vec<String>, String) + Send + Sy
|
||||
/// Releases the resources this plugin uses to detect other instances (the named mutex on
|
||||
/// Windows, the D-Bus name on Linux or the Unix socket on macOS).
|
||||
///
|
||||
/// The plugin calls this automatically on [`tauri::RunEvent::Exit`], so you normally don't need
|
||||
/// to call it yourself. Call it manually before terminating the process through means that skip
|
||||
/// that event, such as [`std::process::exit`], so a future instance of the app isn't mistaken
|
||||
/// for a still-running one.
|
||||
/// The plugin releases them automatically in [`AppHandle::cleanup_before_exit`], which Tauri runs
|
||||
/// right before the process exits or restarts (after your [`tauri::RunEvent::Exit`] handler
|
||||
/// returns), so you normally don't need to call this yourself and the app keeps being the single
|
||||
/// instance while your own exit cleanup runs. Call it manually to release the lock earlier, or
|
||||
/// before terminating the process through means that skip [`AppHandle::cleanup_before_exit`], such
|
||||
/// as [`std::process::exit`], so a future instance of the app isn't mistaken for a still-running
|
||||
/// one.
|
||||
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
|
||||
platform_impl::destroy(manager)
|
||||
if let Some(lock) = manager.try_state::<InstanceLockId>() {
|
||||
let _ = manager.resources_table().close(lock.0);
|
||||
}
|
||||
}
|
||||
|
||||
struct InstanceLockId(ResourceId);
|
||||
|
||||
/// Stores the platform lock in the app's resource table, which Tauri clears in
|
||||
/// [`AppHandle::cleanup_before_exit`], so the lock's [`Drop`] implementation releases it only once
|
||||
/// the app is really exiting.
|
||||
pub(crate) fn manage_instance_lock<R: Runtime, M: Manager<R>>(manager: &M, lock: impl Resource) {
|
||||
let rid = manager.resources_table().add(lock);
|
||||
manager.manage(InstanceLockId(rid));
|
||||
}
|
||||
|
||||
/// Builds the single-instance plugin.
|
||||
|
||||
@@ -5,14 +5,28 @@
|
||||
#[cfg(feature = "semver")]
|
||||
use crate::semver_compat::semver_compat_string;
|
||||
|
||||
use crate::SingleInstanceCallback;
|
||||
use crate::{SingleInstanceCallback, manage_instance_lock};
|
||||
use tauri::{
|
||||
AppHandle, Manager, RunEvent, Runtime,
|
||||
AppHandle, Resource, Runtime,
|
||||
plugin::{self, TauriPlugin},
|
||||
};
|
||||
use zbus::{blocking::Connection, interface, names::WellKnownName};
|
||||
|
||||
struct ConnectionHandle(Connection);
|
||||
/// Owns the D-Bus name, released when the app exits (see [`crate::destroy`]).
|
||||
struct InstanceLock {
|
||||
connection: Connection,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Resource for InstanceLock {}
|
||||
|
||||
impl Drop for InstanceLock {
|
||||
fn drop(&mut self) {
|
||||
if let Ok(name) = WellKnownName::try_from(self.name.as_str()) {
|
||||
let _ = self.connection.release_name(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SingleInstanceDBus<R: Runtime> {
|
||||
callback: Box<SingleInstanceCallback<R>>,
|
||||
@@ -26,8 +40,6 @@ impl<R: Runtime> SingleInstanceDBus<R> {
|
||||
}
|
||||
}
|
||||
|
||||
struct DBusName(String);
|
||||
|
||||
pub fn init<R: Runtime>(
|
||||
callback: Box<SingleInstanceCallback<R>>,
|
||||
dbus_id: Option<String>,
|
||||
@@ -71,7 +83,13 @@ pub fn init<R: Runtime>(
|
||||
.build()
|
||||
{
|
||||
Ok(connection) => {
|
||||
app.manage(ConnectionHandle(connection));
|
||||
manage_instance_lock(
|
||||
app,
|
||||
InstanceLock {
|
||||
connection,
|
||||
name: dbus_name,
|
||||
},
|
||||
);
|
||||
}
|
||||
Err(zbus::Error::NameTaken) => {
|
||||
let connection = Connection::session()?;
|
||||
@@ -107,24 +125,7 @@ pub fn init<R: Runtime>(
|
||||
}
|
||||
}
|
||||
|
||||
app.manage(DBusName(dbus_name));
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.on_event(move |app, event| {
|
||||
if let RunEvent::Exit = event {
|
||||
destroy(app);
|
||||
}
|
||||
})
|
||||
.build()
|
||||
}
|
||||
|
||||
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
|
||||
if let Some(connection) = manager.try_state::<ConnectionHandle>()
|
||||
&& let Some(dbus_name) = manager
|
||||
.try_state::<DBusName>()
|
||||
.and_then(|name| WellKnownName::try_from(name.0.clone()).ok())
|
||||
{
|
||||
let _ = connection.0.release_name(dbus_name);
|
||||
}
|
||||
}
|
||||
@@ -8,15 +8,26 @@ use std::{
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use crate::SingleInstanceCallback;
|
||||
#[cfg(feature = "semver")]
|
||||
use crate::semver_compat::semver_compat_string;
|
||||
use crate::{SingleInstanceCallback, manage_instance_lock};
|
||||
use tauri::{
|
||||
AppHandle, Config, Manager, RunEvent, Runtime,
|
||||
AppHandle, Config, Manager, Resource, Runtime,
|
||||
plugin::{self, TauriPlugin},
|
||||
};
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
/// Owns the Unix socket file, removed when the app exits (see [`crate::destroy`]).
|
||||
struct InstanceLock(PathBuf);
|
||||
|
||||
impl Resource for InstanceLock {}
|
||||
|
||||
impl Drop for InstanceLock {
|
||||
fn drop(&mut self) {
|
||||
socket_cleanup(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init<R: Runtime>(cb: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
|
||||
plugin::Builder::new("single-instance")
|
||||
.setup(|app, _api| {
|
||||
@@ -32,6 +43,7 @@ pub fn init<R: Runtime>(cb: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
|
||||
ErrorKind::NotFound | ErrorKind::ConnectionRefused => {
|
||||
// This process claims itself as singleton as likely none exists
|
||||
socket_cleanup(&socket);
|
||||
manage_instance_lock(app, InstanceLock(socket.clone()));
|
||||
listen_for_other_instances(socket, app.clone(), cb);
|
||||
}
|
||||
_ => {
|
||||
@@ -45,19 +57,9 @@ pub fn init<R: Runtime>(cb: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.on_event(|app, event| {
|
||||
if let RunEvent::Exit = event {
|
||||
destroy(app);
|
||||
}
|
||||
})
|
||||
.build()
|
||||
}
|
||||
|
||||
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
|
||||
let socket = socket_path(manager.config(), manager.package_info());
|
||||
socket_cleanup(&socket);
|
||||
}
|
||||
|
||||
fn socket_path(config: &Config, _package_info: &tauri::PackageInfo) -> PathBuf {
|
||||
let identifier = config.identifier.replace(['.', '-'].as_ref(), "_");
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
#[cfg(feature = "semver")]
|
||||
use crate::semver_compat::semver_compat_string;
|
||||
|
||||
use crate::SingleInstanceCallback;
|
||||
use crate::{SingleInstanceCallback, manage_instance_lock};
|
||||
use tauri::{
|
||||
AppHandle, Manager, RunEvent, Runtime,
|
||||
AppHandle, Resource, Runtime,
|
||||
plugin::{self, TauriPlugin},
|
||||
};
|
||||
use windows_sys::Win32::{
|
||||
@@ -36,9 +36,24 @@ const WMCOPYDATA_SINGLE_INSTANCE_DATA_NUL: usize = 1543;
|
||||
/// How long the second instance waits for the first instance to take its arguments.
|
||||
const FORWARD_TIMEOUT_MS: u32 = 10_000;
|
||||
|
||||
struct MutexHandle(isize);
|
||||
/// Owns the named mutex and the event target window, released when the app exits
|
||||
/// (see [`crate::destroy`]).
|
||||
struct InstanceLock {
|
||||
mutex: isize,
|
||||
window: isize,
|
||||
}
|
||||
|
||||
struct TargetWindowHandle(isize);
|
||||
impl Resource for InstanceLock {}
|
||||
|
||||
impl Drop for InstanceLock {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
ReleaseMutex(self.mutex as _);
|
||||
CloseHandle(self.mutex as _);
|
||||
DestroyWindow(self.window as _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct UserData<R: Runtime> {
|
||||
app: AppHandle<R>,
|
||||
@@ -145,39 +160,26 @@ pub fn init<R: Runtime>(callback: Box<SingleInstanceCallback<R>>) -> TauriPlugin
|
||||
std::process::exit(0);
|
||||
}
|
||||
} else {
|
||||
app.manage(MutexHandle(hmutex as _));
|
||||
|
||||
let userdata = UserData {
|
||||
app: app.clone(),
|
||||
callback,
|
||||
};
|
||||
let userdata = Box::into_raw(Box::new(userdata));
|
||||
let hwnd = create_event_target_window::<R>(&class_name, &window_name, userdata);
|
||||
app.manage(TargetWindowHandle(hwnd as _));
|
||||
manage_instance_lock(
|
||||
app,
|
||||
InstanceLock {
|
||||
mutex: hmutex as _,
|
||||
window: hwnd as _,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.on_event(|app, event| {
|
||||
if let RunEvent::Exit = event {
|
||||
destroy(app);
|
||||
}
|
||||
})
|
||||
.build()
|
||||
}
|
||||
|
||||
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
|
||||
if let Some(hmutex) = manager.try_state::<MutexHandle>() {
|
||||
unsafe {
|
||||
ReleaseMutex(hmutex.0 as _);
|
||||
CloseHandle(hmutex.0 as _);
|
||||
}
|
||||
}
|
||||
if let Some(hwnd) = manager.try_state::<TargetWindowHandle>() {
|
||||
unsafe { DestroyWindow(hwnd.0 as _) };
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "system" fn single_instance_window_proc<R: Runtime>(
|
||||
hwnd: HWND,
|
||||
msg: u32,
|
||||
|
||||
Reference in new issue
Block a user