`browser.runtime.sendMessage` throws "no connection" error when popup is trying to talk to terminated background event page

To reproduce this error:

Manifest version 3

In popup (“action”) a button click invokes chrome.runtime.sendMessage.
The background script is listening to it via chrome.runtime.onMessage.addListener and will open some extension page (i.e. settings page or contact page) in the callback.

The promise returned by chrome.runtime.sendMessage will be rejected with "Could not establish connection. Receiving end does not exist." if the background went idle/stopped running and its event page got terminated, so that the user has to click the popup button twice (since the first attempt will trigger the background and re-activate it).

How should this be dealt with?

As a workaround I could always try a second time to send the message(?) - but I’m sure there’s a saner way to handle this issue…

I think more generally speaking the issue is with not having persistent background scripts anymore in v3 and the lack of experience on how to handle non-persistent bg scripts.

Oooh, I see now!

So, it seems the background script gets executed each time it starts running again.
The problem in my case was that I do some async storage initialization and only after that I register the listener to listen to messages from elsewhere.

The fix then is: Register the listener in the background script right away (sync) and not postponed (i.e. after an async op).

Side note: The async initialization stuff shouldn’t even be executed multiple times during a single browser session -> can be safely moved to the callback of chrome.runtime.onInstalled.addListener.

1 Like