Is it possible for async_ids to overflow?

58 Views Asked by At

In node.js when one performs something that's async in nature, an async_id is generated for it. This id is accessible via the async_hooks API.

Since the ids are integers (64bit) is it possible that they overflow sometime?

It seems like the only reason for ids to reset is when there is an exception. https://github.com/nodejs/node/blob/94454927f697840a25c1ae73ebbcf9a5324b9060/lib/internal/process/execution.js#L129

1

There are 1 best solutions below

0
On

I haven't dug into Node.JS code, and for this answer I am gonna assume those async_id's are generated starting at 0 and going up. If negative values also enter the equation this goes even crazier.

When you have 64 bits (int) to store a value, you have 2^63 = 9,223,372,036,854,775,808 ~= 9.22e+18 possible positive values (including 0).

That means you have that number of possible async indexes to be created while your program is running.

Let's suppose an extreme case, where your program creates 1 billion async operations per second, and is running nonstop for 100 years (both things are nearly impossible):

  • 100 years = 3,153,600,000 ~= 3.15e+9 seconds
  • 1,000,000,000 async operations * 3,153,600,000 seconds = 3,153,600,000,000,000,000 ~= 3.15e+18

This means your program would have to be running nonstop roughly for another 200 years to actually overflow the promise indexes. That means 300 years.

You can judge for yourself but I think it is empirically impossible.

Hope this helps.