Why is my web worker terminated?

I try to access web workers when I encounter very complex behavior. For some reason, it stopped after a few seconds, although I have a code in it that works.

Here is my code;

Main JavaScript file:

$(document).ready(function () { var worker = new Worker("js/TestWorker.js"); worker.addEventListener('message', function (event) { console.log(event.data); }); worker.addEventListener('error', function (event) { console.log(event); }); }); 

Working file:

 (function () { var updateCounter = 0; var updater = function () { updateCounter += 1; console.log("Update counter: " + updateCounter); postMessage("test"); setTimeout(updater, 10000); }; updater(); })(); 

As indicated, the employee simply ceases to function after a few seconds, 10-20 seconds or so.

But if I add this piece of code to my main JavaScript file,

 var check = function () { var localWorker = worker; // setTimeout(check, 1000); }; // setTimeout(check, 1000); 

The employee works as intended. SetTimeout calls are also not required, so they are commented out. (Note that I can simply replace the destination with a working .length or something similar, and it will still work fine.

Can someone explain this behavior? Does the working and (erroneously) garbage collected by the browser end up, or is something else happening here that I am missing?

It should be noted that my browser (Chrome) does not display any errors or warnings on the console.

EDIT: The same behavior is observed, regardless of whether the code is executed inside an anonymous function or not.

EDIT2: If I put a working variable in the global scope, it does not end prematurely. What could be the reason for this?

+6
source share
3 answers

Some studies show that while web workers should function as you expect (i.e. they will not collect garbage noticeably), there are some known issues in Chrome that mean you cannot rely on this behavior.

Of particular interest is this very similar error: https://bugs.chromium.org/p/chromium/issues/detail?id=572225 , which, in turn, refers to a more basic error: https: //bugs.chromium. org / p / chromium / issues / detail? id = 572226

It seems that this is due to the garbage attempt to collect workers who cannot perform any future actions (in this case, the garbage collection will be undetectable, as expected), but the lack of detection logic for this state means that any pending actions that directly not associated with a response to an incoming message, will be ignored.

Basically, although you should be sure that web workers behave like DOM nodes that can be deleted explicitly, in practice (for now) you need to make sure that you always keep a link to a worker accessible from somewhere else when the garbage collector hits him, he can kill the worker. This is only necessary if you are using setTimeout or the like in a worker; if he just replies to messages, you will not have a problem.

+2
source

Maybe working var should be global

 var worker; $(document).ready(function () { worker = new Worker("js/TestWorker.js"); worker.addEventListener('message', function (event) { console.log(event.data); }); worker.addEventListener('error', function (event) { console.log(event); }); }); 
+2
source
 (function () { ... })(); 

This is an anonymous function that will be called once after the definition, after which the browser discards it. Your web worker determines its scope and why it only works for a short period of time.

0
source

All Articles