Repository navigation
Description
Product or interface
Desktop app
Version
3.1.19.232
Platform
Windows
OS version and architecture
Windows 11 x64 (build 26300, UBR 9818)
Issue area
UI / UX
Desktop log upload ID (optional)
No response
Steps to reproduce
背景
应用在 18 处用 BrowserWindow.getAllWindows() 枚举自己的顶层窗口。该 Electron API 只返回 BrowserWindow 实例。而 Mini App 弹出窗口在页面运行于原生 web-contents-view 后端时是一个 BaseWindow,因此从不出现在这个列表里。
关键前提:原生后端是默认配置,不是边缘情况
dist/main/modules/browser/browser-backend-policy.js:
const NATIVE_BACKEND = 'web-contents-view'; target(document) {
if (!document.rollout)
return { backend: NATIVE_BACKEND, notTargeted: false };
...
return matched
? { backend: 'webview', notTargeted: false }
: { backend: NATIVE_BACKEND, notTargeted: true };
} select() {
// Every new page in this process uses the launch backend; promotions and
// rollbacks alike wait for the next launch.
const backend = this.launch?.backend ?? NATIVE_BACKEND;只有命中 webview 灰度桶的账号走 BrowserWindow 弹出窗口;没有灰度文档、不在名单、或尚未解析完成的账号全部走 BaseWindow。也就是说,被枚举漏掉的恰恰是大多数用户那条路径。
复现步骤
- 以不在
webview灰度名单内的账号登录桌面端(后端为原生web-contents-view;可在客户端日志archon-browser-input-*.log中确认"renderer_backend":"web-contents-view")。 - 打开任意 Mini App,把它弹出成独立窗口。
- 关闭主窗口,只留下弹出窗口;再从 Dock / 任务栏激活应用。
- 实际:会额外新开一个主窗口,而不是把屏幕上已有的弹出窗口带到前台。
- 在弹出窗口内触发对话框(例如 Mini App 的文件选择)。
- 实际:对话框没有正确的父窗口位置。
同样可观察到:弹出窗口不跟随 native theme 的背景色;弹出窗口内没有应用自定义右键菜单;页面睡眠(MINIAPP_SLEPT)等运行时通知送不到该窗口。
Expected and actual behavior
期望
应用的「所有顶层窗口」视图应覆盖 Mini App 弹出窗口,无论它由 BrowserWindow 还是 BaseWindow 承载。退出清理、激活判空、对话框 owner 定位、运行时广播都应覆盖它。
实际
BrowserWindow.getAllWindows() 漏掉原生后端的弹出窗口,带来四个具体后果:
1) 退出清理漏掉它
dist/main/lifecycle/application-bootstrap-production.js:297-307
destroyAll: () => {
for (const window of electron_1.BrowserWindow.getAllWindows()) {
try {
if (!window.isDestroyed())
window.destroy();
}
catch (error) {
logLifecycleError('Failed to destroy a window during update shutdown', error);
}
}
},2) 激活判空用错枚举,会开出重复主窗口
dist/main/lifecycle/application-bootstrap-production.js:384-392
const registerActivateHandler = (0, application_bootstrap_1.createActivateHandlerRegistrar)(electron_1.app, () => {
if (electron_1.BrowserWindow.getAllWindows().length === 0) {
void windows_1.windowManager.createInitialWindow().then(() => (0, dock_1.flushDockFileQueue)());
}
else {
(0, utils_1.bringToFront)();
(0, dock_1.flushDockFileQueue)();
}
});屏幕上只剩一个原生后端弹出窗口时,这个判断为 0,于是新开一个主窗口。而 bringToFront()(dist/main/utils/window.js:14-45)也只会抬主窗口,抬不动弹出窗口。
3) 对话框 owner 定位:分支写了,但永远走不到
dist/main/utils/dialog.js:53-66
const allWindows = electron_1.BrowserWindow.getAllWindows();
for (const win of allWindows) {
// 检查主窗口的 webContents
if (win.webContents.id === event.sender.id) {
return win;
}
// 检查子视图
const views = win.contentView?.children || [];
for (const view of views) {
if ('webContents' in view && view.webContents?.id === event.sender.id) {
return win;
}
}
}contentView.children 这一段正是为「窗口内挂了 WebContentsView」写的,而原生后端的 Mini App 页面正是这样挂上去的(dist/main/modules/browser/native-browser-view.js:10-21):
class NativeBrowserView {
constructor(preferences) {
this.view = new electron_1.WebContentsView({ webPreferences: preferences });
}
attachInitial(parent) {
parent.contentView.addChildView(this.view);
}但它遍历的枚举永远不可能包含那个 BaseWindow,所以这段代码对该场景是死代码。
4) 运行时广播与右键菜单漏掉它
dist/main/modules/local-runtime/index.js:1316-1332、:1374-1382
function broadcastEmbeddedBrowserAgentAction(event) {
for (const window of electron_1.BrowserWindow.getAllWindows()) {
if (window.isDestroyed?.() || window.webContents.isDestroyed?.())
continue;
window.webContents.send(constants_1.IPC_CHANNELS.EMBEDDED_BROWSER_AGENT_ACTION, event);
}
}
...
function broadcastMiniAppSlept(event) {
for (const window of electron_1.BrowserWindow.getAllWindows()) {
...
window.webContents.send(constants_1.IPC_CHANNELS.MINIAPP_SLEPT, event);dist/main/ipc/contextMenu.ipc.js:205-211
electron_1.app.on('web-contents-created', (_, webContents) => {
setupContextMenuForWebContents(webContents);
});
// 为已存在的窗口设置右键菜单
electron_1.BrowserWindow.getAllWindows().forEach((window) => {
setupContextMenuForWebContents(window.webContents);
});全部 18 处调用点
ipc/contextMenu.ipc.js:209、ipc/devtools.ipc.js:22、ipc/settings.ipc.js:222、ipc/storage.ipc.js:315、lifecycle/application-bootstrap-production.js:298 与 :385、modules/local-runtime/computer-use/index.js:274、modules/local-runtime/utility/index.js:236、modules/local-runtime/index.js:1317 :1324 :1378、modules/remote-control-bridge/ipc.js:233 :247 :262、modules/updater/events.js:63、utils/dialog.js:53、windows/utils.js:53。
成因判定:这是设计遗漏,不是刻意排除
- 代码本身已经在尝试解决:
utils/dialog.js:60-64的contentView.children查找分支正是为「窗口拥有 WebContentsView」而写,但它遍历的枚举永远给不出那个BaseWindow。分支对目标场景不可达,说明意图本就是要找到它。 - 团队清楚 BaseWindow 的语义差异:
dist/main/modules/browser/miniapp-detached-window.js:274-276——// Electron types each BaseWindow event separately; these are registered by name. - 应用里根本没有「所有顶层窗口」这个模型:
dist/main/windows/manager.js:15——const ALL_WINDOW_TYPES = ['onboarding', 'login', 'archon'];,Mini App 弹出窗口在两种后端下都没登记,destroyWindowsExcept()同样够不到它。 - 团队早就知道 BaseWindow 不受全局枚举覆盖:
dist/main/modules/browser/background-browser-render-host.js:193-212自行持有并destroy()它的 BaseWindow 槽位。
全代码库没有任何注释表明要排除 BaseWindow。这是架构性缺口:应用从未建模「所有顶层窗口」,始终只枚举其中一个子类。
为什么不能直接把 getAllWindows() 换掉
BaseWindow.getAllWindows() 确实存在(当前 Electron 文档中 BaseWindow 的静态方法为 getAllWindows()、getFocusedWindow()、fromId(id)、clearPersistedState(name)),且因为 BrowserWindow extends BaseWindow 它是严格超集。但全局替换会引入三个回归:
BaseWindow没有webContents属性 ——webContents是BrowserWindow独有的,BaseWindow的实例属性只有win.contentView。原生后端的弹出窗口window.webContents为undefined。注意现有代码的可选链写在了错误的一侧:window.webContents.isDestroyed?.()中的?.保护的是.isDestroyed而非.webContents,window.webContents为undefined时照样抛TypeError。一旦换成BaseWindow.getAllWindows(),broadcastEmbeddedBrowserAgentAction、broadcastMiniAppSlept、broadcastRuntimeFailed与ipc/contextMenu.ipc.js:210会立刻崩 —— 当前使用BrowserWindow.getAllWindows()只是「意外地安全」。- 会把后台渲染槽位窗口一起捞进来 ——
dist/main/modules/browser/background-browser-render-host.js:38-65创建的槽位同样是BaseWindow、同样没有webContents,位于x:-10000, y:-10000、opacity:0、focusable:false,且showInactive()之后isVisible()仍为true。把它们纳入destroyAll()会在运行期拆掉后台 Agent 渲染用的合成面。 - 18 处调用点并非同一种语义 —— 销毁 / 广播 / 判空 / 选对话框父窗,四类职责需要四种不同答案。
建议修复方案
方案 A:建立显式的顶层窗口注册表(推荐)
新增单一归属方,统一登记「应用创建的所有顶层窗口」,并按用途打标:user(主窗口 / 登录 / 引导,以及 Mini App 弹出窗口,两种后端都登记)与 internal(后台渲染槽位)。弹出窗口创建时登记、closed 时摘除;后台渲染宿主登记为 internal。随后按语义消费:
| 用途 | 改法 |
|---|---|
| 强制销毁 | registry.destroyUserWindows();对 BaseWindow 先关闭其子 WebContentsView.webContents 再销毁 |
| 运行时广播 | registry.broadcast(channel, payload) —— 只发给 user 且拥有 webContents 的窗口 |
| 判空 | registry.hasUserWindow() |
| 对话框父窗 | 先在注册表里按 contentView.children 解析 owner,再回退到某个 BrowserWindow,都没有则不设父窗 |
| 主题背景色 | 注册表中 user 且有 webContents 的窗口 |
| 右键菜单 | 同上 |
方案 B:分级枚举(改动更小)
// windows/top-level-windows.js
const internalWindows = new WeakSet();
function registerInternalWindow(window) { internalWindows.add(window); }
function getUserFacingWindows() {
return electron_1.BaseWindow.getAllWindows()
.filter((w) => !w.isDestroyed() && !internalWindows.has(w));
}BackgroundBrowserRenderHost 创建槽位时调用 registerInternalWindow(window),随后逐个迁移调用点;广播类站点额外按 w.webContents 过滤,并把可选链修正为 w.webContents?.isDestroyed?.()。
两案都必须补上:显式关闭子视图
Electron 官方文档明确写道:
When you add a
WebContentsViewto aBaseWindowand theBaseWindowis closed, thewebContentsof theWebContentsVieware not destroyed automatically. It is your responsibility to close thewebContentswhen you no longer need them, e.g. when theBaseWindowis closed.
Unlike with aBrowserWindow, if you don't explicitly close thewebContents, you'll encounter memory leaks.
NativeBrowserView.destroy(parent)(dist/main/modules/browser/native-browser-view.js:34-46)已经做了 detach(parent) + this.view.webContents.close(),但 destroyAll() 直接 window.destroy() 绕过了它,渲染进程会泄漏。另外 BaseWindow.destroy() 不会触发 close 事件(Electron 文档:"close event will also not be emitted for this window, but it guarantees the closed event will be emitted"),这意味着销毁方必须自己负责释放子视图。
相关
同一功能区域的另一个问题:#633 —— Mini App 弹出为独立窗口后,先关闭 MiniMax Code 会留下一个无法关闭的假死窗口。其中的「应用级退出清理覆盖不到弹出窗口」正是本 issue 的第 1 条后果。
另外两点可直接修掉的小缺陷
- 可选链写错侧:
window.webContents.isDestroyed?.()在没有webContents的窗口上必抛,应为window.webContents?.isDestroyed?.()。目前只是因为枚举排除了BaseWindow才没暴露,一旦修正枚举就会变成崩溃。 application-bootstrap-production.js:385的激活判空应改为统计「面向用户的窗口」,而不是BrowserWindow的数量。
Redacted error summary
No response
Screenshots
No response
Before submitting
- I have searched existing issues.
- I have included my version and removed sensitive information.
Activity