Closed Bug 1720808 Opened 3 years ago Closed 1 year ago

[Enhancement] Consider creating a runtime.beforeServiceWorkerShutdown event for service workers in extensions using Manifest v3.

Categories

(Core :: DOM: Service Workers, enhancement)

Firefox 91
enhancement

Tracking

()

RESOLVED INVALID

People

(Reporter: anonymous30901032, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0

Steps to reproduce:

The name could also be called runtime.beforeSuspend to make it more consistent with other runtime events. This event would allow an extension to return {cancel: true} to avoid shutting down a service worker. Here are a few concerns that this could help address if extension background pages are removed and replaced with service workers ( https://bugzilla.mozilla.org/show_bug.cgi?id=1578286 ):

  1. Excess overhead. Some extensions monitor frequently fired events like webRequest, webNavigation, and tabs.onUpdated. It is not logical to continually start and terminate service workers that are extensively used, whether they have low or high memory usage (Chrome terminates a service worker if it has been idle for 30 seconds).

  2. Global variables and race conditions. Many extensions require global variables and switching to browser.storage.local to store them may cause race conditions, as it is async. Some extensions will now require something like a mutex library to avoid this situation, adding more complexity to extensions.

  3. Local storage and large data structures. Some extensions may need to add an extra permission 'Unlimited storage' to store variables that would otherwise have been a global variable in a background script. Also, some data structures may not be possible to save in local storage and could be time consuming to create or initialize.

  4. Slow delays on service worker startup. The delay may have an impact when a quick response is required. For example, imagine if a user tries to load a URL and Firefox restarts a large service worker that is listening to webRequest.onBeforeRequest and it takes 500 ms to restart. This introduces an excessive delay before the tab can begin to load.

  5. No warning when a service work is terminated. An extension may be waiting on a setTimeout, a promise, or a user event and shouldn't be shutdown. An extension may also need to place data in local storage or do some cleanup activity before it is terminated.

The average extension background pages doesn't take up much memory, especially compared to a tab. For example, uBlock Origin is around 150 KB and a YouTube tab is around 30+ MB. Most users only have a few extensions installed at most, so there would rarely be any performance issues when some service workers are kept alive full time.

Extension developers may have to create an extension tab with a moz-extension URL as a new 'hub' if background pages are removed and service workers can't avoid termination. This requires an extra permission (tabs.hide API), explanations to our user base, instructions on not to discard the tab, etc... It would be nice if this could be avoided.

The Bugbug bot thinks this bug should belong to the 'Core::DOM: Service Workers' component, and is moving the bug to that component. Please revert this change in case you think the bot is wrong.

Component: Untriaged → DOM: Service Workers
Product: Firefox → Core
Status: UNCONFIRMED → RESOLVED
Closed: 1 year ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.