Skip to content

Message Port Closed Error in Chrome Extensions

This error message occurs when a Chrome extension's content script attempts to communicate with the background script but the communication channel closes before a response is received. While it doesn't typically break functionality, it indicates improper handling of cross-extension messaging.

Problem Overview

The error appears in developer consoles as:

Uncaught (in promise) 
message: "A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received"

This is not caused by your application code itself, but rather by browser extensions that improperly handle their internal messaging protocols.

Primary Cause: Browser Extensions

The overwhelming majority of these errors stem from browser extensions with faulty message handling:

WARNING

This error is typically caused by third-party extensions, not your application code. The most common culprits are ad blockers, privacy tools, and productivity extensions.

Common Offending Extensions

Based on community reports, these extensions frequently cause the error:

  • Ad blockers (AdBlock, uBlock Origin, etc.)
  • Ghostery (privacy protection)
  • Fonts Ninja (font identification)
  • BlockSite (website blocker)
  • Tampermonkey (userscript manager)
  • Avast Online Security & Privacy

Solutions

For End Users/Developers

If you're seeing this error while developing:

  1. Test in Incognito Mode: Open your application in Chrome's incognito mode (where extensions are disabled by default)

  2. Identify the Problematic Extension:

    • Disable extensions one by one
    • Refresh your application after each disable
    • When the error disappears, you've found the culprit
  3. Whitelist Your Site: For necessary extensions like Ghostery, add your development domain to the extension's trusted sites list

Ghostery whitelist settings

For Extension Developers

If you're building a Chrome extension, ensure proper message handling:

js
// Background script (service worker in MV3)
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  // Handle the message asynchronously
  someAsyncOperation().then(result => {
    sendResponse({data: result});
  });
  
  // Return true to indicate async response
  return true;
});
js
// Content script sending message
chrome.runtime.sendMessage('ping').then(response => {
  // Handle response
  console.log(response.data);
});

// Alternative callback style
chrome.runtime.sendMessage('ping', (response) => {
  // Handle response
  console.log(response.data);
});

DANGER

Never forget to call sendResponse() when you've indicated an asynchronous response by returning true. The message channel will close if the background script becomes inactive before sending a response.

Additional Causes and Solutions

Database Connections

In some cases, unclosed database connections can cause similar errors:

js
// Remember to close connections after use
connection = sql.connect(connectionParams);
// ... perform operations ...
connection.close(); // Don't forget this!

Hosts File Configuration

On Windows systems, ensure your hosts file contains proper localhost mappings:

127.0.0.1       localhost
::1             localhost

Extension Polyfill Issues

Some extensions use the webextension-polyfill library, which had compatibility issues with Chrome's messaging changes. These should be resolved in recent versions.

When to Be Concerned

This error is generally harmless for end users but should be addressed if:

  • You're an extension developer (fix your messaging code)
  • The error appears in production (users might have problematic extensions)
  • It's accompanied by actual functionality issues

For most developers, this error can be safely ignored during development once you've confirmed it's extension-related.

Conclusion

The "message port closed" error is primarily a Chrome extension issue rather than a problem with your web application. While annoying in development consoles, it rarely affects actual functionality. The best approach is to identify and configure problematic extensions rather than attempting to "fix" this in your application code.