MV3 service worker listeners stop working

762 views
Skip to first unread message

Damian Garcia

unread,
Apr 6, 2023, 12:52:50 PM4/6/23
to Chromium Extensions

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):

  1. Issue 1419673: chrome.tabs is sometimes undefined in MV3 background service worker (my report)

  2. Chrome Extension V3 event listeners stop working after several days

  3. Chrome extension not receiving events after service worker restarts

  4. chrome.tabs.onActivated.addListener causing this error : Uncaught TypeError: Cannot read properties of undefined (reading 'onActivated')

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:


  1. When there is no issue (i.e. my extension is working fine), my service worker inactivates after 30 seconds, which is the expected behavior.

  2. 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?


Simeon Vincent

unread,
Apr 6, 2023, 1:48:07 PM4/6/23
to Damian Garcia, Chromium Extensions
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?

I don't recall seeing mention of extension SWs getting stuck in a "RUNNING" state before – wOxxOm tends to stay abreast of extension issue comments and may be able to correct me. I am aware of multiple issues related to SWs not receiving events as expected. There's definitely at least one bug in Chromium, possibly more. Here's a list of similar issues I saw just now when digging through the Chromium issue tracker. 
  • 1271154: MV3 service worker broken after auto-update and manual refresh
  • 1316588: browserAction not getting dispatched when extension button clicked
  • 1403815: MV3 worker is not resuming after stop
  • 1417943: ServiceWorker chrome.tabs events stop triggering after a while even if ServiceWorker is active/running
  • 1426461: SW stops working in MV3
I should also note that I've seen similar, but I suspect different problems with MV2 event pages. I'm pretty sure I've seen more than one of these issues, but this is the only one I could quickly find.
  • 1316588: browserAction not getting dispatched when extension button clicked
Finally, there are a few similar issues specifically observed when performing automated tests.
  • 1371432: Extension service worker goes into dead mode, no way to Active in puppeteer(MV3)
  • 1325792: MV3 Service worker does not automatically restart when suspended in Selenium Webdriver tests
So far we collectively don't have a good understanding of why these issues are happening. One theory chromium engineers are investigating is that there may be an issue with how inter-process messages are being handled, but only time will tell.

Simeon - @dotproto


--
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.

Damian Garcia

unread,
Apr 6, 2023, 4:54:21 PM4/6/23
to Chromium Extensions, Simeon Vincent, Chromium Extensions, Damian Garcia

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

Pavel Aronovich

unread,
Apr 10, 2023, 9:18:24 AM4/10/23
to Chromium Extensions, Damian Garcia, Simeon Vincent, Chromium Extensions
Hi,

Maybe it's worth adding your information to this error.

Thanks.

четверг, 6 апреля 2023 г. в 23:54:21 UTC+3, Damian Garcia:

Sonali Agrawal

unread,
Jul 7, 2023, 2:05:49 AM7/7/23
to Chromium Extensions, Damian Garcia, Simeon Vincent, Chromium Extensions
Hi Damian, 

This is exactly what I am facing too.

If I keep my extension idle for some hrs/days, and upgrade the extension, the listeners stops working. And without upgrade as well the issue occurs. 

In sw-internals page, the status shows "RUNNING" though.

Please let me know if you found any solution or workaround around this.

Oliver Dunk

unread,
Jul 7, 2023, 4:43:34 AM7/7/23
to Sonali Agrawal, Chromium Extensions, Damian Garcia, Simeon Vincent
Hi all,

I just wanted to jump in to say that if you don't see an open bug that covers what you're seeing (which sounds like it might be the case), please do file a new one.

If you have steps to reproduce and a demo extension that is the key thing that makes it most easy to work on a fix. We can still investigate without that, but it's much harder to figure out what's going on. But of course I understand that getting these bugs fixed is important regardless.

We fixed https://bugs.chromium.org/p/chromium/issues/detail?id=1271154 recently which I don't expect is the same but I'd encourage you to try on Canary and just make sure that hasn't solved the issue :)

Thanks,
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


Reply all
Reply to author
Forward
0 new messages