Unchecked runtime.lastError: The message port closed before a response was received.

14,700 views
Skip to first unread message

Suchintan Das

unread,
Nov 25, 2021, 11:58:24 AM11/25/21
to Chromium Extensions
I am new to chrome extension and was working over a project of chrome extension in manifest v3, while using messaging I faced this error that my response port is closing earlier and is not able to receive the response. I tried various tutorial and google searches to find a right solution to this but to can't really find a solution that worked for my case.

If anyone knows how to fix the problem please share.

background.js


chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    let url=tab.url;
    console.log(tab);
    if(url.indexOf("https://www.amazon.in/s?k=")>-1 && changeInfo.status==='complete')
        {
            productsearch(url);
            producttrigger();
        }
 });
const productsearch = (url) => {
    let extract=url.substring(url.indexOf("=")+1,url.indexOf("&"));
    extract=extract.replace("+"," ")
    console.log(extract);
}
const producttrigger = () =>{
    chrome.runtime.sendMessage({
            show:true
        },function(response){});
}
function getCookies(domain, name, callback) {
    chrome.cookies.getAll({ "url": domain }, function (cookies) {
        if (callback) {
            callback(cookies);
        }
    })};
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
        if (!message.myPopupIsOpen) {
            console.log("ehhloo", message)
            return;
        } else {
            getCookies(`https://${DOMAIN_NAME}`, COOKIE_NAME, function (refreshToken) {
                console.log(refreshToken);
                let url;
                chrome.tabs.query({
                    active: true
                }, tabs => {
                    // console.log("Inside this function!")
                   
                });
                sendResponse({success:true})
   
            });
   
        }
           
   
    });

popup.js

let loc = "../pages/sign-in.html"
function changing(){
     chrome.runtime.sendMessage({
        'myPopupIsOpen': true
    }, function (response) {
        if (response.success) {
            loc = "../pages/preference.html"
        }
    });
}
Thank you in advance

Krisztián Balázs Farkas

unread,
Nov 26, 2021, 11:57:50 AM11/26/21
to Suchintan Das, Chromium Extensions
For me what was working (sometimes) is that returning a true from the handler. AFAIK that could solve this problem. Just put a return true at the end of the if (!message.myPopupIsOpen) function. Another solution could be to use onConnect and postMessage (long-lived connection). Not sure if that's the best idea, but that could work for sure.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/40c1d8a9-e4dd-4e24-a32e-b2eed4e24b2fn%40chromium.org.

rahul kumar

unread,
Jan 23, 2023, 12:54:02 AM1/23/23
to Chromium Extensions, farkas.kris...@gmail.com, Chromium Extensions, suchintand...@gmail.com
This error message indicates that there was a problem with the communication between the listener and the message channel. The reason is that sendMessage is now promisified internally, so you can 'await' it, but the by-product is that when you don't specify a callback yourselves, it is added internally anyway in order for the call to return a Promise, which means that since you don't call sendResponse in onMessage, the API will think that it was you who made a mistake of using a callback and not providing a response, and report it as such.

Since the new behaviour is very confusing for almost everyone, a solution might be to stop showing this error when the callback is not specified, however it might cause confusion for those developers who still use callbacks and forgot to call sendResponse inside onMessage by mistake, which should be reported ideally as it always was.

If you see your extension causing this errors - inspect closely all your onMessage listeners. Some of them probably need to start returning promises (marking them as async should be enough).

If still error showing, try disabled all installed extensions in Chrome then you will get a clear console without errors.

wOxxOm

unread,
Jan 23, 2023, 9:30:59 AM1/23/23
to Chromium Extensions, ling...@gmail.com, farkas.kris...@gmail.com, Chromium Extensions, suchintand...@gmail.com
The previous message is a copypaste of my old comment about a slightly different problem. In this case the reason is much simpler: you call chrome.runtime.sendMessage when the popup is not shown. The popup cannot receive messages if it's not shown. You can suppress the error by using `()=>chrome.runtime.lastError` instead of `function(response){}`. Another solution would be show the popup in a new window using chrome.windows.create or as a DOM element inside the web page.

Atakan ATEŞ

unread,
Feb 16, 2023, 8:23:33 AM2/16/23
to Chromium Extensions, Suchintan Das
I get the same error on my website https://www.fuux.net. According to my research, it is a common error of chromium browsers. There seems to be no solution, it should be ignored.

25 Kasım 2021 Perşembe tarihinde saat 19:58:24 UTC+3 itibarıyla Suchintan Das şunları yazdı:
Reply all
Reply to author
Forward
0 new messages