Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix(ensu): prevent duplicate desktop instances

setalpbbebb7619b2f
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 20b7cc6510..e8f5024f76 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2351,6 +2351,7 @@ dependencies = [
"tauri-plugin-fs",
"tauri-plugin-opener",
"tauri-plugin-process",
+ "tauri-plugin-single-instance",
"tauri-plugin-updater",
"uuid",
"windows-sys 0.61.2",
@@ -9462,6 +9463,22 @@ dependencies = [
"tauri-plugin",
]
+[[package]]
+name = "tauri-plugin-single-instance"
+version = "2.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5cd0cb5c412a5071b69bab6a6df1583cbb89460d4a83b6a24769b08d15b6b1e1"
+dependencies = [
+ "serde",
+ "serde_json",
+ "tauri",
+ "thiserror 2.0.18",
+ "tokio",
+ "tracing",
+ "windows-sys 0.60.2",
+ "zbus 5.15.0",
+]
+
[[package]]
name = "tauri-plugin-updater"
version = "2.10.1"
diff --git a/rust/apps/ensu/changes/multiple-instances.md b/rust/apps/ensu/changes/multiple-instances.md
new file mode 100644
index 0000000000..8553d61e5b
--- /dev/null
+++ b/rust/apps/ensu/changes/multiple-instances.md
@@ -0,0 +1 @@
+- Opening Ensu again on Windows and Linux now brings the existing window forward, preventing duplicate instances from making Your Notes unavailable.
diff --git a/rust/apps/ensu/src-tauri/Cargo.toml b/rust/apps/ensu/src-tauri/Cargo.toml
index 77da99191f..53a228b28b 100644
--- a/rust/apps/ensu/src-tauri/Cargo.toml
+++ b/rust/apps/ensu/src-tauri/Cargo.toml
@@ -29,7 +29,10 @@ libc = "0.2.0"
rustix = { version = "1.1.4", features = ["fs"] }
[target.'cfg(windows)'.dependencies]
-windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Storage_FileSystem", "Win32_System_SystemInformation"] }
+windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_SystemInformation", "Win32_System_Threading"] }
+
+[target.'cfg(target_os = "linux")'.dependencies]
+tauri-plugin-single-instance = "2.4.4"
[features]
custom-protocol = ["tauri/custom-protocol"]
diff --git a/rust/apps/ensu/src-tauri/src/main.rs b/rust/apps/ensu/src-tauri/src/main.rs
index 9897cfb488..cae6fa6abf 100644
--- a/rust/apps/ensu/src-tauri/src/main.rs
+++ b/rust/apps/ensu/src-tauri/src/main.rs
@@ -4,12 +4,18 @@ use tauri::{Manager, RunEvent, async_runtime};
mod commands;
mod logging;
+#[cfg(any(windows, target_os = "linux"))]
+mod single_instance;
fn main() {
logging::install_panic_hook();
logging::log("App", "starting Tauri backend");
- let app = tauri::Builder::default()
+ let builder = tauri::Builder::default();
+ #[cfg(any(windows, target_os = "linux"))]
+ let builder = builder.plugin(single_instance::plugin());
+
+ let app = builder
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_opener::init())
diff --git a/rust/apps/ensu/src-tauri/src/single_instance.rs b/rust/apps/ensu/src-tauri/src/single_instance.rs
new file mode 100644
index 0000000000..e498612601
--- /dev/null
+++ b/rust/apps/ensu/src-tauri/src/single_instance.rs
@@ -0,0 +1,63 @@
+use tauri::{AppHandle, Manager, plugin::TauriPlugin};
+
+use crate::logging;
+
+#[cfg(windows)]
+mod windows;
+
+pub fn plugin() -> TauriPlugin<tauri::Wry> {
+ #[cfg(target_os = "linux")]
+ {
+ tauri_plugin_single_instance::init(|app, _, _| focus_main_window(app))
+ }
+ #[cfg(windows)]
+ {
+ tauri::plugin::Builder::new("single-instance")
+ .setup(|app, _| {
+ match windows::Instance::acquire(&app.config().identifier)? {
+ Some(instance) => {
+ app.manage(instance);
+ }
+ None => {
+ app.cleanup_before_exit();
+ std::process::exit(0);
+ }
+ }
+ Ok(())
+ })
+ .on_event(|app, event| {
+ if matches!(event, tauri::RunEvent::Ready) {
+ let instance = app.state::<windows::Instance>();
+ let app = app.clone();
+ if let Err(error) = instance.listen(move || focus_main_window(&app)) {
+ logging::log(
+ "App",
+ format!("failed to listen for activation error={error}"),
+ );
+ }
+ }
+ })
+ .build()
+ }
+}
+
+fn focus_main_window(app: &AppHandle) {
+ logging::log(
+ "App",
+ "additional launch received; focusing existing window",
+ );
+ if let Some(window) = app.get_webview_window("main") {
+ for (operation, result) in [
+ ("show", window.show()),
+ ("restore", window.unminimize()),
+ ("focus", window.set_focus()),
+ ] {
+ if let Err(error) = result {
+ logging::log(
+ "App",
+ format!("failed to {operation} existing window error={error}"),
+ );
+ }
+ }
+ }
+}
diff --git a/rust/apps/ensu/src-tauri/src/single_instance/windows.rs b/rust/apps/ensu/src-tauri/src/single_instance/windows.rs
new file mode 100644
index 0000000000..8b7ce40e7d
--- /dev/null
+++ b/rust/apps/ensu/src-tauri/src/single_instance/windows.rs
@@ -0,0 +1,69 @@
+use std::io;
+use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
+use std::sync::Arc;
+
+use windows_sys::Win32::Foundation::{ERROR_ALREADY_EXISTS, GetLastError, WAIT_OBJECT_0};
+use windows_sys::Win32::System::Threading::{
+ CreateEventW, INFINITE, SetEvent, WaitForSingleObject,
+};
+
+pub(super) struct Instance {
+ activation: Arc<OwnedHandle>,
+}
+
+impl Instance {
+ #[expect(unsafe_code, reason = "Create, own, and signal a named Win32 event")]
+ pub(super) fn acquire(identifier: &str) -> io::Result<Option<Self>> {
+ let event_name = format!("Local\\{identifier}.activation")
+ .encode_utf16()
+ .chain(Some(0))
+ .collect::<Vec<_>>();
+ let raw_event = unsafe { CreateEventW(std::ptr::null(), 0, 0, event_name.as_ptr()) };
+ let already_running = unsafe { GetLastError() } == ERROR_ALREADY_EXISTS;
+ if raw_event.is_null() {
+ return Err(io::Error::last_os_error());
+ }
+ let activation = unsafe { OwnedHandle::from_raw_handle(raw_event) };
+ if already_running {
+ if unsafe { SetEvent(activation.as_raw_handle()) } == 0 {
+ return Err(io::Error::last_os_error());
+ }
+ return Ok(None);
+ }
+ Ok(Some(Self {
+ activation: Arc::new(activation),
+ }))
+ }
+
+ pub(super) fn listen(&self, on_activation: impl Fn() + Send + 'static) -> io::Result<()> {
+ let activation = Arc::clone(&self.activation);
+ std::thread::Builder::new()
+ .name("ensu-activation".into())
+ .spawn(move || {
+ loop {
+ match wait_for_activation(&activation) {
+ Ok(()) => on_activation(),
+ Err(error) => {
+ crate::logging::log(
+ "App",
+ format!("activation listener failed error={error}"),
+ );
+ break;
+ }
+ }
+ }
+ })?;
+ Ok(())
+ }
+}
+
+#[expect(
+ unsafe_code,
+ reason = "Wait on the activation event while its handle remains owned"
+)]
+fn wait_for_activation(activation: &OwnedHandle) -> io::Result<()> {
+ match unsafe { WaitForSingleObject(activation.as_raw_handle(), INFINITE) } {
+ WAIT_OBJECT_0 => Ok(()),
+ _ => Err(io::Error::last_os_error()),
+ }
+}