Windows 0.3.7 updater exit and Windows session cleanup reliability; no private history or other platform changes
This commit is contained in:
+71
-40
@@ -74,6 +74,8 @@ internal sealed partial class MainForm : Form
|
||||
private Task<Exception?>? _exitCleanupTask;
|
||||
private bool _exitResourcesDisposed;
|
||||
private bool _sessionEnding;
|
||||
private bool _installerExitRequested;
|
||||
private readonly ClientExitRecoveryFixture? _exitRecoveryFixture;
|
||||
private bool _windowExitSelfTestConnected;
|
||||
private bool _updateCheckRunning;
|
||||
private DateTimeOffset _nextUpdateCheckAt = DateTimeOffset.MinValue;
|
||||
@@ -85,8 +87,11 @@ internal sealed partial class MainForm : Form
|
||||
internal bool NetworkRecoveryFailed { get; private set; }
|
||||
|
||||
internal MainForm(bool runWindowExitSelfTest = false, bool preview = false, bool uiOnly = false,
|
||||
bool autoConnect = false, Func<CancellationToken, Task>? startupRecovery = null)
|
||||
bool autoConnect = false, Func<CancellationToken, Task>? startupRecovery = null,
|
||||
ClientExitRecoveryFixture? exitRecoveryFixture = null)
|
||||
{
|
||||
if (exitRecoveryFixture is not null && !uiOnly) throw new ArgumentException("Recovery fixtures require uiOnly isolation.");
|
||||
_exitRecoveryFixture = exitRecoveryFixture;
|
||||
_uiOnly = uiOnly;
|
||||
_preview = preview;
|
||||
_startupRecovery = startupRecovery;
|
||||
@@ -192,33 +197,34 @@ internal sealed partial class MainForm : Form
|
||||
return false;
|
||||
}
|
||||
var notes = string.IsNullOrWhiteSpace(update.Notes) ? "包含稳定性和网络恢复改进。" : update.Notes.Trim();
|
||||
var choice = MessageBox.Show(
|
||||
$"发现新版本 {update.Version}\n\n{notes}\n\n是否现在安全升级?\n升级将覆盖原安装目录,但会保留账号、设备标识和本机配置。",
|
||||
"纵横家·发现更新",
|
||||
update.Mandatory ? MessageBoxButtons.OKCancel : MessageBoxButtons.YesNo,
|
||||
MessageBoxIcon.Information);
|
||||
accepted = update.Mandatory ? choice == DialogResult.OK : choice == DialogResult.Yes;
|
||||
if (!accepted)
|
||||
{
|
||||
_nextUpdateCheckAt = DateTimeOffset.UtcNow.AddHours(6);
|
||||
return false;
|
||||
}
|
||||
|
||||
UseWaitCursor = true;
|
||||
Text = $"纵横家 · 正在下载 {update.Version}…";
|
||||
using var progressWindow = new UpdateProgressForm(update.Version);
|
||||
progressWindow.Show(this);
|
||||
Enabled = false;
|
||||
string installer;
|
||||
try { installer = await ClientUpdater.DownloadAsync(update, _lifetime.Token, new Progress<int>(progressWindow.Report)); }
|
||||
finally { Enabled = true; }
|
||||
// Start and authorize the installer BEFORE stopping the working client.
|
||||
// The installer stages and validates all files, then requests graceful shutdown.
|
||||
// Cancelling UAC or failing to start must leave the current connection alive.
|
||||
var installerProcess = ClientUpdater.LaunchInstaller(installer);
|
||||
_installerLaunched = true;
|
||||
_ = ObserveInstallerAsync(installerProcess);
|
||||
return true;
|
||||
return await ClientUpdateFlow.RunAsync(
|
||||
() => !_exiting && !_sessionEnding && !IsDisposed && !Disposing && !_lifetime.IsCancellationRequested,
|
||||
() => {
|
||||
var choice = MessageBox.Show(this,
|
||||
$"发现新版本 {update.Version}\n\n{notes}\n\n是否现在安全升级?\n升级将覆盖原安装目录,但会保留账号、设备标识和本机配置。",
|
||||
"纵横家·发现更新",
|
||||
update.Mandatory ? MessageBoxButtons.OKCancel : MessageBoxButtons.YesNo,
|
||||
MessageBoxIcon.Information);
|
||||
accepted = update.Mandatory ? choice == DialogResult.OK : choice == DialogResult.Yes;
|
||||
if (!accepted) _nextUpdateCheckAt = DateTimeOffset.UtcNow.AddHours(6);
|
||||
return accepted;
|
||||
}, async () => {
|
||||
UseWaitCursor = true;
|
||||
Text = $"纵横家 · 正在下载 {update.Version}…";
|
||||
using var progressWindow = new UpdateProgressForm(update.Version);
|
||||
progressWindow.Show(this);
|
||||
Enabled = false;
|
||||
try { return await ClientUpdater.DownloadAsync(update, _lifetime.Token, new Progress<int>(progressWindow.Report)); }
|
||||
finally { if (!_exiting && !IsDisposed && !Disposing) Enabled = true; }
|
||||
}, installer => {
|
||||
// Start and authorize the installer BEFORE stopping the working client.
|
||||
// The installer stages and validates all files, then requests graceful shutdown.
|
||||
// Cancelling UAC or failing to start must leave the current connection alive.
|
||||
var installerProcess = ClientUpdater.LaunchInstaller(installer);
|
||||
_installerLaunched = true;
|
||||
_ = ObserveInstallerAsync(installerProcess);
|
||||
AppPaths.WriteLog("update.install.started version=" + update.Version);
|
||||
});
|
||||
}
|
||||
catch (OperationCanceledException) when (_lifetime.IsCancellationRequested)
|
||||
{
|
||||
@@ -228,7 +234,7 @@ internal sealed partial class MainForm : Form
|
||||
{
|
||||
_nextUpdateCheckAt = DateTimeOffset.UtcNow.AddMinutes(accepted ? 30 : 2);
|
||||
AppPaths.WriteError(exception);
|
||||
if (accepted)
|
||||
if (accepted && !_exiting && !_sessionEnding && !IsDisposed && !Disposing)
|
||||
{
|
||||
MessageBox.Show(
|
||||
$"更新未启动,已保留当前版本和当前连接。\n\n{exception.Message}",
|
||||
@@ -241,8 +247,7 @@ internal sealed partial class MainForm : Form
|
||||
finally
|
||||
{
|
||||
_updateCheckRunning = false;
|
||||
UseWaitCursor = false;
|
||||
Text = "纵横家";
|
||||
if (!_exiting && !IsDisposed && !Disposing) { UseWaitCursor = false; Text = "纵横家"; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1249,7 +1254,7 @@ internal sealed partial class MainForm : Form
|
||||
eventArgs.Cancel = true;
|
||||
return;
|
||||
}
|
||||
if (_uiOnly || _preview)
|
||||
if ((_uiOnly || _preview) && _exitRecoveryFixture is null)
|
||||
{
|
||||
_ = BeginExitCleanup();
|
||||
CompleteExitResources();
|
||||
@@ -1269,13 +1274,21 @@ internal sealed partial class MainForm : Form
|
||||
SetConnectionState(false, "正在恢复本地网络", "正在恢复系统代理并关闭内核,请稍候…");
|
||||
}
|
||||
|
||||
var error = await cleanup;
|
||||
Exception? error;
|
||||
try { error = await cleanup.WaitAsync(_exitRecoveryFixture?.Deadline ?? TimeSpan.FromSeconds(85)); }
|
||||
catch (TimeoutException timeout)
|
||||
{
|
||||
// Leave every still-owned recovery journal intact. The installer
|
||||
// observes a nonzero client exit and must not replace its files.
|
||||
error = timeout;
|
||||
AppPaths.WriteLog("exit.recovery.timeout journals_retained=true");
|
||||
}
|
||||
if (_sessionEnding || IsDisposed || Disposing) return;
|
||||
if (error is not null)
|
||||
{
|
||||
// Never install over a client whose network recovery failed.
|
||||
NetworkRecoveryFailed = true;
|
||||
MessageBox.Show("网络恢复未全部完成,恢复记录已保留;本次不继续自动安装。\n" + error.GetBaseException().Message,
|
||||
if (!_installerExitRequested) MessageBox.Show("网络恢复未全部完成,恢复记录已保留;本次不继续自动安装。\n" + error.GetBaseException().Message,
|
||||
"纵横家 · 网络恢复提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
}
|
||||
CompleteExitResources();
|
||||
@@ -1293,13 +1306,19 @@ internal sealed partial class MainForm : Form
|
||||
_refreshTimer.Stop(); _updateTimer.Stop(); _trafficTimer.Stop(); _healthTimer.Stop();
|
||||
_nodeTrafficTimer.Stop();
|
||||
_lifetime.Cancel();
|
||||
if (_uiOnly || _preview) return _exitCleanupTask = Task.FromResult<Exception?>(null);
|
||||
if ((_uiOnly || _preview) && _exitRecoveryFixture is null) return _exitCleanupTask = Task.FromResult<Exception?>(null);
|
||||
// The same tracked cleanup serves menu/installer exit and session end.
|
||||
// Run it without a UI synchronization context so WM_ENDSESSION can
|
||||
// wait within Windows' shutdown budget without blocking its continuations.
|
||||
return _exitCleanupTask = Task.Run(async () =>
|
||||
{
|
||||
try { await _xray.DisposeAsync().ConfigureAwait(false); return (Exception?)null; }
|
||||
try
|
||||
{
|
||||
if (_exitRecoveryFixture is { } fixture) await fixture.Dispose().ConfigureAwait(false);
|
||||
else await _xray.DisposeAsync().ConfigureAwait(false);
|
||||
return (Exception?)null;
|
||||
}
|
||||
catch (OperationCanceledException error) when (_sessionEnding) { return error; }
|
||||
catch (Exception error) { AppPaths.WriteError(error); return error; }
|
||||
});
|
||||
}
|
||||
@@ -1320,14 +1339,17 @@ internal sealed partial class MainForm : Form
|
||||
{
|
||||
if (message.Msg == 0x11 /* WM_QUERYENDSESSION */)
|
||||
{
|
||||
// A query may be canceled by another application. Do not cancel
|
||||
// our session or await network cleanup until Windows confirms it.
|
||||
// A query can be canceled. Return promptly without disconnecting
|
||||
// or launching any recovery helper before Windows confirms it.
|
||||
message.Result = (IntPtr)1;
|
||||
return;
|
||||
}
|
||||
if (message.Msg == 0x16 /* WM_ENDSESSION */)
|
||||
{
|
||||
if (message.WParam == IntPtr.Zero) { message.Result = IntPtr.Zero; return; }
|
||||
if (message.WParam == IntPtr.Zero)
|
||||
{
|
||||
message.Result = IntPtr.Zero; return;
|
||||
}
|
||||
CompleteSessionEnding();
|
||||
}
|
||||
base.WndProc(ref message);
|
||||
@@ -1337,6 +1359,7 @@ internal sealed partial class MainForm : Form
|
||||
{
|
||||
if (_sessionEnding) return;
|
||||
_sessionEnding = true;
|
||||
_xray.ConfirmSessionShutdown();
|
||||
var cleanup = BeginExitCleanup();
|
||||
// Windows can terminate us after its short shutdown allowance. Never
|
||||
// remove recovery journals here or display a modal recovery error.
|
||||
@@ -1436,11 +1459,19 @@ internal sealed partial class MainForm : Form
|
||||
|
||||
internal void RequestExit()
|
||||
{
|
||||
if (_exiting || IsDisposed || Disposing) return;
|
||||
if (IsDisposed || Disposing) return;
|
||||
_exitRequested = true;
|
||||
if (_exiting) return;
|
||||
Close();
|
||||
}
|
||||
|
||||
internal void RequestExitForUpdate()
|
||||
{
|
||||
_installerExitRequested = true;
|
||||
AppPaths.WriteLog("update.shutdown.requested");
|
||||
RequestExit();
|
||||
}
|
||||
|
||||
private static void OpenRegistrationPage()
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user