Can be background script reloaded (by Firefox) without reloading whole addon?

Is it possible that Firefox can reload my background script?
Like restarting it, maybe when resources are low…

After investigating some strange “Uncaught (in promise) Error: An unexpected error occurred” errors I’ve noticed it’s coming from my code for detecting container/private window:

const isPrivate = () => browser.runtime.getBackgroundPage().then(bc => bc === null);

I don’t know why but if you reload background script of any addon (using F5 in the inspect tab or by calling location.reload() in the background script) then when you execute browser.runtime.getBackgroundPage(), you get the error.

Now I don’t know if I should report it, since it’s kind of a special case…

I guess the general rule is report more even if that creates a duplicate wontfix. As long as you have an actual issue that you are pretty certain is not just you being silly (which is hard to find out sometimes, so making a minimal reproducible example is often helpful, also to double check you’re not doing something wrong).

1 Like

So as I was preparing example I’ve noticed that the problem is related to context menu(!!!???).

Maybe I’m doing something wrong? The code will be executed without errors, but then the browser.runtime.getBackgroundPage() will be broken…
This is my testing code:

(async () => {
  console.log('background is initialized');

  try {   // without this whole block, the code in "page.js" will work
    await browser.menus.removeAll();    // without this line there will be an error "Unchecked lastError value: Error: ID already exists: some_menu"
    browser.menus.create({    // does NOT return a Promise
      id: 'some_menu',
      title: 'Hello menu',
      contexts: ['page'],
      onclick: () => {},
    });
  } catch (e) {
    console.warn(e);   // this will never fire, there is no exception!
  }

  console.log('context menu created');

  const tabs = await browser.tabs.query({});
  if (!tabs.find(t => t.url.endsWith('/page.html'))) {
    await browser.tabs.create({url: '/page.html'});
    setTimeout(() => location.reload(), 3_000);
  }

})();

Whole testing example is here: https://www.dropbox.com/s/7ee7g6994b8f7w4/broken_context_menu_reload.zip?dl=1

EDIT:
Reported here: