0

I am writing a Chrome extension for MV3, and am running into a problem where my extension stops working after a while.

Just after manually restarting my extension (and thus its service worker), everything works fine. But after, say, 20 minutes, my extension stops receiving any events I have added a listener for. I have to manually restart either the extension, or the service worker at chrome://serviceworker-internals/?devtools.

It appears to be because chrome stopped and then reran my service worker after some time. The problem is that this form of rerunning has my service worker not receiving any events.

The following is some minimal code to reproduce the issue:

console.log("Main");
chrome.webRequest.onBeforeRedirect.addListener(
    async (details) => {
        console.debug(details);
    },
    { urls: ["https://*/*"] }
);
chrome.webRequest.onBeforeRequest.addListener(
    async (details) => {
        console.debug(details);
    },
    { urls: ["https://*/*"] },
    ["requestBody"]
);
chrome.tabs.onUpdated.addListener(
    (tabId, changeInfo, tab) => {
        console.debug(changeInfo);
    }
);
chrome.webNavigation.onDOMContentLoaded.addListener(
    (details) => {
        console.debug(details);
    }
);

When I manually restart, I get the "Main" log and all the debug objects as expected. When I notice my extension stops working and check the service worker's logs, because it has been rerun, the console logs are since cleared, but the only log I see is "Main", implying my listeners did all get readded. Yet no events are received and debug logged. Upon manually restarting, I get the "Main" log and all the debug objects again as expected, until the next time it breaks.

I have seen this SO answer which says that a workaround is to subscribe to webNavigation if webRequest is not working. I am subscribed to both, and my issue is that the service worker is woken up and running, but its events are not firing.

Cloud
  • 938
  • 1
  • 8
  • 24
  • The events you have like onUpdated or webNavigation won't be able to wake up the worker in time for webRequest.onBeforeRequest so you'll have to keep the service worker [always running](https://stackoverflow.com/a/66618269). In Chrome Canary this webRequest bug should be fixed. – wOxxOm Sep 14 '22 at 13:23
  • As the OP of the linked answer, I appreciate your response. Could you elaborate on the worker not waking up in time? Even if the first event that causes the worker to be rerun has the worker wake too late to receive said first event, should subsequent events not fire as expected once the worker has woken up and is running? Regardless of how long I wait after the worker has woken back up, I do not seem to be receiving those events. – Cloud Sep 14 '22 at 13:27
  • The timeline goes something like this: Manually restart extension/worker → Refresh a page → Check worker logs ("Main", object, object etc.) → Wait about 20min for worker to get rerun → Notice extension events are not firing → Check if worker is running (it is) → Check worker logs (only "Main") → Try refreshing some pages → Check worker logs again (still only "Main") → Manually restart extension/worker again → Refresh a page → Check worker logs ("Main", object, object etc.) → ... – Cloud Sep 14 '22 at 13:33
  • The workaround mentioned there (using webNavigation or other events that wake the worker) can be used only if webRequest event that you want to observe occurs after the worker is already running. In your case onBeforeRequest can occur when the worker doesn't run, this event will be lost unless you force the worker to run forever as shown in the workarounds via messaging. – wOxxOm Sep 14 '22 at 14:46
  • I may be alright with losing a few events from the worker still starting. This would be a separate issue. However, my issue of not receiving events persists after the worker is already running. Say webNavigation wakes the worker, and webRequest fires around that time but is lost. A few seconds pass such that the worker has properly started up. Even after then, none of the 4 events I have added listeners to are firing, despite me actively performing actions while the worker has already rerun, that would normally cause them to fire (like refreshing pages). – Cloud Sep 14 '22 at 15:12
  • Sounds like a different bug. See if it occurs in Chrome Canary or in the [latest chromium builds](https://download-chromium.appspot.com). – wOxxOm Sep 14 '22 at 15:39

1 Answers1

1

This is a known issue with service workers. We have already introduced improvements and will continue to do so. As of now (May 10, 2023):

  • All extension events and API calls will extend the service worker lifetime.
  • Selected use cases such as native messaging will keep extensions service workers alive for longer than 5 min.
StormyKnight
  • 509
  • 3
  • 13
  • When the service worker inevitably stops, after it gets woken back up, I am still experiencing a lack of any received events despite once again adding listeners. It happens inconsistently, but has continued to be the case over the last few months. – Cloud May 16 '23 at 13:03
  • How are your events declared? [They shouldn't be nested](https://developer.chrome.com/docs/extensions/mv3/service_workers/events/#declare-events) . – StormyKnight May 17 '23 at 16:24
  • They are declared like the minimal code snippet in the question. Ie they are not nested. It may be worth noting that I most often use the extension by loading unpacked. – Cloud May 17 '23 at 22:30