Service Worker code paused when popup is closed in MV3

178 views
Skip to first unread message

Shiva Chaitanya Mothkuri

unread,
Mar 12, 2024, 9:11:17 AMMar 12
to Chromium Extensions
Hi,

I'm trying to execute some long running code(puppeteer script) from service worker. When I invoke this action from popup by clicking a button, the code starts running.

When the popup loses focus and close during puppeteer navigation, or I manually close the popup - the code execution pauses there, not totally stops. If I open the popup by clicking on the extension icon, the execution resumes from the exact line of code where it was paused last time.

This was working fine on MV2 with background js. I'm totally confused what's happening here. I followed multiple articles where it was suggested to follow some steps to keep the service worker alive. But, nothing works.

The issue is that, service worker is not getting terminated - but just getting paused.

Could anyone help how to fix this issue.


This is the code in my service worker. Simplified actual promises from my actual code, to print numbers to console every second. Please look at the attached video for clear understanding of my issue.

I'm expecting the background code to run even though popup is closed.
async print(i){
return new Promise(function(resolve, reject) {
console.log("i=", i)
setTimeout(() => {
resolve(i)
}, 1000)
});
}

async executePuppeteerCode(tabId, port) {
console.log("tabId", tabId)
for(let i=0; i < 100000; i++){
await this.print(i)
}
}
Screen Recording 2024-03-12 at 6.33.36 PM.mov

Patrick Kettner

unread,
Apr 19, 2024, 10:37:44 AMApr 19
to Shiva Chaitanya Mothkuri, Chromium Extensions
Hello Shiva
That is the expected behavior in manifest v3. Service workers are intended to run as minimally as possible. There are a number of heuristics that are used to determine when to stop the service worker. Here is our documentation about how to keep a service worker alive

--
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/1e37b38f-0898-428a-af26-34b17d1220dbn%40chromium.org.
Reply all
Reply to author
Forward
0 new messages