Event page: In onMessage, sendResponse to content script from within XHR event handler fails silently

3,577 views
Skip to first unread message

bzlm

unread,
Dec 21, 2013, 6:21:08 AM12/21/13
to chromium-...@chromium.org
I have an extension with an injected content script that sends a message to an event page, and the event page fetches an URL using XHR and responds to the message when the XHR has finished. But the response never reaches the content script. 

I'm *guessing* this is because the on-demand port created by the messaging system is torn down when the listener function returns, which is before the sendResponse occurs. But if that's the case, is the only solution creating a persistent port (which I haven't tried yet)? Or is something else afoot here? When debugging, I can see that the messaging internals *try* to send some response to some port. There's no visible error.

I can verify that it works if the sendResponse is *not* inside onreadystatechange.

Event page ("background.js") code:

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                sendResponse("response"); } };
        xhr.open("GET", "http://url/with/permissions", true);
        xhr.send(); }

Content script code:

chrome.runtime.sendMessage(null, "1", {}, function (response) {  
    alert("Got response!"); })

PhistucK

unread,
Dec 21, 2013, 7:50:51 AM12/21/13
to bzlm, Chromium-extensions
When you add a breakpoint at the sendRespone line (within the onreadystate handler), does Chrome break on it?
If it does and your content script still does not run the callback, you can search crbug.com for an existing issue and star it. If you cannot find one, file a new issue using the "New issue" link on the same page. It would be helpful and may expedite the process if you attached a reduced test case (like the one you mentioned) to the issue.
Please, do not add a "+1" or "Me too" or "Confirmed" (or similar) comment. It just wastes the time of Chrome engineers and sends unnecessary e-mails to all of the people who starred the issue.

Thank you.


PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/39bd75fe-d900-4a1e-84e7-c781f0492d0b%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/groups/opt_out.

bzlm

unread,
Dec 21, 2013, 1:02:17 PM12/21/13
to chromium-...@chromium.org, bzlm
Yes, Chrome breaks on it, and I can step into messaging internals that attempt to post a message to a port.

I've filed an issue and included the minimal repro in the original post. https://code.google.com/p/chromium/issues/detail?id=330415

I'm working around this using the "persistent port" approach, which works fine. 

Scott Fujan

unread,
Dec 21, 2013, 4:39:38 PM12/21/13
to bzlm, Chromium-extensions

You have to return true at the end of the listener. Otherwise, the response function gets cleaned up before it can be called.

PhistucK

unread,
Dec 22, 2013, 12:45:12 AM12/22/13
to Antony Sargent, François Beaufort, bzlm, Chromium-extensions, Scott Fujan
I remember reading that (in this group) already (though I forgot it). I see that the documentation does not include this valuable and time saving piece of information.

Can someone, please, update the documentation?
Source path -

Thank you!


PhistucK


François Beaufort

unread,
Dec 23, 2013, 3:59:16 AM12/23/13
to PhistucK, Antony Sargent, bzlm, Chromium-extensions, Scott Fujan, man...@chromium.org
Renato, can you have a look at this?

On Sun, Dec 22, 2013 at 6:45 AM, PhistucK <phis...@gmail.com> wrote:
I remember reading that (in this group) already (though I forgot it). I see that the documentation does not include this valuable and time saving piece of information.

Can someone, please, update the documentation?



François Beaufort | beaufort...@gmail.comging.html

bzlm

unread,
Dec 23, 2013, 6:08:36 AM12/23/13
to chromium-...@chromium.org
It's mentioned in the actual *documentation* for the onMessage event, here:

The onMessage documentation is linked from the message passing tutorial, so I should have seen it. But I was looking for the cause everywhere but in the actual event listener...

PhistucK

unread,
Dec 23, 2013, 1:30:05 PM12/23/13
to bzlm, Chromium-extensions
Since others were doing the same, I think it is worth mentioning this in the message passing documentation as well, perhaps in a note similar to this one -
"Note: If multiple pages are listening for onMessage events, only the first to call sendResponse() for a particular event will succeed in sending the response. All other responses to that event will be ignored."


PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

Renato Mangini (Chromium)

unread,
Dec 27, 2013, 10:25:08 AM12/27/13
to François Beaufort, PhistucK, Antony Sargent, bzlm, Chromium-extensions, Scott Fujan, Renato Mangini (Chromium)

Sure! Could you guys create a bug at crbug.com and assign it to me?

Thanks, 

bzlm

unread,
Dec 27, 2013, 12:56:32 PM12/27/13
to chromium-...@chromium.org
Please mention in the bug to include sample code both in the message passing tutorial and the messaging API example code that actually returns something in the listener (even false, perhaps with a comment that explains why). As it stands, only the onMessage sendResponse property documentation reveals that the return value of the listener function is ever used.

PhistucK

unread,
Dec 27, 2013, 2:31:03 PM12/27/13
to bzlm, Chromium-extensions
You can file an issue by yourself and mention whatever you want there. crbug.com. Post a link to the created issue and it will get triaged.


PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

François Beaufort

unread,
Dec 31, 2013, 3:29:57 AM12/31/13
to Renato Mangini (Chromium), PhistucK, Antony Sargent, bzlm, Chromium-extensions, Scott Fujan

Kyle Poole

unread,
Jun 13, 2014, 2:49:12 AM6/13/14
to chromium-...@chromium.org
I just wasted 2 hours on this, wondering if I'd forgotten how Javascript works :/

While I see it in the documentation now, I don't think it stands out well enough. Something like a bold "The listener must return true if you'd like to use the sendResponse callback asynchronously" would've saved me the trouble, I think.

Marc Fawzi

unread,
Jun 13, 2014, 7:27:28 AM6/13/14
to Kyle Poole, chromium-...@chromium.org
use Stackoverflow for documentation ;) I recall finding it out about the return true business on there, not the Chrome Extension docs


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
Reply all
Reply to author
Forward
0 new messages