Not able to test service worker lifecycle

171 views
Skip to first unread message

Manvel Saroyan

unread,
Oct 6, 2022, 4:44:35 AM10/6/22
to Chromium Extensions

We have a problem at our company of reliably testing service worker lifecycle and that currently blocks our MV2->MV3 migration, see details below 👇

We are using Puppeteer for testing our extension and was hoping to use it for testing the service worker lifecycle, our script does the following:

  • Open `chrome://serviceworker-internals` in a new tab

  • Loading our UI into another page

  • Click on stop button in `chrome://serviceworker-internals`

  • Click on the extension UI element that is supposed to wake up the extension.

The problem we are having is that when we are doing the test with automation framework (Puppeteer in our case) service worker is being killed for good, it never wakes up, even when the “start” button is clicked in chrome://serviceworker-internals page.

Was able to create a test case that actually reproduces that issue reliably here: https://github.com/Manvel/puppeteer-kill-extension-sw 

Also filed a bug report here: https://crbug.com/1371432 

One interesting observation is that the issue doesn’t happen when the service worker inspector page is open.

Wonder if you had a similar issue and how did you fix it?

wOxxOm

unread,
Oct 6, 2022, 11:31:05 AM10/6/22
to Chromium Extensions, Manvel Saroyan
Try starting the worker directly by sending ServiceWorker.startWorker command. Also try uninstalling the extension, restarting the browser, installing it again. If it helps, it's probably the same bug in ManifestV3 SW registration that plagues users randomly for about a year, still not fixed.

Manvel Saroyan

unread,
Oct 6, 2022, 2:10:42 PM10/6/22
to Chromium Extensions, wOxxOm, Manvel Saroyan
Thanks for the pointers @wOxxOm and for all the work you do here for the community, super helpful.


> Try starting the worker directly by sending ServiceWorker.startWorker command

That would defeat the purpose of the test unfortunately, as plan is to ensure that we can re-activate service worker from content script and extension pages when it's stopped. But I love the idea of having control over service worker through Devtools protocol, unfortunately I never manage to make it work with Puppeteer, probably missing something very trivial(don't have much experience using that protocol though): 
```
await (await workerTarget.createCDPSession()).send("ServiceWorker.stopWorker", {versionId: 0});
await (await workerTarget.createCDPSession()).send("ServiceWorker.startWorker", {scopeURL: `chrome-extension://${extensionID}/`}); 
// ProtocolError: Protocol error (Target.attachToTarget): No target with given id found
```

> Also try uninstalling the extension, restarting the browser, installing it again
It happens every time service worker is stopped using automation framework, even after I reload extension, restart browser and execute same script the service worker will go into that dead mode again. It's somehow connected to accessing the Service worker https://github.com/Manvel/puppeteer-kill-extension-sw/blob/main/puppeteer.js#L15, feels like something is broken in there, some race condition that stops service worker for good. Interestingly enough, the issue is not reproducible when the service worker inspector page is open, which is also odd.

> ManifestV3 SW registration that plagues users randomly for about a year
Interesting, need to learn more about those.
Reply all
Reply to author
Forward
0 new messages