I understand web workers and write an application ( github ) for downloading the list of VKontakte group members. Authorization and receipt of the group id occurs in the “frontend”, and a long download is entrusted to the “worker” - the dedicated web worker 'y.

On groups up to 3.5 million works. But I tried MDK with their 7 million and ran into a console error: InternalError: too much recursion . Firefox 41.0.1, os x 10.11.

How to more accurately understand the cause of this error?

Could this be a data memory limit (I assume that about 70 megs weigh the id in text form)? Or is it how I crookedly organized calls to api - is there a callback snowball winding up somewhere?

You can reproduce the error here : log in via VC and enter the mdk group. I fall off at 65–71%.

I make requests to the VKontakte API via jsonp : I form the url of the request and add the callback parameter with the name of the function that I call “on return”. And I execute importScripts() with this url.

All code is wrapped in an object that hangs its method for handling a callback:

 self.callback = this.runLoop.bind(this); 

Maybe this design creates an extra round of call stack?

I would be grateful for the ideas on the debugger worker, as well as help in debugging the project.

  • The problem, as I understand it, is in importScripts - you run it in runLoop , then without leaving it the same method is called again, etc. Did not try to contact VC asynchronously, through the same ajax? Then the start of the method would always be "clean", i.e. without any recruitment - BOPOH
  • Through XMLHttpRequest will not work cross-domain. Therefore, jsonp c importScripts() . It is asynchronous. - Sergiks
  • Write console.log(1); after importScripts console.log(1); and put a breakpoint on it. What do you think - when will it work for you? And what kind of asynchrony are you talking about? There is no asynchrony, there is synchronism, there is a limit of recursion, which you rest against - BOPOH
  • 1. Wrote. Conclusions are poured into the console in the course of reading a large group - i.e. execution there reaches, and - comes to the end. 2. how can a debugger put a breakpoint inside the web worker's code? FireBug inside WW does not know how. - Sergiks
  • one
    I use chrome. Put brekpoint on importScripts, put on console.log. It comes to importScripts and again to him and again to him, etc. Those. console.log will be reached only when returning from recursion. Wrapped importScripts in setTimeout(function() {importScripts (url);}, 100) - reached 100%, i.e. just did what I suggested - the call to VC occurs asynchronously - BOPOH

1 answer 1

It seems that I did not take into account that importScripts() not only imports the script, but also executes it immediately, even before exiting the function. This causes a lump of calls to the runLoop() method, which overflows the stack.

Fixed wrap in setTimeout() :

 setTimeout( function(){ importScripts(url)}, 0); 

Thanks @BOPOH for discovering and explaining this point.