Skip to content

fix: 修复 macOS 退出时卡在关窗阶段无法结束进程 - #453

Open
Jeasin wants to merge 1 commit into
SPlayer-Dev:devfrom
Jeasin:fix/macos-quit
Open

Jeasin wants to merge 1 commit into
SPlayer-Dev:devfrom
Jeasin:fix/macos-quit

Conversation

@Jeasin

@Jeasin Jeasin commented Nov 7, 2026 •

Copy link
Copy Markdown

问题

#450:macOS 上用 ⌘Q 退出后,窗口消失了、Dock 图标还在,进程一直没有结束,要等十几秒才被收掉。

原因

退出流程是「before-quit 里 preventDefault() → 异步清理 → 再调一次 app.quit()」。

第二次 app.quit() 会去关闭所有窗口,而 Electron 在关窗阶段会把内部的退出标记(is_quitting_)复位。于是最后一个窗口关闭后,Browser::OnWindowAllClosed() 走的是 window-all-closed 分支(macOS 下该分支不做任何事),不会再进入 will-quit,进程就停在这里,只能靠 10 秒看门狗 app.exit(1) 兜底。

临时埋点抓到的日志(dev 分支,打包后运行):

[退出调试] 清理完成,调用 app.quit(),窗口:visible=true
[退出调试] before-quit finished=true started=true defaultPrevented=false
[退出调试] 主窗口 close quitting=true
[退出调试] app.quit() 调用已返回
[退出调试] window-all-closed platform=darwin          ← 走到这里就停了,没有 will-quit
[退出调试] 看门狗触发,卡在阶段:关闭窗口             ← 10 秒后

窗口正是在第二步被销毁的,所以此时点 Dock 图标会走 activate 重新建窗(#445 里那个 cache 协议报错就出现在这一步,该问题已由 a8716df 修复)。

修复

清理已经完成,在 window-all-closed 里补一次 app.quit(),退出流程就能继续走到 will-quit,正常结束。

关联 Issue

Closes #450

验证

  • 打包后实测同一路径退出:10 秒(看门狗强杀)→ 0.2 秒
  • 新增 electron/main/core/lifecycle.spec.ts:清理顺序、清理期间重复触发不重复清理、关掉最后一个窗口后补一次退出
  • vitest run 全量 585 个用例通过

退出流程在 before-quit 里拦一次、异步清理、再调一次 app.quit()。第二次退出
会关闭所有窗口,而 Electron 在关窗阶段会复位内部退出标记,最后一个窗口关闭后
只触发 window-all-closed(macOS 上该分支什么都不做),不再进入 will-quit,
进程要等 10 秒后的看门狗兜底,期间用户看到的是窗口消失、图标仍在。

清理此时已经完成,在 window-all-closed 里补一次退出调用即可正常收尾。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS 使用⌘Q关闭会报错

1 participant