Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did my Javascript run out of asyncIds ? (RangeError in inspector_async_hook.js)

First i use async and await very often and i get this error:

RangeError: Value undefined out of range for undefined options property undefined
    at Set.add (<anonymous>)
    at AsyncHook.init (internal/inspector_async_hook.js:19:25)
    at PromiseWrap.emitInitNative (internal/async_hooks.js:134:43)

And i dont know how i can fix this, i write my code completly in Typescript and i dont created any file that is named 'async_hooks'.

And i dont run more then 10 function async at once i use await very often so it shouldnt stack up but javascript seems not to reduce the asyncId and reach the number limit very fast.

I tried to use less async await but this didnt fix the problem, but the error msg comes later. If i use very less async await i can prevent that this error comes until the function successfully finish the job. (I use Electron 7)

Electron seems to have a very low async pool but it can be reproduced by a default typescript code:

class Test {
    private async testCompare(a,b):Promise<boolean> {
        return a == b;
    }

    public async testRun():Promise<void> {
        for (let index = 0; index < 999999999; index++) {
            for (let index2 = 0; index2 < 999999999; index2++) {
                await this.testCompare(index,index2)
            }
        }
    }

}
new Test().testRun();

This Code produce very much ram usage, and i think i have the same problem in my program. I think that the async pool get filled up until it reached its limit.

like image 728
Dani GTA Avatar asked Nov 02 '19 18:11

Dani GTA


People also ask

Does JavaScript block on await?

Though it creates a confusion, in reality async and await will not block the JavaScript main thread. Like mentioned above they are just syntactic sugars for promise chaining. Putting other way both code snippets below are same.

Is JavaScript async blocking?

JavaScript is single-threaded and runs in the same thread as the UI. So all JavaScript code will block the UI. As mentioned by others web workers can be used to run code in other threads, but they have limitations. The difference between async functions and regular ones is that they return a promise.

How do you handle multiple asynchronous calls in node JS?

In order to run multiple async/await calls in parallel, all we need to do is add the calls to an array, and then pass that array as an argument to Promise. all() . Promise. all() will wait for all the provided async calls to be resolved before it carries on(see Conclusion for caveat).


2 Answers

I got the same error for Set.add as you did once my set size reached 16777216 (2^24). I'm unable to find the info on this limit in the documentation, but I'll assume sets are limited to 16777216 unique values?

Easily tested with a simple for loop.

This will throw the exact same error:

let s = new Set();
for (let i = 0; i <= 16777216; i++) s.add(i);

This will run successfully:

let s = new Set();
for (let i = 0; i < 16777216; i++) s.add(i);

Do note that this will eat up some ~5GB of memory, so increase your heap limit accordingly if it's crashing due to memory restrictions.

like image 143
MunyuShizumi Avatar answered Sep 27 '22 02:09

MunyuShizumi


Just encountered this using a Set, i used the following as a quick workaround

class Set {
  hash = {}

  add (v) {
    this.hash[JSON.stringify(v)] = true
  }

  has (v) {
    return this.hash[JSON.stringify(v)] == true
  }
}

this has no limit, only limited by your systems memory

like image 44
Biereagu Sochima Avatar answered Sep 26 '22 02:09

Biereagu Sochima