Do iOS Safari ServiceWorkers get shut down due to thermal state?

Hi! I'm working on an iOS Safari extension that has a ServiceWorker. Lately we've noticed that this ServiceWorker seems to get killed seemingly at random, and there are no logs or crash reports to tell us what happened.

I'm hypothesizing that iOS might be shutting down Safari ServiceWorkers when the ProcessInfo.thermalState approaches .serious. I have circumstantial evidence that our ServiceWorker tends to get killed more often at higher levels of thermalState but can't yet say conclusively that this is the case. I can't find any direct evidence of this on internet searches either.

Is anyone able to shed light onto this topic? The specific symptoms are:

  1. ServiceWorker stops, and the menu entry for its console window no longer appears on macOS Safari.
  2. No crash logs via Xcode or Sentry, and no Console messages as far as we could tell (caveat: MobileSafari generates a LOT of messages! We might have missed it.)
  3. If attached via debugger, the native part of our extension just disappears and the debugger loses connection with no error message.
  4. ServiceWorker no longer works for the lifetime of the Safari process. Sometimes, when we kill Safari and restart, we can get the ServiceWorker back. This usually requires toggling our extension's "enabled" state in system settings.
  5. In some cases, even killing/relaunching Safari and toggling the system setting doesn't bring our ServiceWorker back. I'm hypothesizing right now that this happens when the thermal state is high.

I've tried simulating a serious/critical thermal state in the Xcode Devices window, but couldn't repro the ServiceWorker problem. I don't know if that setting affects the whole system, though, or just our own apps.

Help appreciated!

Yuna

Hi Yuna, we are dealing with pretty much exactly the same issue as you, did you end up finding a work around or fix for it?

Hi Yuna - I believe we're running into a similar issue. Our extension will work just fine for long periods of time, but then suddenly stop working for a while. Sometimes it will start working again, but often we need to disable/re-enable the extension. This causing big headaches for the team. In our situation, our website is trying to communicate with the onMessageExternal listener added via the background script. When it's not working, it returns undefined instead of the expected data from the chrome.storage.local. No errors, just silently returning undefined.

The issue is hard to reproduce. But with my own device, I tried opening a ton of apps. The extension still worked correctly, but then when I restarted Safari I got the error to reproduce. Not conclusive evidence (maybe the error would have shown up even without starting all the other apps) but gives us something to go on. Would love to hear what people have tried.

In the same boat here!

Following along here, as I am experiencing the same issue.

I may be in the same boat as well. Looking at documentation here: https://web.dev/learn/pwa/service-workers#:~:text=Service%20workers%20don't%20live,been%20busy%20for%20too%20long. in section service worker lifespan

"If not already running, a service worker will start whenever a network request in its scope is asked for, or when a triggering event, like periodic background sync or a push message, is received. Service workers don't live indefinitely. While exact timings differ between browsers, service workers will be terminated if they've been idle for a few seconds, or if they've been busy for too long. If a service worker has been terminated and an event occurs that would start it up, it will restart."

I'm more wondering what kind of event would restart it in an extension? I have a listener: chrome.runtime.onMessage.addListener(... but not sure if this even gets triggered to restart the service worker? Anyone else having similar trouble?

Service workers don't live forever... unless you give them something to do before they are shutted down by design. In the past this was the case for Chromium-based browsers, now also for Safari (and probably also for Firefox). For those who have this kind of problem, I suggest taking a look at (and studying) the post below and the related cited Github repositories (Highlander series), where a fully functional methodology for keeping a service worker active forever is exposed.

https://stackoverflow.com/questions/66618136/persistent-service-worker-in-chrome-extension/75082732#75082732

I'm also in this club. For some reason, the extension works pretty stable on my wife's phone. But needs to be reenabled once a day for me.

Do iOS Safari ServiceWorkers get shut down due to thermal state?
 
 
Q