chrome.runtime.onSuspend for MV3 service worker?

270 views
Skip to first unread message

Guy Erez

unread,
May 24, 2023, 6:34:12 AM5/24/23
to Chromium Extensions
Hey,

We've been looking for a solution to recognize when our service worker is about to be shut down (so we can perform backups, etc).

I saw that in the past, we could work with chrome.runtime.onSuspend, but it was only for the events page. Do we have a similar mechanism / a signal we can listen to in our extension's service worker?

Thanks!

Oliver Dunk

unread,
May 24, 2023, 6:58:03 AM5/24/23
to Guy Erez, Chromium Extensions
Hi Guy,

Unfortunately onSuspend isn't supported by service workers. Our thinking is that providing this may offer a false sense of security since this can happen in other ways (e.g a crash) where it won't run. We would also need to limit how much can be done to avoid things staying alive indefinitely.

I do personally think there's still some value to something like this, so I want to keep discussing it, but nothing today.

Do you have other workarounds in mind given that answer or would it help to discuss options? As an example, you could store things in the storage.session API that you don't want to lose.
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


--
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/d1cbf22b-5b89-4958-b735-da24e2042f0fn%40chromium.org.

Guy Erez

unread,
May 24, 2023, 9:27:03 AM5/24/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Guy Erez
Thanks for the quick reply Oliver!
Well, we do backup quite often, but we're seeing an edge case where our extension initialize quite often for some users.
My hunch is that there's a crash / the extension is being shutdown by the browser, and we have no idea it's coming - so we're missing some data.

Other than frequent backup, are there any other best practices we can employ?

Oliver Dunk

unread,
May 25, 2023, 8:57:25 AM5/25/23
to Guy Erez, Chromium Extensions
Could you say a little bit more about what you mean by "shutdown"?

Is the service worker just being terminated while idle? Or do you mean it is somehow crashing beyond that/no longer responding to events etc.?

Also, what sort of state do you have that you're backing up?

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

Guy Erez

unread,
May 25, 2023, 10:39:11 AM5/25/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Guy Erez
Sure, by "shutdown" I mean that the service worker is "killed" and re-initializes (or "installs" itself again) - running it's initialization code again.
It feels like it's somehow crashing & not responding to events.
We're backing things up using local storage.

Oliver Dunk

unread,
May 25, 2023, 2:23:08 PM5/25/23
to Guy Erez, Chromium Extensions
Hi Guy,

If I'm understanding correctly, this actually sounds like intended behaviour. Extension service workers (like web service workers) are shut down when idle: https://developer.chrome.com/docs/extensions/mv3/service_workers/service-worker-lifecycle/#idle-shutdown

If you'd like to avoid running initialization code more than once, you can look into events like runtime.onInstalled and runtime.onStartup. For persisting data in memory, the storage.session API can help: https://developer.chrome.com/docs/extensions/reference/storage/

Let me know if that matches what you're seeing.

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

Guy Erez

unread,
May 29, 2023, 10:05:55 AM5/29/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Guy Erez
Thanks!
I looked into runtime.onInstalled and runtime.onStartup, if I understand correctly, they're only triggered right before the service worker initializes.
Whereas I'd want to "catch" the moment before it shuts down so I can send logs/backup etc. Is that not possible?

Simeon Vincent

unread,
May 29, 2023, 9:25:08 PM5/29/23
to Guy Erez, Chromium Extensions, Oliver Dunk
Whereas I'd want to "catch" the moment before it shuts down so I can send logs/backup etc. Is that not possible?

Nope, it's not possible. The Worker and ServiceWorker interfaces don't have an equivalent to a windows beforeunload or unload events, so there's no way to detect and react to termination.

Simeon - @dotproto


Guy Erez

unread,
Jun 1, 2023, 10:30:22 AM6/1/23
to Chromium Extensions, Simeon Vincent, Chromium Extensions, Oliver Dunk, Guy Erez
Bummer, appreciate the conversation :)
Reply all
Reply to author
Forward
0 new messages