From ab9cc8d0a55ee173e424b85b8185092eb5be3739 Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Thu, 12 Nov 2026 15:46:15 +0800 Subject: [PATCH] Add targeted local port cleanup to desktop workbench --- desktop/Ads.Desktop.Smoke/Program.cs | 29 ++++ .../Ads.Desktop.Tests.csproj | 1 + desktop/Ads.Desktop.Tests/PortCleanupTests.cs | 86 ++++++++++ desktop/Ads.Desktop/LocalPortProcesses.cs | 152 ++++++++++++++++++ desktop/Ads.Desktop/MainViewModel.cs | 34 ++++ desktop/Ads.Desktop/MainWindow.axaml | 3 +- desktop/README.md | 1 + docs/verification.md | 7 + 8 files changed, 312 insertions(+), 1 deletion(-) create mode 100644 desktop/Ads.Desktop.Tests/PortCleanupTests.cs create mode 100644 desktop/Ads.Desktop/LocalPortProcesses.cs diff --git a/desktop/Ads.Desktop.Smoke/Program.cs b/desktop/Ads.Desktop.Smoke/Program.cs index 343ed7d..7ac4df0 100644 --- a/desktop/Ads.Desktop.Smoke/Program.cs +++ b/desktop/Ads.Desktop.Smoke/Program.cs @@ -9,6 +9,15 @@ internal static class Program [STAThread] public static int Main(string[] args) { + if (args.Length == 2 && args[0] == "--port-child") + { + var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, int.Parse(args[1])); + listener.Start(); + Console.WriteLine("READY"); + Console.Out.Flush(); + Thread.Sleep(Timeout.Infinite); + return 0; + } AppBuilder.Configure().UseSkia().UseHeadless(new AvaloniaHeadlessPlatformOptions { UseHeadlessDrawing = false }).SetupWithoutStarting(); using var finished = new CancellationTokenSource(); Exception? failure = null; @@ -72,6 +81,17 @@ internal static class Program Require(!vm.HasConnection && vm.Notice.Contains("已被占用"), "Occupied port was not rejected safely"); } finally { occupied.Stop(); } + var childInfo = ProcessTools.CreateStartInfo("dotnet", [typeof(Program).Assembly.Location, "--port-child", vm.Port.ToString()]); + using var child = System.Diagnostics.Process.Start(childInfo)!; + try + { + Require(await child.StandardOutput.ReadLineAsync().WaitAsync(TimeSpan.FromSeconds(10)) == "READY", "Listener child did not start"); + await vm.ClearPortCommand.ExecuteAsync(null); + Require(child.HasExited && vm.PortCleanupStatus.Contains("已释放"), "GUI clear-port action did not terminate listener"); + Require(window.FindControl