Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 735
Marek Avatar asked Mar 17 '11 10:03

Marek


2 Answers

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.

like image 71
abraham Avatar answered Jan 02 '23 04:01

abraham


In my case, however, I needed to provide the sendResponse function to the chrome.runtime.sendMessage(msg, responseCallbackMissingHere), which will give you the "Attempting to use a disconnected port object" error.

like image 33
Doug Avatar answered Jan 02 '23 05:01

Doug