MV3: How am I supposed to run a task that can run for ~5mins in the background?

Hi,

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.

Thank you for your help
Marcel

1 Like

I think you need to stream/checkpoint the state of your task to chrome.storage.session. This may require difficult design changes.

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));
}

But I’m not sure it will work also in Firefox…

Thank you! I just tested it, and it doesn’t seem to work

According to the source code, these seem to be the reasons for stopping a background page termination: https://searchfox.org/mozilla-central/source/toolkit/components/extensions/parent/ext-backgroundPage.js#826-842

I’m not entirely sure what each of these is, though

1 Like

Maybe @rob will be able to help? :slight_smile:
(I’m sorry for the ping Rob, I hope I’m not disturbing you from something important!)

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?

Hi @dotproto

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