How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue?
In case you're an extension developer who googled your way here trying to stop causing this error:
The issue isn't CORB (as another answer here states) as blocked CORs manifest as warnings like -
Cross-Origin Read Blocking (CORB) blocked cross-origin responsehttps://www.example.com/example.html with MIME type text/html. Seehttps://www.chromestatus.com/feature/5629709824032768 for moredetails.
The issue is most likely a mishandled async response to runtime.sendMessage. As MDN says:
To send an asynchronous response, there are two options:
- return true from the event listener. This keeps the sendResponsefunction valid after the listener returns, so you can call it later.
- return a Promise from the event listener, and resolvewhen you have the response (or reject it in case of an error).
When you send an async response but fail to use either of these mechanisms, the supplied
sendResponse argument to
sendMessage goes out of scope and the result is exactly as the error message says: your message port (the message-passing apparatus) is closed before the response was received.
Webextension-polyfill authors have already written about it in June 2018.
So bottom line, if you see your extension causing these errors - inspect closely all your onMessage listeners. Some of them probably need to start returning promises
(marking them as async should be enough). [Thanks @vdegenne]
If you go to chrome://extensions/, you can just toggle each extension one at a time and see which one is actually triggering the issue.
Once you toggle the extension off, refresh the page where you are seeing the error and wiggle the mouse around, or click. Mouse actions are the things that are throwing errors.
So I was able to pinpoint which extension was actually causing the issue and disable it.