philipBC
Posts: 14
Joined: Fri Aug 12, 2016 1:15 pm

[SOLVED] Long setTimeout in NodeJS on Raspberry PI

Wed Sep 14, 2016 12:36 pm

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

setTimeout(function(){
     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
http://stackoverflow.com/questions/3715 ... spberry-pi and
https://github.com/nodejs/node/issues/4262
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
setInterval(function(){
},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.
Last edited by philipBC on Wed Sep 14, 2016 3:34 pm, edited 1 time in total.

Heater
Posts: 16007
Joined: Tue Jul 17, 2012 3:02 pm

Re: Long setTimeout in NodeJS on Raspberry PI not working

Wed Sep 14, 2016 2:36 pm

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.
Memory in C++ is a leaky abstraction .

philipBC
Posts: 14
Joined: Fri Aug 12, 2016 1:15 pm

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

Wed Sep 14, 2016 3:36 pm

I thought I was going nuts until I found your post.
Thanks for sharing, Heater.

Return to “Other programming languages”