Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix images breaking on Firefox after a while (add heartbeat to prevent background page reap) #1852

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

djahandarie
Copy link
Collaborator

@djahandarie djahandarie commented Feb 28, 2025

Since switching our image rendering to be over postMessage / MessageChannels, Firefox has had an issue where when its background page is reaped (which happens on MV3 extension on Firefox) after some time, and images will break on any existing tabs, while the rest of the extension will work (because sendMessage will cause the background page to come back alive, with a new worker, but the frontend will not know that it needs to reconnect to a new worker), and also new tabs will work because they will reconnect to the new backend worker.

I considered some different ideas (like ① putting a heartbeat directly in frontend worker to backend worker, and autoreparing, or ② having the backend signal all frontends that it's going byebye onSuspend), but in the end this idea was the simplest, to simply prevent the background page from dying.

The rest is as the comment says:

   /**
     * This is used to keep the background page alive on Firefox MV3, as it does not support offscreen.
     * The reason that backend persistency is required on FF is actually different from the reason it's required on Chromium --
     * on Chromium, persistency (which we achieve via the offscreen page, not via this heartbeat) is required because the load time
     * for the IndexedDB is incredibly long, which makes the first lookup after the extension sleeps take one minute+, which is
     * not acceptable. However, on Firefox, the database is backed by sqlite and starts very fast. Instead, the problem is that the
     * media-drawing-worker on the frontend holds a MessagePort to the database-worker on the backend, which closes when the extension
     * sleeps, because the database-worker is killed and currently there is no way to detect a closed port due to
     * https://github.com/whatwg/html/issues/1766 / https://github.com/whatwg/html/issues/10201
     *
     * So this is our only choice. We can remove this once there is a way to gracefully detect the closed MessagePort and rebuild it.
     * @returns {Promise<import('api').ApiReturn<'heartbeat'>>}
     */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant