Reason for Uncaught Error: Attempting to use a disconnected port object

Marek picture Marek · Mar 17, 2011 · Viewed 32.9k times · Source

I am getting this error in background page when responding to request from content script. Does anyone know what can be causing this error?

Full stack trace:

Uncaught Error: Attempting to use a disconnected port object   chrome/RendererExtensionBindings:147 
chrome.Port.postMessage     chrome/RendererExtensionBindings:147 
chromeHidden.Port.dispatchOnConnect.connectEvent         chrome/RendererExtensionBindings:89 
myExtension.foo.sendResponse.state      background.js:1573 
db.readTransaction.tx.executeSql.paramStr      background.js:1038 

This only happens after a couple of hours of browsing reloading extension is not helping - not that it would be a solution after restarting chrome browser all gets back to normal for couple of hours content script can keep sending request to background but NO response from background can be sent back Is there any way I could catch this Uncaught error and reset the listener?

I am using chrome.extension.onRequest.addListener for my communication. Before I respond I query DB and I do not send any other response before query is finished.

Thanks, Marek

Answer

abraham picture abraham · Jun 24, 2011

This is caused when a connection get closed. For example if you open a tab that has the content_script injected, it opens a connection, the tab is closed, and then the background_page tries to pass a message. It will fail because the tab is no longer active to receive the message.

In your case I would guess that as tabs close and new tabs open you are attempting to post messages with the old tabId instead of creating a new connection to the new tab. I would recommend reading through the long-lived connections section again.