PSA: Change to onMessageExternal asynchronous responses

13 views
Skip to first unread message

Justin Lulejian

unread,
1:41 PM (2 hours ago) 1:41 PM
to Chromium Extensions

Dear Chromium Extension Developers,


TL;DR: If your extension uses runtime.onMessageExternal for asynchronous responses, you must now explicitly return true; to prevent the “The message port closed before a response was received.” error in Chromium 145.0.7614.0 and later.


As part of recent messaging changes, we identified and fixed a bug in the onMessageExternal listener that was not behaving per API documentation. Although this was mentioned in the original thread, we want to ensure wider visibility.


The runtime.onMessageExternal listener previously kept the message channel open even without explicitly returning true, allowing asynchronous responses without error. After the fix, developers must* explicitly return true; from the listener to send an asynchronous response and prevent a “The message port closed before a response was received.” error.


Prior to the fix, this example worked without return true;:

// background.js

function onMessageExternalListener(message, sender, sendResponse) {

  setTimeout(() => {

    sendResponse('pong');

  }, 100);

  // Listener should `return true;` but actually didn't need to.

}

chrome.runtime.onMessage.addListener(onMessageExternalListener);


After the fix, return true; is required:


// background.js

function onMessageExternalListener(message, sender, sendResponse) {

  setTimeout(() => {

    sendResponse('pong');

  }, 100);

  return true;

}

chrome.runtime.onMessage.addListener(onMessageExternalListener);


We apologize for any unexpected errors or inconvenience this may have caused. In the future, we will ensure API changes like this are announced more explicitly to give extension developers ample time to adapt.


Thank you,

Justin, on behalf of the Chromium Extensions Team

*: As of Chrome 146 you may also return a Promise to indicate an asynchronous response.
Reply all
Reply to author
Forward
0 new messages