My MV3 extension has a background service worker that has listeners for chrome.tabs.onActivated and chrome.tabs.onUpdated. The callbacks in the listeners are responsible for injecting content scripts and updating popup/action details. Relevant code: https://github.com/damianmgarcia/Hide-n-Seek/blob/main/extension/background/js/background.js#L265
I noticed that my extension's service worker was failing to inject content-scripts for seemingly no reason after hours, or even days of the extension working without problem. Without a successfully-injected content script, my extension becomes useless.
Reloading the tabs did not fix the issue. Closing and reopening the tabs did not fix the issue. However, closing and reopening Chrome did fix the issue.
I reported this to crbugs in February when I noticed it, and at the time I attributed the problem to the chrome.tabs API because chrome.tabs was returning undefined within the context of a service worker with tabs permissions.
However, this problem goes beyond the chrome.tabs API. Other listeners on other APIs are also affected.
There have been several reports of MV3 service worker listeners becoming unresponsive after varying periods of time. Here are 4 that I am aware of (including my own report):
Issue 1419673: chrome.tabs is sometimes undefined in MV3 background service worker (my report)
Chrome Extension V3 event listeners stop working after several days
Chrome extension not receiving events after service worker restarts
In report #2, a user named Yacov gave a detailed answer with some insight and a possible solution to the problem. I tried the solution but it did not work in my case. However this detail may be important:
"Here, I am not quite sure what is happening (whether there is or isn't a bug in Chrome), but it seems like after X period of time, the Chrome API listeners stop responding. As the extension never stops and restarts, there is no recovery of the listeners and the background script is forever stuck in this state. The extension icon works without an issue, but the main script is never executed. Only a restart of Chrome or the computer seems to resolve this." -- Yacov
After I read this, I paid attention to my service worker's state and I noticed the following:
When there is no issue (i.e. my extension is working fine), my service worker inactivates after 30 seconds, which is the expected behavior.
When the issue arises (i.e. the service worker's listeners stop working and content scripts stop being injected), I have noticed that my service worker never inactivates. It gets stuck on the “RUNNING” state when viewed from the service worker internals page. This is not the expected behavior.
Is anyone else aware of this issue? Why would the service worker’s listeners stop working after hours or days? Why is the service worker getting stuck in the “running” state?
Is anyone else aware of this issue? Why would the service worker’s listeners stop working after hours or days? Why is the service worker getting stuck in the “running” state?
--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/7bffec5e-c350-417e-b8e3-1674ea985f45n%40chromium.org.
Thank you, Simeon. I am experiencing this issue as I write this. The sw-internals page lists the following for the extension:
Scope: chrome-extension://eaiedffaednbjaggdbkmgkmoekngebjp/
Registration ID: 4997
Navigation preload enabled: false
Navigation preload header length: 4
Active worker:
Installation Status: ACTIVATED
Running Status: RUNNING
Fetch handler existence: DOES_NOT_EXIST
Fetch handler type: NO_HANDLER
Script: chrome-extension://eaiedffaednbjaggdbkmgkmoekngebjp/background/js/background.js
Version ID: 6714
Renderer process ID: 4160
Renderer thread ID: 1
DevTools agent route ID: 1
The service worker has been stuck on "RUNNING" for over 1 hour. Here are some notes on some of the service worker's listeners:
chrome.tabs.onActivated listener does not respond when switching tabs
chrome.tabs.onUpdated listener does not respond when refreshing tabs
chrome.storage.local.onChanged listener does not respond when adding or removing properties
chrome.runtime.onMessage listener does respond when I select my extension name from a target page’s devtools “javascript context” dropdown, and then send a message using chrome.runtime.sendMessage
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/7812dc2d-b0f5-4014-82ad-5abcaabc59f9n%40chromium.org.