Fix single-instance mutex race during app restart handoff - #50
Merged
Conversation
Settings "Save & Restart" starts the replacement iMirror process before the old process finishes disconnecting and shutting down. The new process could lose the single-instance mutex race and silently exit, leaving the user with no running iMirror. App.Main now waits up to 5 seconds for the previous holder to release the mutex (via SingleInstanceGate), treating an abandoned mutex as a successful acquire. Genuine duplicate launches still exit, just after a bounded wait. Adds 5 SingleInstanceGate tests covering unowned, held, released-within-timeout, and abandoned mutex cases. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Balragon
force-pushed
the
codex/fix-restart-mutex-race
branch
from
July 3, 2026 10:38
3dabd17 to
a509287
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
RestartApplicationAsync(settings "Save & Restart") starts the replacement process before the old process finishesShutdownApplicationAsync(disconnect, tray dispose,Application.Shutdown).App.Mainexits immediately when the single-instance mutex is already held, so the relaunched instance could silently quit while the old one was still shutting down — the restart button would just close the app.Fix
SingleInstanceGate.TryAcquireExistingMutex: boundedWaitOnethat treatsAbandonedMutexException(previous process died without releasing) as a successful acquire.App.Mainnow waits up to 5 seconds for the previous instance to release the mutex before giving up. Genuine duplicate launches still exit as before, after the bounded wait. Both paths are logged.No change to update/installer handoff behavior: the Inno Setup
/CLOSEAPPLICATIONSrelaunch path already waits for the old process to exit, and it also benefits from the extra tolerance.Testing
5 new
SingleInstanceGateTestscover: unowned mutex, mutex held past the timeout (returns false), holder releasing within the timeout, abandoned mutex, and null argument.Validation
dotnet build iMirror.sln -c Release— passed, 0 warnings, 0 errors.dotnet test MacMirrorReceiver.Tests\MacMirrorReceiver.Tests.csproj -c Release— passed: 72 tests, 0 failed.Independent of #49; can merge in any order (trivial CHANGELOG adjacency with other Unreleased entries).
🤖 Generated with Claude Code