I’ve migrated my addon to MV3. I need to run a longer-running task in the background that fetches and pushes various things from/to the network, but the background page (now an Event Page) gets terminated before the task can finish. How is this supposed to work? What’s the use of an Event Page when I can’t run the task I need to run? The only option I see is to open a new tab an run the task in there which is super ugly.
I’ve build this helper (in typescript) for my Chormium extension that will perform dummy “getCurrent” operation (to extend the 30s timeout kill) while it waits for the asynchronous operation to finish:
// executes long-running async work without letting the service worker to die
export async function withoutKillingWorker<T>(asyncWork: () => Promise<T>): Promise<T> {
const id = self.setInterval(() => browser.tabs.getCurrent(), 2e4);
return await asyncWork().finally(() => self.clearInterval(id));
}
Generally speaking the background context should stay alive as long as necessary to complete long running work, but there may be cases where we don’t quite live up to that ideal.
@Marcel_Klehr, can you describe in more detail exactly what work is failing to complete in time. Or better yet, if possible could you share a branch of your repo or demo that shows this issue in action?
my extension is running a synchronization in the background that triggers lots of network requests and eventually changes some bookmarks. You can observe the behavior in this version of the extension: https://github.com/floccusaddon/floccus/releases/tag/v5.0.5