From 6d07a9641f5672fc45e7e421c315990e5c896fb9 Mon Sep 17 00:00:00 2001
From: Nessar
Date: Thu, 19 Nov 2026 23:04:28 +0100
Subject: [PATCH] Address single-instance review findings (#50)
- Reopen the window even when the relaunch's ack event is gone.
- Exit quietly instead of panicking on timeout or Win32 error; bound the startup gate wait.
- Fail softly in accept_relaunches; name the locks (Primary, HeldMutex).
- Share window_hwnd, derive REOPEN from TRAY_MESSAGE, name the marker.
- Test a crashed primary and a relaunch that finds no window.
Co-Authored-By: Claude Opus 5.5
---
crates/app/src/main.rs | 10 +-
crates/app/src/single_instance.rs | 228 ++++++++++++++++++++----------
crates/app/src/title_bar.rs | 8 +-
crates/app/src/tray.rs | 36 ++---
4 files changed, 183 insertions(+), 99 deletions(-)
diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs
index 7c9b11c..e70bcc5 100644
--- a/crates/app/src/main.rs
+++ b/crates/app/src/main.rs
@@ -262,8 +262,10 @@ fn init_theme(cx: &mut App) {
}
fn main() {
- let Some((_instance, starting)) =
- single_instance::launch().expect("Failed to check for a running instance")
+ let Some(single_instance::Primary {
+ instance: _instance,
+ starting,
+ }) = single_instance::launch()
else {
return;
};
@@ -288,12 +290,12 @@ fn main() {
};
match startup() {
Ok(setup) => gpui_kit::open_window(options, cx, |window, cx| {
- single_instance::attach(window);
+ single_instance::accept_relaunches(window);
cx.new(|cx| RootView::new(setup, window, cx))
})
.map(drop),
Err((lines, preferences)) => gpui_kit::open_window(options, cx, |window, cx| {
- single_instance::attach(window);
+ single_instance::accept_relaunches(window);
apply_theme(preferences.theme, window, cx);
window
.observe_window_appearance(move |window, cx| {
diff --git a/crates/app/src/single_instance.rs b/crates/app/src/single_instance.rs
index 77102f7..ea5c790 100644
--- a/crates/app/src/single_instance.rs
+++ b/crates/app/src/single_instance.rs
@@ -4,15 +4,14 @@ use std::marker::PhantomData;
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
use gpui_kit::Window;
-use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use windows::Win32::{
Foundation::{
- ERROR_TIMEOUT, HANDLE, HWND, LPARAM, LRESULT, WAIT_ABANDONED, WAIT_EVENT, WAIT_OBJECT_0,
- WAIT_TIMEOUT, WPARAM,
+ HANDLE, HWND, LPARAM, LRESULT, WAIT_ABANDONED, WAIT_EVENT, WAIT_OBJECT_0, WAIT_TIMEOUT,
+ WPARAM,
},
System::Threading::{
- CreateEventW, CreateMutexW, EVENT_MODIFY_STATE, INFINITE, OpenEventW, ReleaseMutex,
- SetEvent, WaitForMultipleObjects, WaitForSingleObject,
+ CreateEventW, CreateMutexW, EVENT_MODIFY_STATE, OpenEventW, ReleaseMutex, SetEvent,
+ WaitForMultipleObjects, WaitForSingleObject,
},
UI::{
Shell::{DefSubclassProc, RemoveWindowSubclass, SetWindowSubclass},
@@ -24,65 +23,91 @@ use windows::core::{BOOL, Error, HSTRING, PCWSTR, Result, w};
const STARTING: PCWSTR = w!("Local\\nessar.arena-tracker.starting");
const INSTANCE: PCWSTR = w!("Local\\nessar.arena-tracker.instance");
const WINDOW_MARKER: PCWSTR = w!("nessar.arena-tracker.main-window");
-const RELAY_TIMEOUT_MS: u32 = 5_000;
-const REOPEN: u32 = WM_APP + 2;
+// The marker is only a presence flag, its value is never read.
+const MARKED: HANDLE = HANDLE(1 as _);
+const REOPEN_SUBCLASS: usize = 0;
+// Past this wait another instance is alive, so the launch gives up quietly.
+const LAUNCH_TIMEOUT_MS: u32 = 5_000;
+// Shares the window's WM_APP message space with the tray's message.
+const REOPEN: u32 = crate::tray::TRAY_MESSAGE + 1;
// The acknowledgement event is the second object in the wait list.
const REOPENED: WAIT_EVENT = WAIT_EVENT(WAIT_OBJECT_0.0 + 1);
// Windows requires a mutex to be released on the thread that acquired it.
-pub struct SessionLock(OwnedHandle, PhantomData<*mut ()>);
+pub struct HeldMutex(OwnedHandle, PhantomData<*mut ()>);
-impl SessionLock {
- fn from_wait(handle: OwnedHandle, status: WAIT_EVENT) -> Result {
+impl HeldMutex {
+ /// `None` when the wait timed out. An abandoned mutex (crashed owner) counts as acquired.
+ fn from_wait(handle: OwnedHandle, status: WAIT_EVENT) -> Result