New issue
Advanced search Search tips

Issue 649947 link

Starred by 15 users

Issue metadata

Status: Available
Owner: ----
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: All
Pri: 2
Type: Bug



Sign in to add a comment

unable to use chrome.runtime.sendMessage() after disable/enable of extension

Reported by aaronoro...@gmail.com, Sep 24 2016

Issue description

UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36

Steps to reproduce the problem:
My chrome extension uses executeScript on the background pages to inject some javascript into some webpages. After doing that if I disable and enable my chrome extension I get the following error on the frontend pages: 

VM1507 extensions::schemaUtils:112 Uncaught Error: Invocation of form runtime.connect(null, ) doesn't match definition runtime.connect(optional string extensionId, optional object connectInfo)

I have not been able to figure out a way to recover from this error except for reloading the tab. Here are the steps to reproduce: 

1) load new page
2) background page calls executeScript on new page
3) disable chrome extension and then enable it

4) on load of background page I query all of the tabs and run the executeScript on them again (this is in an attempt to fix this problem) but is also needed to handle the case when someone just installed the chrome extension for the first time so that it will work on all of their tabs out of the box. 

5) call chrome.runtime.sendMessage()  - which returns:

VM1507 extensions::schemaUtils:112 Uncaught Error: Invocation of form runtime.connect(null, ) doesn't match definition runtime.connect(optional string extensionId, optional object connectInfo)

If the executeScript had already previously been run on the tab.

What is the expected behavior?
Should not get an error. The frontend page should be able to message the background page as it was before extension was removed and reloaded.

What went wrong?
Getting error described above

Did this work before? N/A 

Chrome version: 53.0.2785.101  Channel: n/a
OS Version: 
Flash Version: Shockwave Flash 22.0 r0

Here's the thread where the issue was reported by another user as well: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-extensions/QLC4gNlYjbA
 

Comment 1 by phistuck@gmail.com, Sep 25 2016

If you can attach a basic extension that simply reproduces the issue (preferably, only this issue and no other functionality), that would help to triage this.

Comment 2 Deleted

Comment 3 by woxxom@gmail.com, Sep 25 2016

Apparently, the reasoning was to ensure security: all parts of an extension share same execution context, tracked internally in the browser. When the background page is reloaded, it's effectively destroyed and recreated thus no longer part of that context.

In short, the idea of this bugreport is to consider a possibility of reattaching "orphaned" contents scripts to the new context of the reloaded extension. Or maybe find another solution. The obvious challenge is whether it's possible to reliably avoid spoofing by a malicious 3rdparty extension.

Comment 4 by phistuck@gmail.com, Sep 25 2016

