Skip to content

Commit 5b08cca

Browse files
committed
fix: fix mutagen controller to release process on exception
1 parent b803aa1 commit 5b08cca

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

App/Services/MutagenController.cs

Lines changed: 19 additions & 5 deletions
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;
@@ -440,9 +449,9 @@ private async Task<MutagenClient> EnsureDaemon(CancellationToken ct)
440449
{
441450
await StopDaemon(cts.Token);
442451
}
443-
catch
452+
catch (Exception stopEx)
444453
{
445-
// ignored
454+
_logger.LogError(stopEx, "failed to stop daemon");
446455
}
447456

448457
ReplaceState(new SyncSessionControllerStateModel
@@ -494,6 +503,8 @@ private async Task<MutagenClient> StartDaemon(CancellationToken ct)
494503
}
495504
catch (Exception e) when (e is not OperationCanceledException)
496505
{
506+
_logger.LogWarning(e, "failed to start daemon process, attempt {attempt} of {maxAttempts}", attempts,
507+
maxAttempts);
497508
if (attempts == maxAttempts)
498509
throw;
499510
// back off a little and try again.
@@ -548,8 +559,11 @@ private void StartDaemonProcess()
548559
// https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.environment?view=net-8.0
549560
_daemonProcess.StartInfo.UseShellExecute = false;
550561
_daemonProcess.StartInfo.RedirectStandardError = true;
551-
// TODO: log exited process
552-
// _daemonProcess.Exited += ...
562+
_daemonProcess.EnableRaisingEvents = true;
563+
_daemonProcess.Exited += (object? sender, EventArgs e) =>
564+
{
565+
_logger.LogInformation("mutagen daemon exited with code {exitCode}", _daemonProcess?.ExitCode);
566+
};
553567
if (!_daemonProcess.Start())
554568
throw new InvalidOperationException("Failed to start mutagen daemon process, Start returned false");
555569

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy