Windows 0.3.7 updater exit and Windows session cleanup reliability; no private history or other platform changes

This commit is contained in:
2026-11-07 04:05:04 +00:00
parent ac40dd3a6c
commit 1590443809
15 changed files with 608 additions and 80 deletions
+25 -9
View File
@@ -22,6 +22,8 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
private bool _disposed;
private NetworkOwnerLease? _ownerLease;
private bool _recoveryComplete;
private bool _stoppedCleanly;
private readonly CancellationTokenSource _systemShutdown = new();
private readonly SemaphoreSlim _lifecycle = new(1, 1);
private readonly string _xrayDirectory = Path.Combine(AppContext.BaseDirectory, "xray");
private string? _baseConfigJson;
@@ -66,6 +68,8 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
public bool CanRebindNetwork => !string.IsNullOrWhiteSpace(_baseConfigJson) && _boundNetwork is not null;
bool IHandoverRuntime.IsRunning => IsRunning;
internal void ConfirmSessionShutdown() => _systemShutdown.Cancel();
public async Task SuspendForNetworkChangeAsync(CancellationToken token)
{
await _lifecycle.WaitAsync(token);
@@ -288,6 +292,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
{
ValidateRuntime();
if (string.IsNullOrWhiteSpace(_baseConfigJson)) throw new InvalidOperationException("线路配置为空。");
_stoppedCleanly = false;
PrivateAuxiliaryRuntime.ValidateLifetime(_baseConfigJson);
var privateAuxiliary = PrivateAuxiliaryRuntime.IsPrivate(_baseConfigJson);
var preservePrivateRoute = privateAuxiliary && PrivateAuxiliaryRuntime.Address(_baseConfigJson) != PrivateAuxiliaryRouting.PublicPhoneHost;
@@ -435,16 +440,18 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
{
// A rejected second instance must not delete the active owner's DNS,
// routes, PID journal or virtual adapter through its error/Dispose path.
if (_ownerLease is null || !_recoveryComplete) return;
if (_ownerLease is null || !_recoveryComplete || _stoppedCleanly) return;
_learningEpoch.Cancel();
var proxyRestored = SystemProxy.Restore();
Exception? restoreError = null;
using var recovery = new CancellationTokenSource(TimeSpan.FromSeconds(25));
var sessionToken = _systemShutdown.Token;
using var recovery = CancellationTokenSource.CreateLinkedTokenSource(sessionToken);
recovery.CancelAfter(TimeSpan.FromSeconds(25));
Exception? dnsRestoreError = null;
try { await ProxyDnsPolicy.RestoreAsync(recovery.Token).ConfigureAwait(false); }
catch (Exception error) { dnsRestoreError = error; AppPaths.WriteError(error); }
try { await TunRouting.RestoreAsync(recovery.Token).ConfigureAwait(false); }
catch (Exception error) { restoreError = error; AppPaths.WriteError(error); }
try { recovery.Token.ThrowIfCancellationRequested(); await ProxyDnsPolicy.RestoreAsync(recovery.Token).ConfigureAwait(false); }
catch (Exception error) { dnsRestoreError = error; if (!sessionToken.IsCancellationRequested) AppPaths.WriteError(error); }
try { recovery.Token.ThrowIfCancellationRequested(); await TunRouting.RestoreAsync(recovery.Token).ConfigureAwait(false); }
catch (Exception error) { restoreError = error; if (!sessionToken.IsCancellationRequested) AppPaths.WriteError(error); }
try
{
if (_process is { HasExited: false })
@@ -464,21 +471,30 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
SafeDelete(AppPaths.PidFile);
SafeDeleteConfig();
}
await TunRouting.CleanupOwnedAdapterAsync(CancellationToken.None).ConfigureAwait(false);
// Confirmed system shutdown forbids launching fresh helper processes.
// Kill the already-owned core above, retain failed journals, and let
// startup recovery finish after Windows is ready again.
sessionToken.ThrowIfCancellationRequested();
await TunRouting.CleanupOwnedAdapterAsync(sessionToken).ConfigureAwait(false);
if (restoreError is not null)
{
// Removal of a failed TUN device may have removed its routes too.
// Retry the retained journal; never erase it merely because exit was requested.
using var retry = new CancellationTokenSource(TimeSpan.FromSeconds(15));
using var retry = CancellationTokenSource.CreateLinkedTokenSource(sessionToken);
retry.CancelAfter(TimeSpan.FromSeconds(15));
retry.Token.ThrowIfCancellationRequested();
await TunRouting.RestoreAsync(retry.Token).ConfigureAwait(false);
}
if (!proxyRestored && !SystemProxy.Restore())
throw new IOException("系统代理恢复未完成,恢复记录已保留。请勿继续升级安装。");
if (dnsRestoreError is not null)
{
using var retry = new CancellationTokenSource(TimeSpan.FromSeconds(25));
using var retry = CancellationTokenSource.CreateLinkedTokenSource(sessionToken);
retry.CancelAfter(TimeSpan.FromSeconds(25));
retry.Token.ThrowIfCancellationRequested();
await ProxyDnsPolicy.RestoreAsync(retry.Token).ConfigureAwait(false);
}
_stoppedCleanly = true;
}
private async Task HandleCoreExitAsync(Process exited)