Skip to content

fix: fix daemon.lock race on mutagen startup #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions App/Services/MutagenController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ private async Task<SyncSessionControllerStateModel> UpdateState(MutagenClient cl
/// </summary>
private async Task<MutagenClient> EnsureDaemon(CancellationToken ct)
{
_logger.LogDebug("EnsureDaemon called");
ObjectDisposedException.ThrowIf(_disposing, typeof(MutagenController));
if (_mutagenClient != null && _daemonProcess != null)
return _mutagenClient;
Expand Down Expand Up @@ -479,12 +480,14 @@ private async Task<MutagenClient> EnsureDaemon(CancellationToken ct)
/// </summary>
private async Task<MutagenClient> StartDaemon(CancellationToken ct)
{
_logger.LogDebug("StartDaemon called");
// Stop the running daemon
if (_daemonProcess != null) await StopDaemon(ct);

// Attempt to stop any orphaned daemon
try
{
_logger.LogDebug("creating MutagenClient to stop orphan");
var client = new MutagenClient(_mutagenDataDirectory);
await client.Daemon.TerminateAsync(new DaemonTerminateRequest(), cancellationToken: ct);
}
Expand All @@ -496,6 +499,10 @@ private async Task<MutagenClient> StartDaemon(CancellationToken ct)
{
// Mainline; no daemon running.
}
finally
{
_logger.LogDebug("finished with orphan mutagen client");
}

// If we get some failure while creating the log file or starting the process, we'll retry
// it up to 5 times x 100ms. Those issues should resolve themselves quickly if they are
Expand Down Expand Up @@ -528,6 +535,7 @@ private async Task<MutagenClient> StartDaemon(CancellationToken ct)
ct.ThrowIfCancellationRequested();
try
{
_logger.LogDebug("creating mainline mutagen client");
var client = new MutagenClient(_mutagenDataDirectory);
_ = await client.Daemon.VersionAsync(new VersionRequest(), cancellationToken: ct);
_mutagenClient = client;
Expand Down
20 changes: 0 additions & 20 deletions MutagenSdk/MutagenClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@ public class MutagenClient : IDisposable

public MutagenClient(string dataDir)
{
// Check for the lock file first, since it should exist if it's running.
var daemonLockFile = Path.Combine(dataDir, "daemon", "daemon.lock");
if (!File.Exists(daemonLockFile))
throw new FileNotFoundException(
"Mutagen daemon lock file not found, did the mutagen daemon start successfully?", daemonLockFile);

// We should not be able to open the lock file.
try
{
using var _ = File.Open(daemonLockFile, FileMode.Open, FileAccess.Write, FileShare.None);
// We throw a FileNotFoundException if we could open the file because
// it means the same thing and allows us to return the path nicely.
throw new InvalidOperationException(
$"Mutagen daemon lock file '{daemonLockFile}' is unlocked, did the mutagen daemon start successfully?");
}
catch (IOException)
{
// this is what we expect
}

// Read the IPC named pipe address from the sock file.
var daemonSockFile = Path.Combine(dataDir, "daemon", "daemon.sock");
if (!File.Exists(daemonSockFile))
Expand Down
Loading