fix: Handle "Extension context invalidated" errors from stale content scripts#309
fix: Handle "Extension context invalidated" errors from stale content scripts#309
Conversation
Co-authored-by: ujiro99 <677231+ujiro99@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #309 +/- ##
==========================================
- Coverage 19.03% 19.02% -0.01%
==========================================
Files 312 312
Lines 28849 28865 +16
Branches 1322 1322
==========================================
+ Hits 5490 5491 +1
- Misses 23359 23374 +15 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-authored-by: ujiro99 <677231+ujiro99@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Co-authored-by: ujiro99 <677231+ujiro99@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
Pull request overview
拡張機能の更新後に古い content script が動き続けた場合に発生する "Extension context invalidated" エラーを、ストレージ層で握りつぶして挙動を安定化し、同種エラーを Sentry 送信対象から除外するためのPRです。
Changes:
BaseStorage.get/setに"Extension context invalidated"発生時のハンドリング(get はデフォルト返却、set は false 返却)を追加await利用箇所のchrome.runtime.lastErrorチェックを削除し、例外として扱う形に整理- Sentry の
beforeSendで"Extension context invalidated"をフィルタしてイベント送信を抑止
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| packages/extension/src/services/storage/index.ts | storage get/set の例外ハンドリング追加(context invalidated を握りつぶし) |
| packages/extension/src/lib/sentry/index.ts | beforeSend で context invalidated エラーをフィルタして Sentry 送信を抑止 |
| await area.set({ [key]: value }) | ||
| try { | ||
| if (area === chrome.storage.sync) { | ||
| await debouncedSyncSet({ [key.toString()]: value }) |
There was a problem hiding this comment.
chrome.storage.sync 経路は debouncedSyncSet が chrome.runtime.lastError をログ出力して Promise を reject しないため、拡張コンテキスト無効化などで実際に失敗してもこの try/catch に入らず true を返し得ます。今回の「invalidated なら false」の意図に揃えるなら、debouncedSyncSet 側で lastError 時に reject する/失敗情報を返す等でエラー伝播を揃えてください。
| await debouncedSyncSet({ [key.toString()]: value }) | |
| await new Promise<void>((resolve, reject) => { | |
| area.set({ [key]: value }, () => { | |
| if (chrome.runtime.lastError != null) { | |
| reject(chrome.runtime.lastError) | |
| } else { | |
| resolve() | |
| } | |
| }) | |
| }) |
| if (isContextInvalidated(error)) { | ||
| console.warn("Extension context invalidated, ignoring storage get") | ||
| return defaultValue as T |
There was a problem hiding this comment.
この console.warn は selectionchange など高頻度で呼ばれる経路から発火し得るため、コンテキスト無効化中に大量ログになる可能性があります。1回だけ警告する(フラグで抑制/レート制限)か、必要なら error も併記しつつ最小限に抑える形にすると運用上安全です。
| if (isContextInvalidated(error)) { | ||
| console.warn("Extension context invalidated, ignoring storage set") | ||
| return false | ||
| } |
There was a problem hiding this comment.
この console.warn も高頻度に呼ばれる可能性があるため、コンテキスト無効化中にログスパムになり得ます。警告を1回に抑制する(フラグ/レート制限)など、運用上のノイズを抑える対応を検討してください。
| } catch (error) { | ||
| if (isContextInvalidated(error)) { | ||
| console.warn("Extension context invalidated, ignoring storage get") | ||
| return defaultValue as T | ||
| } |
There was a problem hiding this comment.
BaseStorage.get/set にコンテキスト無効化時の分岐(デフォルト返却 / false返却)が追加されていますが、packages/extension/src/services/storage/index.test.ts では現状この分岐の回帰検知ができません。area.get/area.set が Extension context invalidated で reject したときに握りつぶすこと、他のエラーは再throwすることをユニットテストで追加してください。
There was a problem hiding this comment.
以下のテストを index.test.ts に追加しました。(e570696)
BS-GET-01:area.getが "Extension context invalidated" で reject → デフォルト値を返すBS-GET-02:area.getが他のエラーで reject → 再throwBS-SET-01:area.setが "Extension context invalidated" で reject →falseを返すBS-SET-02:area.setが他のエラーで reject → 再throw
| const exceptionValue = event.exception?.values?.[0] | ||
| if ( | ||
| exceptionValue?.value?.includes("Extension context invalidated") || | ||
| exceptionValue?.type?.includes("Extension context invalidated") | ||
| ) { |
There was a problem hiding this comment.
"Extension context invalidated" のフィルタが追加されていますが、index.test.ts に beforeSend フィルタの挙動(該当時に null が返って送信されない/非該当時はイベントが返る)を検証するテストが見当たりません。回帰防止のためテスト追加をお願いします。
There was a problem hiding this comment.
customBeforeSend を TestUtils にエクスポートし、以下のテストを追加しました。(47fb946)
ST-BF-01:valueに "Extension context invalidated" を含む →nullを返すST-BF-02:typeに "Extension context invalidated" を含む →nullを返すST-BF-03: 無関係なエラー → イベントをそのまま返すST-BF-04:exception.valuesが存在しない → イベントをそのまま返す
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ling Co-authored-by: ujiro99 <677231+ujiro99@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Co-authored-by: ujiro99 <677231+ujiro99@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
拡張機能の更新後に古い content script が動き続けた場合に発生する
"Extension context invalidated"エラーを、ストレージ層で適切にハンドリングし、Sentry への報告を抑止します。Changes
BaseStorage.set—area.set()を try/catch でラップし、Extension context invalidated発生時は警告ログを出力してfalseを返す(未処理の rejection を伝播させない)BaseStorage.get—area.get()を try/catch でラップし、Extension context invalidated発生時は警告ログを出力してデフォルト値を返す。また、デフォルト値のクローン処理を遅延評価(値が null/undefined のときのみ実行)に変更してパフォーマンスを改善BaseStorage.get—await使用時はchrome.runtime.lastErrorのチェックが不要なため削除(エラーは Promise reject として表面化するため)Sentry.customBeforeSend—exception.values[0].valueと.typeの両フィールドをチェックし、Extension context invalidatedエラーを Sentry 送信前にグローバルにフィルタstorage/index.test.ts—BaseStorage.get/setのコンテキスト無効化ハンドリングのユニットテストを追加(invalidated 時の握りつぶし・その他エラーの再 throw の両ケース)sentry/index.test.ts—customBeforeSendフィルタの挙動テストを追加(該当エラー時にnullを返す・非該当時はイベントをそのまま返す)Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.