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

A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received #384

Open
fregante opened this issue Jun 5, 2022 · 7 comments

Comments

@fregante
Copy link
Contributor

fregante commented Jun 5, 2022

I'm seeing this message in Chrome 102 and the 5-result search page suggests that this is a new message: https://www.google.com/search?q=%22A+listener+indicated+an+asynchronous+response+by+returning+true%22

Screen Shot 2

No-polyfill repro

// in background
chrome.tabs.sendMessage(65, 'good soup', () => console.error(chrome.runtime.lastError))

// in tab
chrome.runtime.onMessage.addListener(() => true)

Then close the tab

Polyfill repro

// in background
await browser.tabs.sendMessage(65, 'good soup')

// in tab, never-resolving promise
browser.runtime.onMessage.addListener(() => new Promise(console.info))

Then close the tab

What's happening

My guess is that Chrome used to return "The message port closed before a response was received." but now it returns a different message, which isn't handled here:

const CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE = "The message port closed before a response was received.";

To be clear, Chrome still immediately returns the previous message when chrome.runtime.onMessage.addListener was never called. Here's what the call looks like before and after installing the onMessage listener

Screen Shot

Possible solution

Feel free to update the PR or replace it with your own solution, I just hope this is fixed in a timely manner because you'll likely get more issues like this one 😁

Related

@fregante
Copy link
Contributor Author

fregante commented Jun 6, 2022

This might actually be an opportunity to fix this very related issue:

But I'd still rather see a quick fix merged as soon as possible at least to preserve the old behavior.

@Rob--W
Copy link
Member

Rob--W commented Jun 9, 2022

This changed in https://bugs.chromium.org/p/chromium/issues/detail?id=1304272.
The original message was split in two different cases in https://chromium.googlesource.com/chromium/src/+/9256bc8b668f61e755ab587f623e7da94d8a6f47%5E%21/#F4, by emitting "A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received" instead of "Could not establish connection. Receiving end does not exist." in some cases.

The fix in #385 looks good.

@johnmalib
Copy link

This error message indicates that there was a problem with the communication between the listener and the message channel. The reason is that sendMessage is now promisified internally, so you can 'await' it, but the by-product is that when you don't specify a callback yourselves, it is added internally anyway in order for the call to return a Promise, which means that since you don't call sendResponse in onMessage, the API will think that it was you who made a mistake of using a callback and not providing a response, and report it as such.

Since the new behaviour is very confusing for almost everyone, a solution might be to stop showing this error when the callback is not specified, however it might cause confusion for those developers who still use callbacks and forgot to call sendResponse inside onMessage by mistake, which should be reported ideally as it always was.

If you see your extension causing this errors - inspect closely all your onMessage listeners. Some of them probably need to start returning promises (marking them as async should be enough).

If still error showing, try disabled all installed extensions in Chrome then you will get a clear console without errors.

@JohnnyQ352
Copy link

This is not a Chrome issue. This is an Adobe PDF extension issue. If you disable the Adobe PDF extension it will go away proving this fact.

Johnny.

@SQDCME
Copy link

SQDCME commented Jan 28, 2023

This is not a Chrome issue. This is an Adobe PDF extension issue. If you disable the Adobe PDF extension it will go away proving this fact.

Johnny.

I've been struggling with this since I setup OneSignal on my site and YES, I have the PDF extension active on my Chrome and YES, after I deactivated the extension, the error has not appeared since. Never would have thought to look for the fix concerning Adobe PDF, thank you!

@JohnnyQ352
Copy link

You are very welcome. I try to post all I find that I have noticed. Have a great day.

@photoThorsten
Copy link

In my case it seems that the "Shoop Cashback & Gutscheine"-Chrome-Extension causes this message, as it came after installing that said extension und it doesn't come after deactivating that extension...

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 a pull request may close this issue.

6 participants