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

2

31

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

Illailladvised answered 17/3, 2011 at 10:15 Comment(2)
Please provide your code inside background page and content script.Linetta
Side note: If anyone having this error in their react application, react dev tools prints this error. It will be disappear when you disable the extension.Autocephalous
M
24

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.

Metalline answered 24/6, 2011 at 2:40 Comment(0)
S
3

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.

Seam answered 10/12, 2017 at 21:50 Comment(2)
sendMessage() or postMessage()?Luckily
chrome.runtime.sendMessage() developer.chrome.com/extensions/runtime#method-sendMessageSeam

© 2022 - 2024 — McMap. All rights reserved.