fix(macos): 模型窗口改由主线程承载,修复启动模型失败与后端崩溃 #29

Merged
Morris merged 1 commits from fix/model-launch-main-thread into main 2026-11-19 20:25:33 +08:00
Owner

问题

macOS 发布版点击「启动模型」后模型窗口不出现(或直接失败),且后端进程很容易崩溃——界面随即失去连接、提示无法连接后端。

根因

macOS 的 AppKit 要求 NSApplication 运行在主线程。此前 AvaloniaHost 把 StartWithClassicDesktopLifetime 丢到一个新建的后台线程里跑:

var thread = new Thread(() => {
    AppBuilder.Configure<HostApp>()...StartWithClassicDesktopLifetime(Array.Empty<string>());
});

而 Flutter 模式下 GC-BOOT 的主线程被 FlutterShell 的 ui.WaitForExit() 占住。模型窗口一创建就在副线程触碰 AppKit,触发进程级崩溃,后端随之退出。

纯 Avalonia 版(主程序自己就是 UI 线程)不会暴露这个问题,只在 Flutter 打包模式下出现。

修改

  1. AvaloniaHost:新增 RunOnCurrentThread() / Shutdown();EnsureStartedAsync() 改为只等待就绪信号,不再自建线程。
  2. BackendServer.Start:新增 out AvaloniaHost 重载,把模型窗口宿主交给启动器。
  3. FlutterShell:前端拉起后,在主线程运行 Avalonia 事件循环;界面退出时从后台 Shutdown();无桌面会话(Avalonia 起不来)时退化为继续等待界面退出。
  4. Program.cs(独立进程入口):后端放后台,主线程留给 Avalonia。

验证

  • 启动 GC.app:/api/health 返回 uiAvailable: true;前端连接 127.0.0.1:xxxxx -> 127.0.0.1:17890 (ESTABLISHED)。
  • POST /api/models/contour-map/launch → HTTP 200 {"ok":true,"message":"已启动模型 contour-map"},模型窗口正常弹出。
  • 启动模型后再查 /api/health 仍正常,后端不再崩溃。
## 问题 macOS 发布版点击「启动模型」后模型窗口不出现(或直接失败),且后端进程很容易崩溃——界面随即失去连接、提示无法连接后端。 ## 根因 macOS 的 AppKit 要求 `NSApplication` 运行在**主线程**。此前 `AvaloniaHost` 把 `StartWithClassicDesktopLifetime` 丢到一个新建的后台线程里跑: ```csharp var thread = new Thread(() => { AppBuilder.Configure<HostApp>()...StartWithClassicDesktopLifetime(Array.Empty<string>()); }); ``` 而 Flutter 模式下 `GC-BOOT` 的主线程被 `FlutterShell` 的 `ui.WaitForExit()` 占住。模型窗口一创建就在副线程触碰 AppKit,触发进程级崩溃,后端随之退出。 纯 Avalonia 版(主程序自己就是 UI 线程)不会暴露这个问题,只在 Flutter 打包模式下出现。 ## 修改 1. `AvaloniaHost`:新增 `RunOnCurrentThread()` / `Shutdown()`;`EnsureStartedAsync()` 改为只等待就绪信号,不再自建线程。 2. `BackendServer.Start`:新增 `out AvaloniaHost` 重载,把模型窗口宿主交给启动器。 3. `FlutterShell`:前端拉起后,在**主线程**运行 Avalonia 事件循环;界面退出时从后台 `Shutdown()`;无桌面会话(Avalonia 起不来)时退化为继续等待界面退出。 4. `Program.cs`(独立进程入口):后端放后台,主线程留给 Avalonia。 ## 验证 - 启动 GC.app:`/api/health` 返回 `uiAvailable: true`;前端连接 `127.0.0.1:xxxxx -> 127.0.0.1:17890 (ESTABLISHED)`。 - `POST /api/models/contour-map/launch` → `HTTP 200 {"ok":true,"message":"已启动模型 contour-map"}`,模型窗口正常弹出。 - 启动模型后再查 `/api/health` 仍正常,**后端不再崩溃**。
Morris added 1 commit 2026-11-19 20:25:15 +08:00
fix(macos): 模型窗口改由主线程承载,修复启动模型失败与后端崩溃
PR 审核 / 后端编译 (pull_request) Canceled after 0s
PR 审核 / 后端自检 (pull_request) Canceled after 0s
PR 审核 / 前端检查(Flutter) (pull_request) Canceled after 0s
PR 审核 / 安全扫描 (pull_request) Canceled after 0s
PR 审核 / 审核汇总 (pull_request) Canceled after 0s
ead63e8ffe
macOS 的 AppKit 要求 NSApplication 运行在主线程。此前 AvaloniaHost 把
StartWithClassicDesktopLifetime 放到新建的后台线程,点「启动模型」即触发
进程崩溃:界面无反应、后端随之消失,部分机器表现为模型启动失败。

- AvaloniaHost 新增 RunOnCurrentThread / Shutdown:事件循环必须在主线程运行,
  EnsureStartedAsync 改为只等待就绪信号,不再自行起线程。
- BackendServer.Start 增加 out AvaloniaHost 重载,把模型窗口宿主交给启动器。
- FlutterShell 在前端拉起后于主线程运行 Avalonia 事件循环,界面退出时从后台
  请求关闭;无桌面会话(Avalonia 起不来)时退化为继续等待界面退出。
- 独立进程入口 Program.cs 同样把后端放后台,主线程留给 Avalonia。
Morris merged commit faaee9abe2 into main 2026-11-19 20:25:33 +08:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Morris/GC#29