Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upChrome: "The message port closed before a response was received." #130
Comments
Fixes mozilla#130.
This comment has been minimized.
This comment has been minimized.
Thanks for this report - I can confirm that this happens with and without |
Use webextension-polyfill fork to fix mozilla/webextension-polyfill#130 Make sure no `onMessage` listeners are `async`. `WorkerProgram` and `RendererProgram` both listen to all messages from background, but only handle the ones meant for them based on an `if` statement. However, if the listener is an `async` function it will *always* return a Promise, indicating that the listener wants to send a response. It is only allowed to send *one* response per message per document. Otherwise Chrome shows an error.
This comment has been minimized.
This comment has been minimized.
What if the content script goes away after receiving the message, and cannot send a response? |
This comment has been minimized.
This comment has been minimized.
Then the In Firefox, the message is "Message manager disconnected" |
This comment has been minimized.
This comment has been minimized.
hey , can some one give me simple solution |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Add a return true. It will force chrome to wait for the response. chrome.extension.onMessage.addListener((request, sender, sendResponse) => {
// Look up a term from the dictionary using the Ajax API.
const lookupURL = `http://dictionary-lookup.org/${request.arg}`;
sendRequest(lookupURL)
.then(resp => {
sendResponse(resp || '{}');
})
.catch(error => {
sendResponse('{}');
});
return true; // Inform Chrome that we will make a delayed sendResponse
}); example |
This comment has been minimized.
This comment has been minimized.
i think that the: return true; // Inform Chrome that we will make a delayed sendResponse Doesn't solve the issue. |
This comment has been minimized.
This comment has been minimized.
@vrgomes is correct, this fix does not work - is there another resolution to this issue? |
This comment has been minimized.
This comment has been minimized.
Any updates on this? |
This comment has been minimized.
This comment has been minimized.
Using @RobertJGabriel 's answer as a place to start, I found the error occurs when we neglect to send a response at all. I found I needed to call
|
This comment has been minimized.
This comment has been minimized.
Hello, in my case I call sendResponse in my background.js then return true but like @vrgomes said are just delayed so the only difference is that I get error messages later in my background console. |
This comment has been minimized.
This comment has been minimized.
@reZach it looks like to work that way! Greetings. |
This comment has been minimized.
This comment has been minimized.
Yes, you will need that too @vrgomes. I forgot to put that in my reply. |
This comment has been minimized.
This comment has been minimized.
The recent comments are about the non-polyfilled The original reported issue has been fixed a long time ago (in version 0.3.0) by #140 . If you still encounter this issue, upgrade the polyfill to the latest version. If you can still reproduce this issue despite using the latest version, please file a new issue. |
Minimal repro repo:
https://github.com/lydell/webextension-polyfill-messaging-issue
This happens with both v0.2.1 on npm and when built from commit 2537b23.
Summary
If a
browser.runtime.onMessage
callback does not return a Promise,browser.runtime.sendMessage
will be rejected with the following error, causing noise in the console:Workaround
Even if you don't want to send a response, always return a promise
in your
onMessage
callback:You can also make the
onMessage
callbackasync
to implicitly return a Promise (resolving toundefined
in the below example).Files
Copied over for convenience from:
https://github.com/lydell/webextension-polyfill-messaging-issue
Solution?
Should the "The message port closed before a response was received." be detected, and the promise should be resolved with
undefined
?