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

**URL:** https://discourse.mozilla.org/t/browser-runtime-sendmessage-throws-no-connection-error-when-popup-is-trying-to-talk-to-terminated-background-event-page/120888
**Category:** Development
**Created:** [July 7, 2023, 4:36pm UTC](https://discourse.mozilla.org/t/browser-runtime-sendmessage-throws-no-connection-error-when-popup-is-trying-to-talk-to-terminated-background-event-page/120888 "2023-07-07T16:36:47Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![u9t1a0d](https://avatars.discourse-cdn.com/v4/letter/u/f04885/32.png) [@u9t1a0d](https://discourse.mozilla.org/u/u9t1a0d)
#### Post date: [July 7, 2023, 4:36pm UTC](https://discourse.mozilla.org/t/browser-runtime-sendmessage-throws-no-connection-error-when-popup-is-trying-to-talk-to-terminated-background-event-page/120888/1 "2023-07-07T16:36:47Z")

</div>

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.

But:  
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.

---

<div class="post-metadata">

### Author: ![u9t1a0d](https://avatars.discourse-cdn.com/v4/letter/u/f04885/32.png) [@u9t1a0d](https://discourse.mozilla.org/u/u9t1a0d)
#### Post date: [July 7, 2023, 5:22pm UTC](https://discourse.mozilla.org/t/browser-runtime-sendmessage-throws-no-connection-error-when-popup-is-trying-to-talk-to-terminated-background-event-page/120888/2 "2023-07-07T17:22:10Z")

</div>

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`.
