Fixed the .NET server shutting down while the runtime still runs (#1237)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-26 12:01:08 +01:00
1 parent 9ebfce3855
commit 902e406e1d
22 files changed
+923 -115

No files matched your search

@@ -17,7 +17,18 @@ public sealed partial class RustService : BackgroundService
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(RustService).Namespace, nameof(RustService));
private static string TA(string fallbackEN) => I18N.I.TA(fallbackEN, typeof(RustService).Namespace, nameof(RustService));
/// <summary>
/// How long the runtime may take to answer whether it is alive. Its handler does no work, so
/// anything longer means that the runtime cannot answer at the moment.
/// </summary>
private static readonly TimeSpan ALIVE_CHECK_TIMEOUT = TimeSpan.FromSeconds(5);
/// <summary>
/// How long the runtime may take to log one batch of log events.
/// </summary>
private static readonly TimeSpan LOG_TIMEOUT = TimeSpan.FromSeconds(30);
private readonly HttpClient http;
/// <summary>
@@ -32,6 +43,23 @@ public sealed partial class RustService : BackgroundService
/// </remarks>
private readonly HttpClient extractionHttp;
/// <summary>
/// A dedicated client for checking whether the runtime is alive.
/// </summary>
/// <remarks>
/// All other calls share one HTTP/2 connection. A flood of requests can congest it, and the
/// runtime may close it, while the runtime itself keeps running. This client opens a connection
/// of its own, so its check fails only when the runtime cannot accept a connection and answer.
/// </remarks>
private readonly HttpClient aliveHttp;
/// <summary>
/// A dedicated client for the log events, so a burst of them never waits in front of other calls.
/// </summary>
private readonly HttpClient logHttp;
private readonly RustLogForwarder logForwarder;
private readonly SemaphoreSlim fileDialogLock = new(1, 1);
private readonly SemaphoreSlim userLanguageLock = new(1, 1);
private readonly SemaphoreSlim userNameLock = new(1, 1);
@@ -88,6 +116,9 @@ public sealed partial class RustService : BackgroundService
// extraction client below is visible:
this.http = CreateHttpClient(apiPort, certificateFingerprint, TimeSpan.FromSeconds(100));
this.extractionHttp = CreateHttpClient(apiPort, certificateFingerprint, Timeout.InfiniteTimeSpan);
this.aliveHttp = CreateHttpClient(apiPort, certificateFingerprint, ALIVE_CHECK_TIMEOUT);
this.logHttp = CreateHttpClient(apiPort, certificateFingerprint, LOG_TIMEOUT);
this.logForwarder = new(this.logHttp, RustLogForwarder.PAUSE_AFTER_FAILURE);
}
/// <summary>
@@ -104,6 +135,9 @@ public sealed partial class RustService : BackgroundService
this.certificateFingerprint = string.Empty;
this.http = new HttpClient(runtime, disposeHandler: false) { BaseAddress = new Uri("https://127.0.0.1"), Timeout = TimeSpan.FromSeconds(100) };
this.extractionHttp = new HttpClient(runtime, disposeHandler: false) { BaseAddress = new Uri("https://127.0.0.1"), Timeout = Timeout.InfiniteTimeSpan };
this.aliveHttp = new HttpClient(runtime, disposeHandler: false) { BaseAddress = new Uri("https://127.0.0.1"), Timeout = ALIVE_CHECK_TIMEOUT };
this.logHttp = new HttpClient(runtime, disposeHandler: false) { BaseAddress = new Uri("https://127.0.0.1"), Timeout = LOG_TIMEOUT };
this.logForwarder = new(this.logHttp, RustLogForwarder.PAUSE_AFTER_FAILURE);
}
private static HttpClient CreateHttpClient(string apiPort, string certificateFingerprint, TimeSpan timeout)
@@ -160,8 +194,13 @@ public sealed partial class RustService : BackgroundService
public override void Dispose()
{
// First, so the waiting log events still reach the runtime:
this.logForwarder.Dispose();
this.logHttp.Dispose();
this.http.Dispose();
this.extractionHttp.Dispose();
this.aliveHttp.Dispose();
this.userLanguageLock.Dispose();
this.userNameLock.Dispose();
base.Dispose();