Page 1 of 1

[SOLVED] Long setTimeout in NodeJS on Raspberry PI

Posted: Wed Sep 14, 2016 12:36 pm
by philipBC
I am developing a NodeJS application on a Raspberry Pi 3, running Raspbian Jesse Lite.

I stumbled upon a problem with the use of the timer functions setTimeout and setInterval on this platform.

If you set an interval of more than an hour (roughly) then the timed event is never triggered ! After exhaustive testing and online research the problem is related to the Node thread going to sleep. It will wake on an I/O event, but (presumably a bug) the timing functions do not themselves wake up the thread. The code below is set to trigger every 4 hours, but will never trigger.

Code: Select all

     console.log(new Date(), 'Event triggered');
}, 14400000);   // Four hour interval
A way to confirm the problem is to wait until the period has expired, then use <CTRL>Z to push the process into the background and then use fg to return it to the console. The thread will wake up and then execute the timed event.. but late.

I have found two other references to this problem ... spberry-pi and
There is no fix yet, but a very simple workaround. Keep the thread alive by creating a simple setInterval event with a period under 1 hour. eg.

Code: Select all

// Dummy interval timer to keep this thread awake
},60000);   // One minute interval
Of course if you are lucky enough to already have other events happening regularly in your application which will keep it awake, then you will never see this problem.
Indeed, I only stumbled upon it when I extracted part of my code into a 'test' app where nothing else was going on.

Two days of my life I will never get back... hope you can all learn from my experience.

Re: Long setTimeout in NodeJS on Raspberry PI not working

Posted: Wed Sep 14, 2016 2:36 pm
by Heater
Ha Philip, that bug wasted a good few hours for me as well. Including isolating the problem and posting that issue you linked too. Forgot all about it.

Re: [SOLVED] Long setTimeout in NodeJS on Raspberry PI

Posted: Wed Sep 14, 2016 3:36 pm
by philipBC
I thought I was going nuts until I found your post.
Thanks for sharing, Heater.