Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js setInterval() stops executing after 25 days

In my Node.js application, I use setInterval() to run a specific function every 1 hour. The function is executed properly for about 25 days, then the timer stops firing.

25 days seems awfully close to Node.js's TIMEOUT_MAX (2^31 milliseconds ≈ 25 days), but I don't really see why setInterval() should stop executing after that time.

Update:

I think this may have been caused by the following bug in Node.js: setInterval callback function unexpected halt #22149

like image 810
Moorglade Avatar asked Aug 14 '18 06:08

Moorglade


2 Answers

It seems the bug (#22149) has been fixed in Node.js 10.9.0.

It may be also worth noting that this bug seems to be influencing both setInterval() and setTimeout() (as reported here), so the workaround with setTimeout() in the callback function wouldn't work.

like image 156
Moorglade Avatar answered Nov 14 '22 23:11

Moorglade


After reading the issue in github, I think they will fix it in the next release. If you need to work around before that, you can try recursive timeout instead. Checkout my code:

function doSomething() {
    console.log('test');
}

function doSomethingInterval() {
   doSomething();
   setTimeout(doSomethingInterval, 1000);
}

doSomethingInterval();
like image 35
Dat Tran Avatar answered Nov 15 '22 00:11

Dat Tran