Add targeted local port cleanup to desktop workbench

This commit is contained in:
tongtong committed 2026-11-12 15:46:15 +08:00
1 parent 51c4f86918
commit ab9cc8d0a5
8 files changed
+312 -1

No files matched your search

+29
View File
@@ -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<App>().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<Button>("ClearPortButton") != null, "Clear-port button is missing");
Console.WriteLine("PASS: GUI cleanup terminates the exact port owner and frees the port");
}
finally { if (!child.HasExited) { child.Kill(); child.WaitForExit(5000); } }
Console.WriteLine("PASS: Avalonia bindings and device action availability");
return;
}
@@ -118,6 +138,15 @@ internal static class Program
await vm.RefreshCommand.ExecuteAsync(null);
Require(vm.Devices.Single(d => d.Id == "home-phone").Busy == false, "Device lease was not released");
Console.WriteLine("PASS: GUI disconnect releases lease; remote gateway remains online");
vm.SelectedDevice = vm.Devices.Single(d => d.Id == "home-phone");
await vm.ConnectCommand.ExecuteAsync(null);
await UntilAsync(() => vm.Ready, "Device did not recover before own-port cleanup");
await vm.ClearPortCommand.ExecuteAsync(null);
Require(!vm.HasConnection && !vm.Ready && vm.PortCleanupStatus.Contains("已释放"), "Own connection was not cleared");
await vm.RefreshCommand.ExecuteAsync(null);
Require(!vm.Devices.Single(d => d.Id == "home-phone").Busy, "Own-port cleanup did not release lease");
Console.WriteLine("PASS: cleanup of the workbench's own port releases ADB and device lease");
vm.SaveCommand.Execute(null);
Require(!File.ReadAllText(Path.Combine(folder, "settings.json")).Contains(vm.Token), "Saved settings leaked the token");
Require(!vm.Logs.Any(line => line.Contains(vm.Token)), "UI log leaked token");