Skip to content

fix(single-instance): handle Windows startup race and ensure secondary instance exits - #3704

Merged
lucasfernog merged 1 commit into
tauri-apps:v2from
kxrn-gupta:fix/single-instance-windows-startup-race
Oct 15, 2026
Merged

lucasfernog merged 1 commit into
tauri-apps:v2from
kxrn-gupta:fix/single-instance-windows-startup-race

Conversation

@kxrn-gupta

@kxrn-gupta kxrn-gupta commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3587
Closes #3495

Description

On Windows, when two instances are launched in rapid succession (e.g. fast shortcut double-clicks, concurrent autostarts), the primary instance publishes its named mutex (CreateMutexW) before creating its event target window (create_event_target_window).

In the secondary instance:

  1. CreateMutexW detects ERROR_ALREADY_EXISTS.
  2. FindWindowW is called immediately and returns null because the primary instance's event target window has not yet been registered.
  3. Previously, the exit path was nested inside if !hwnd.is_null() with no else branch. Consequently, when FindWindowW missed the startup window, the secondary instance fell through and booted completely, violating the single-instance contract and causing severe concurrency issues (such as SQLite database is locked errors).

Solution

  1. Bounded Polling for Startup Window: Added a bounded retry loop (up to 1,000ms in 25ms intervals) on FindWindowW so that secondary instances launched within the startup window can reliably discover the primary window and forward their CLI arguments / deep-links.
  2. Deterministic Secondary Exit: Regardless of whether the target window was resolved or timed out, if ERROR_ALREADY_EXISTS is observed, the secondary instance executes app.cleanup_before_exit() and terminates with std::process::exit(0).

@FabianLars

Copy link
Copy Markdown
Member

Requesting Tony's review since he also reviewed #3495

@Legend-Master Legend-Master left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should close #3495, let's add it to the PR description.

I don't really like how both PRs are done through agents (without putting in effort even editing the PR descriptions and comment responses) and I honestly don't want to accept either of them but well, a new era here we are 🤷‍♂️

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Package Changes Through 0b039d2

There are 1 changes which include single-instance with patch

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
single-instance 2.5.4 2.5.5

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@kxrn-gupta

Copy link
Copy Markdown
Contributor Author

Fair point on the verbose description formatting Tony, got a bit lazy drafting the writeup! 😅 Cleaned up the polling loop into a single pass and dropped the redundant CloseHandle as requested. Also added Closes #3495 to the description. Appreciate the review!

@Legend-Master Legend-Master left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Also you'll need to sign your commits for me to merge this

https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits

@kxrn-gupta
kxrn-gupta force-pushed the fix/single-instance-windows-startup-race branch from 262a38c to 0b039d2 Compare October 15, 2026 10:18
@kxrn-gupta

Copy link
Copy Markdown
Contributor Author

Done! Rebased onto latest v2 and pushed with verified signed commits. Ready for merge! @Legend-Master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

single-instance (Windows): second instance survives when FindWindowW misses the startup window

4 participants