Node.js + libmysql-client + pingSync + setInterval = headache(true);

I've experienced a behaviour that makes me nuts and I can't solve it.

I have a script that opens a few mysql connections and stores them in an array. In order to prevent MySQL from closing unused connections (the process is supposed to be running 24/7) I use setInterval to fire pingSync() frequently. This approach has worked for me for many months in another project, but on a new host with node 0.8.14 the behaviour is weird.

setInterval(function () {
var count = 0;
console.log('---------------------------------------------------------');
console.log('Length: ');
console.log(connections.length);
connections.forEach(function(connection){
    var res = connection.pingSync();
    console.log('PING mysql '+count+ ' / '+(new Date().getTime()));
    console.log(res);
    count++;
});
console.log('---------------------------------------------------------');
}, 50000);

Expected result:

---------------------------------------------------------
Length:
4
PING mysql 0 / 1351603868929
true
PING mysql 1 / 1351603868929
true
PING mysql 2 / 1351603868929
true
PING mysql 3 / 1351603868929
true
---------------------------------------------------------

The results I got:

#1

---------------------------------------------------------
Length:
4
PING mysql 0 / 1351603868929
true
4
PING mysql 0 / 1351603868929
true
PING mysql 1 / 1351603868929
true
PING mysql 2 / 1351603868929
true
PING mysql 3 / 1351603868929
true
---------------------------------------------------------

#2

---------------------------------------------------------
Length:
4
PING mysql 0 / 1351604113400
4
PING mysql 0 / 1351604113400
PING mysql 1 / 1351604113400
PING mysql 2 / 1351604113400
PING mysql 3 / 1351604113400
---------------------------------------------------------
PING mysql 2 / 1351604113400
PING mysql 3 / 1351604113400
---------------------------------------------------------
---------------------------------------------------------

It looks like my function - or, parts of my function - get executed twice in random order.

Does anyone have an idea what could cause this behaviour? Or any advice how to track down the cause for this mess? Sometimes I get the expected result quoted above... but most of the times I get the mixed results.

EDIT: The setInterval() functions is only invoked once! (I've checked that dozens of times)

I have experienced it in the past that setInterval sometimes executes things in an unexpected manner. If the function runs longer than setInterval is set for, for example. A simple fix for that would be to use setTimeout instead and set it again at the end of your function.

var pingMysql = function () {
  var count = 0;

  console.log('---------------------------------------------------------');
  console.log('Length: ');
  console.log(connections.length);

  connections.forEach(function(connection){
    var res = connection.pingSync();

    console.log('PING mysql '+count+ ' / '+(new Date().getTime()));
    console.log(res);

    count++;
  });

  console.log('---------------------------------------------------------');
  setTimeout(pingMysql, 50000)
};

pingMysql();

Try doing that and see if it helps.