Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is causing my chrome extension's background.js to freeze up and not respond to messages

Every once in a while my chrome extension's background.js page freezes, i have no idea what is causing it.

When the background.js file has frozen, it no longer responds to messages from the content script, and when I try to open the background page via the extensions manager to inspect it, the window pops up but it stays blank, and no interface appears.

The only things im doing in the background page are message passing and retrieving localstorage variables.

I cant figure out what is causing this, the bug only seems to have happened since i transitioned to the new chrome.runtime api, from the chrome.extension api

Can anyone tell me what is going wrong here? or help me figure it out? Thanks!

Heres the background.js file's code in its entirety

if (!chrome.runtime) {
  // Chrome 20-21
  chrome.runtime = chrome.extension;
} else if(!chrome.runtime.onMessage) {
  // Chrome 22-25
  chrome.runtime.onMessage = chrome.extension.onMessage;
  chrome.runtime.sendMessage = chrome.extension.sendMessage;

}

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if (request.method == "getLocalStorage")
    sendResponse({data: localStorage[request.key]}); // decodeURIComponent
  else if (request.method == "setLocalStorage")
    sendResponse({data: localStorage[request.key]=request.value});
  else
    sendResponse({}); // send empty response
});

Is it possible a deadlock situation is occurring that is freezing the page? It doesnt cause the CPU to go mad, so im guessing its not an endless loop.

Update here is the manifest.json as requested

{
   "manifest_version": 2,
   "content_scripts": [ {
      "exclude_globs": [ "http://*.facebook.com/ajax/*", "https://*.facebook.com/ajax/*" , "http://www.facebook.com/ai.php?*", "https://www.facebook.com/ai.php?*", "http://www.facebook.com/ajax/*", "https://www.facebook.com/ajax/*"],
      "include_globs": [ "http://*.facebook.com/*", "https://*.facebook.com/*" ],
      "js": [ "script.js" ],
      "matches": [ "http://*.facebook.com/*", "https://*.facebook.com/*" ],
      "run_at": "document_start"
   } ],
   "converted_from_user_script": true,
   "background": {"scripts": ["background.js"], 
                  "persistent": false},
   "icons": {
      "128": "ET-128x128.png",
      "48": "ET-48x48.png"
   },
   "key": "xxxxxxxxxxxxxxxxxxxx",
   "name": "Extension Test",
   "short_name": "ET",
   "description": "ET Does this and that, but doesnt phone home",
   "version": "999",
   "homepage_url": "http://www.etphonehome.com"
}

Only disabling and re-enabling the extension get it to start working again, once the background page has frozen

Below is a screenshot of the frozen background page inspection window: here is a screenshot of the frozen background page inspection window

like image 816
user280109 Avatar asked Oct 20 '22 14:10

user280109


1 Answers

The localStorage API is problematic because in chrome it is a synchronous API to an inherently asynchronous operation (called from a renderer process, which must then communicate with the browser process that reads from / writes to a backing store in the filesystem and possibly replies back to the renderer process). While it should not in theory be possible to cause deadlocks from webpage or extension code, it's possible there are bugs in chrome's implementation.

One thing you might try is switching from localStorage to chrome.storage.local. It is a little more work to use since it has an asynchronous API, but does not suffer from the same implementation complexity as localStorage.

E.g.

sendResponse({data: localStorage[request.key]});

becomes

chrome.storage.local.get(request.key, function(storageResult) {
  sendResponse(storageResult[request.key]);
});
like image 131
Antony Sargent Avatar answered Nov 14 '22 23:11

Antony Sargent