Skip to content

Commit 62ee76c

Browse files
committed
fix: fix mutagen controller to release process on exception
1 parent 41cf916 commit 62ee76c

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

App/Services/MutagenController.cs

+19-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
using Coder.Desktop.Vpn.Utilities;
1717
using Grpc.Core;
1818
using Microsoft.Extensions.Options;
19+
using Microsoft.Extensions.Logging;
20+
using Serilog;
1921
using DaemonTerminateRequest = Coder.Desktop.MutagenSdk.Proto.Service.Daemon.TerminateRequest;
2022
using MutagenProtocol = Coder.Desktop.MutagenSdk.Proto.Url.Protocol;
2123
using SynchronizationTerminateRequest = Coder.Desktop.MutagenSdk.Proto.Service.Synchronization.TerminateRequest;
24+
using Microsoft.Extensions.Hosting;
2225

2326
namespace Coder.Desktop.App.Services;
2427

@@ -110,6 +113,8 @@ public sealed class MutagenController : ISyncSessionController
110113
// Protects all private non-readonly class members.
111114
private readonly RaiiSemaphoreSlim _lock = new(1, 1);
112115

116+
private readonly ILogger<MutagenController> _logger;
117+
113118
private readonly CancellationTokenSource _stateUpdateCts = new();
114119
private Task? _stateUpdateTask;
115120

@@ -139,15 +144,19 @@ public sealed class MutagenController : ISyncSessionController
139144

140145
private string MutagenDaemonLog => Path.Combine(_mutagenDataDirectory, "daemon.log");
141146

142-
public MutagenController(IOptions<MutagenControllerConfig> config)
147+
public MutagenController(IOptions<MutagenControllerConfig> config, ILogger<MutagenController> logger)
143148
{
144149
_mutagenExecutablePath = config.Value.MutagenExecutablePath;
150+
_logger = logger;
145151
}
146152

147153
public MutagenController(string executablePath, string dataDirectory)
148154
{
149155
_mutagenExecutablePath = executablePath;
150156
_mutagenDataDirectory = dataDirectory;
157+
var builder = Host.CreateApplicationBuilder();
158+
builder.Services.AddSerilog();
159+
_logger = (ILogger<MutagenController>)builder.Build().Services.GetService(typeof(ILogger<MutagenController>))!;
151160
}
152161

153162
public event EventHandler<SyncSessionControllerStateModel>? StateChanged;
@@ -437,9 +446,9 @@ private async Task<MutagenClient> EnsureDaemon(CancellationToken ct)
437446
{
438447
await StopDaemon(cts.Token);
439448
}
440-
catch
449+
catch (Exception stopEx)
441450
{
442-
// ignored
451+
_logger.LogError(stopEx, "failed to stop daemon");
443452
}
444453

445454
ReplaceState(new SyncSessionControllerStateModel
@@ -491,6 +500,8 @@ private async Task<MutagenClient> StartDaemon(CancellationToken ct)
491500
}
492501
catch (Exception e) when (e is not OperationCanceledException)
493502
{
503+
_logger.LogWarning(e, "failed to start daemon process, attempt {attempt} of {maxAttempts}", attempts,
504+
maxAttempts);
494505
if (attempts == maxAttempts)
495506
throw;
496507
// back off a little and try again.
@@ -545,8 +556,11 @@ private void StartDaemonProcess()
545556
// https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.environment?view=net-8.0
546557
_daemonProcess.StartInfo.UseShellExecute = false;
547558
_daemonProcess.StartInfo.RedirectStandardError = true;
548-
// TODO: log exited process
549-
// _daemonProcess.Exited += ...
559+
_daemonProcess.EnableRaisingEvents = true;
560+
_daemonProcess.Exited += (object? sender, EventArgs e) =>
561+
{
562+
_logger.LogInformation("mutagen daemon exited with code {exitCode}", _daemonProcess?.ExitCode);
563+
};
550564
if (!_daemonProcess.Start())
551565
throw new InvalidOperationException("Failed to start mutagen daemon process, Start returned false");
552566

0 commit comments

Comments
 (0)