#3 - this does not happen when the background page is reloaded, it only happens when the extension is disabled and enabled (while this is also causing the background page to reload, it is not a required part, I imagine it would be the same with a regular extension page), even for code that is run using executeScript calls that occurred after the extension is re-enabled. This sounds like a bug (it is a new context, so why would not work?). A low priority bug, but a bug.
Components: Platform>Extensions
Labels: -OS-Linux OS-All
Just to be sure I'm understanding things, you're saying that if the extension had previously injected script via executeScript on a given page, then was disabled and re-enabled, that newly injected script (via executeScript) cannot call sendMessage? (It's somewhat expected that the old script context from before the disable/re-enable will be unable to connect, but the new script should be able to)

Yup, This is what I believe I'm seeing.

After enabling the extension I query for all the tabs and then execute the executeScripts again. The ones that never had the content script executed on them work fine as expected. But the previous ones I can never recover besides forcing a reload. 

Comment 8 by br...@bombbomb.com, Oct 18 2016

It seems/sounds like what Aaron is referring to is some what expected behavior per  http://crbug.com/168263 

I've been troubled over the same message, it used to be that an exception with the message  "Error connecting to extension xxxxxx" would be thrown when an extension was disconnected. That seems to have been replaced by "Invocation of form runtime.connect(null, ) doesn't match definition ...."  This change in behavior appears to been introduced a year ago for  http://crbug.com/537666  when the unload_event was removed.

Our chrome extension handles these issues gracefully by overriding and detecting exceptions in the runtime.sendMessage callback, although a better solution would be ideal for Chrome Extension context scripts to notify them a connection isn't available. The current error doesn't account for the extension being disconnected from the context script.

Looking at some old code, line 142, reflects that if no PortId was available an exception with the appropriate message would be thrown.
https://searchcode.com/file/32977419/src/chrome/renderer/resources/extensions/extension_custom_bindings.js

This is not the case in the current code, perhaps adding that back may clear up some confusion?  See line 161, exception is missing
https://cs.chromium.org/chromium/src/extensions/renderer/resources/runtime_custom_bindings.js?type=cs&sq=package:chromium

Hi br...@bombbomb.com - 

Would you mind showing me the snip of code you use to handle this gracefully? I know I can catch this exception there but then how do you get the page to reconnect?  Thanks. 

Comment 10 by br...@bombbomb.com, Oct 28 2016

Aaron,
I haven't found a easy way to reconnect without having to refresh the page, although we do prompt the user that the feature is unavailable and they need to refresh. The other option is to refresh the page automatically although that can tend to upset people to interrupt work that's in progress.

To refresh we just update the window.location, nothing fancy.  I'm still digging to see if it's possible to reconnect without prompting the user.
This issue also happens when extension users disconnect and connect internet.
More realistically, when someone doesn't shut down the laptop, pull down the screen and open the laptop again at a later point. This activity, disconnects and connects internet. 

I am facing the same error even in that secnario. Although not always but sometimes, which is making it difficult to replicate the issue.

The issue gets easily resolved when i refresh the page where content script is loaded.

Is there no solution to this issue yet?
Status: Available (was: Unconfirmed)
I observe this issue as well. For instance, while developing an extension I hit Ctrl+R, so all other extensions are also reloaded, and a bit of chaos described above ensues.
Cc: rdevlin....@chromium.org

Comment 14 by thd...@gmail.com, May 9 2017

This is a hard one to handle. One idea is to remove injected content scripts upon disabling the extension, but then you would have to retrospectively unregister all event handlers that were registered by the content script also.
For Aaron and others, here is something like the method we're using to help deal with this so our feature gracefully fail. It would be nice if Chrome could provide a native error handling at least, doing the override is a bit messy but we use also use it to proxy callbacks.

function SendMessageOverride(onDisconnected,exceptionLogger)
{

    var chromeSendMessage = chrome.runtime.sendMessage;
    chrome.runtime.sendMessage = function() {
        try
        {
            chromeSendMessage.apply(this,arguments);
        }
        catch (e)
        {
            if (e.message.indexOf('connecting to extension') !== -1 || e.message.indexOf('Invocation of form runtime.connect') !== -1)
            {
                backgroundDisconnected = true; // this is a global
                onDisconnected({error: 'Connection to Background Error', message: e.message});
            }
            else
            {
                onDisconnected({error: 'sendMessage Exception', message: e.message});
            }
            exceptionLogger.log('runtime.sendMessage Exception: '+e.message,{ stack: e.stack });
        }
    };

}

Comment 16 by woxxom@gmail.com, May 9 2017

A simpler connectivity check should do the trick I think:

function isBackgroundConnectable() {
  var port = chrome.runtime.connect();
  if (port) {
    port.disconnect();
    return true;
  }
  return false;
}

Project Member

Comment 17 by sheriffbot@chromium.org, May 10 2018

Labels: Hotlist-Recharge-Cold
Status: Untriaged (was: Available)
This issue has been Available for over a year. If it's no longer important or seems unlikely to be fixed, please consider closing it out. If it is important, please re-triage the issue.

Sorry for the inconvenience if the bug really should have been left as Available.

For more details visit https://www.chromium.org/issue-tracking/autotriage - Your friendly Sheriffbot
This happens to me too...
any suggestions?

Comment 19 by woxxom@gmail.com, May 13 2018

Assaf, the usual workaround is to reinsert the content script via chrome.tabs.executeScript.
Status: Available (was: Untriaged)
This looks like something worth addressing if we have the resources.

Sign in to add a comment