Skip to content

Commit 818d2e0

Browse files
fix(single-instance): limit second instance's wait on Windows (#3645)
* Update windows.rs * Create single-instance-forward-timeout.md * use SMTO_NORMAL * nit --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 323fe80 commit 818d2e0

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"single-instance": patch
3+
---
4+
5+
On Windows, a second instance no longer waits indefinitely for a first instance that stopped processing messages: it stops waiting after 10 seconds and exits. The first instance may still receive the arguments once it resumes processing messages.

‎plugins/single-instance/src/platform_impl/windows.rs‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ use windows_sys::Win32::{
2121
UI::WindowsAndMessaging::{
2222
self as w32wm, AllowSetForegroundWindow, CREATESTRUCTW, CreateWindowExW, DefWindowProcW,
2323
DestroyWindow, FindWindowW, GWL_STYLE, GWLP_USERDATA, GetWindowThreadProcessId,
24-
RegisterClassExW, SendMessageW, WINDOW_LONG_PTR_INDEX, WM_COPYDATA, WM_CREATE, WM_DESTROY,
25-
WNDCLASSEXW, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_EX_TRANSPARENT,
26-
WS_OVERLAPPED, WS_POPUP, WS_VISIBLE,
24+
RegisterClassExW, SMTO_NORMAL, SendMessageTimeoutW, WINDOW_LONG_PTR_INDEX, WM_COPYDATA,
25+
WM_CREATE, WM_DESTROY, WNDCLASSEXW, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW,
26+
WS_EX_TRANSPARENT, WS_OVERLAPPED, WS_POPUP, WS_VISIBLE,
2727
},
2828
};
2929

3030
const WMCOPYDATA_SINGLE_INSTANCE_DATA: usize = 1542;
3131

32+
/// How long the second instance waits for the first instance to take its arguments.
33+
const FORWARD_TIMEOUT_MS: u32 = 10_000;
34+
3235
struct MutexHandle(isize);
3336

3437
struct TargetWindowHandle(isize);
@@ -102,7 +105,21 @@ pub fn init<R: Runtime>(callback: Box<SingleInstanceCallback<R>>) -> TauriPlugin
102105
lpData: bytes.as_ptr() as _,
103106
};
104107

105-
SendMessageW(hwnd, WM_COPYDATA, 0, &cds as *const _ as _);
108+
let sent = SendMessageTimeoutW(
109+
hwnd,
110+
WM_COPYDATA,
111+
0,
112+
&cds as *const _ as _,
113+
SMTO_NORMAL,
114+
FORWARD_TIMEOUT_MS,
115+
std::ptr::null_mut(),
116+
);
117+
if sent == 0 {
118+
tracing::warn!(
119+
"single_instance failed to forward arguments to the running instance within {FORWARD_TIMEOUT_MS}ms (error {})",
120+
GetLastError()
121+
);
122+
}
106123

107124
app.cleanup_before_exit();
108125
std::process::exit(0);

0 commit comments

Comments
 (0)