跳转至

[ISSUE-0062] background SW 缺 unhandledrejection guard

用户截图(v0.10.81 之后某个时刻)chrome 扩展错误面板: - [object Object] 上下文 background.js:5(匿名函数) - 代码段是 wxt/storage 的 getStorageArea() — chrome.storage permission 检查

排查

  1. manifest 是否含 storage:✅ v0.10.81 build 的 manifest.json 含 "storage" permission
  2. storage 是否 import-time check:✅ wxt/storage 是 lazy 检查(每次 getItem 才调 getStorageArea(),不是 import-time)
  3. chrome.storage 是否真 null:理论上不可能(permission 含 + SW context)

→ 错误本身可能是历史累积(用户 chrome 错误面板没清,旧版某个时刻产生)。 → 但没 SW guard 是真实缺陷:未来 SW 内任何 rejection(如 fetch 失败、chrome API 错)会冒到 chrome 错误面板。

修

src/entrypoints/background/index.ts 头部加 SW unhandledrejection guard(同 ext-context-guard 的 main/popup 版本):

if (typeof self !== 'undefined' && typeof self.addEventListener === 'function') {
  self.addEventListener('unhandledrejection', (e: any) => {
    const reason: any = e?.reason;
    const msg = String(reason?.message || reason || '');
    if (
      msg.includes('Failed to fetch') ||
      msg.includes('NetworkError') ||
      msg.includes('AbortError') ||
      msg.includes('Load failed') ||
      msg.includes('Extension context invalidated')
    ) {
      console.warn('[bg] suppressed network error:', msg);
      e.preventDefault?.();
      return;
    }
    // 非 Error 对象(chrome.runtime.lastError 等)
    if (reason && typeof reason === 'object' && !(reason instanceof Error) && !reason.stack) {
      console.warn('[bg] suppressed non-Error rejection:', reason);
      e.preventDefault?.();
    }
  });
}

self 在 SW context 是 ServiceWorkerGlobalScope,支持 unhandledrejection event。

为什么之前 ISSUE-0052 漏 SW

ISSUE-0052 v0.10.72 修了 main/popup/settings/results 4 个 entry 的 ext-context-guard import-time 自启。SW 漏了 — 因为 SW 不 import ext-context-guard.ts(ext-context-guard 内部 typeof window === 'undefined' 守卫只是不装 window listener,但 self listener 也没装)。

用户操作

升级 v0.10.82 后: 1. chrome://extensions/ → 找扩展 → 点扩展卡片的"错误" 2. 点 "全部清除" 把历史错误清掉 3. reload 扩展 4. 之后不该再有 [object Object] / Failed to fetch 冒出

如清完仍有同款错误 → 真有新问题 → 查 log 看具体 reason。

audit_grep

audit_grep:
  - pattern: "self\\.addEventListener\\(\\s*['\"]unhandledrejection['\"]"
    description: "background SW 必须保留 unhandledrejection guard(v0.16.74 起注册点=rejection-guard-install.ts,index.ts 不再注册防双挂)。"
    expect: present
    paths: "src/entrypoints/background/rejection-guard-install.ts"

相关

  • [[0052-ext-context-guard-import-time-init|0052-ext-context-guard-import-time自启-早期fetch漏过]] — 上一轮 main/popup 修,SW 漏
  • [[0034-failed-to-fetch-leak-to-chrome-err-log|0034-Failed-to-fetch-冒到chrome扩展错误日志]] — Failed to fetch 历史
  • 修bug全字典扫描 — 4 entry + SW 全字典扫

✅ No SW 裁决补记(v0.16.69,2026-09-24)

本档红线「No SW 加良性需主控裁决」首次行使:实测 5 条全为 SW 生命周期边缘 幂等操作(同族 Extension context invalidated 先例),归良性但 warn 级双通道 留痕不静默;开洞严格限定 fire-and-forget(有 callsite);unhandledrejection 路径红线原样保留。实现+判据见 v0.16.69 changelog 与 rejection-guard.ts 注释。

🔴 二次裁决(2026-09-24 深夜,Tony 复测证伪 v0.16.69 前提)

Tony 按「全部清除→重新加载→复测」验证(两张截图入会话档案):错误面板仍冒出 4-6 条 [bg] fire-and-forget @ alarms.create(...)/sw-startup: No SW。查证 Chrome 官方文档(Debug extensions 页):扩展错误页收集 runtime errors + console.warn + console.error——v0.16.69 实现注释「面板只收 error」是错误 认知,console.warn 降级对面板无效。

修正:noSwBenign 分支 console.warn → console.debug(面板不收 debug; DevTools verbose 档可查),appendSysLog 保持 warn 级——双通道留痕(系统日志 +verbose)不变,面板清静。unhandled-rejection/sync-error 路径红线不动。 挂载时序(luna NO-GO 两轮回环后定稿):注册隔离至 rejection-guard-install.ts (仅 background entry 链路消费,静态测试钉消费集合),由 background index.ts 首条副作用 import 触发——覆盖 jsstore/db 层依赖求值期;两轮被毙方案:挂 index.ts 模块体(ESM 静态依赖全求值完才执行模块体)、挂共享模块模块体 (页面链路经 short-circuit-log 也 import 它,guard 污染普通页面)。残留不可 拦窗口=wxt 垫片 + install 依赖链求值期 + Chrome 原生脚本加载失败。

环境噪音定性(不可拦,记档):An unknown error occurred when fetching the script.(Chrome 原生 SW 脚本加载失败,先于任何代码)与 bundle 垫片层 [object Object](background.js:5,guard 挂载前)——残留不可拦窗口=wxt 垫片 + system-log/storage 求值期 + Chrome 原生脚本加载失败,垫片层噪音接受现状。