I have a node app that uses express and redis. On our development server, after a bit of use node starts to use 100% cpu. The application still responds but top reports node using 100%. The cpu doesn't drop until node is restarted.
I have not nailed it down to any particular route or function that is causing it.
What is the best way to diagnose this problem?
I looked at node-inspector with the v8-profiler and it gave me the same error that is reported here https://github.com/dannycoates/v8-profiler/issues/10
I found the problem by writing a script to record every request and then replay them.
The problem was caused because I had a callback that was not being returned.
myAsncFunc(function(err, data) {
if (err) { callback(err) }
//node kept going after the error was returned to the user.
// make sure you, return callback(err)
})
Here was my replay.js code for anyone interested.
var request = require('request');
var async = require('async');
var redis = require('redis');
var host = 'http://myhost.com';
var jobs = true;
var client = redis.createClient();
async.whilst(
function () { return jobs; },
function (callback) {
client.lpop('history', function(err, url) {
console.log(url);
if (!url) {
jobs = false;
callback();
}
request.get({url:host+url}, function() {
callback();
});
})
},
function (err) {
console.log('done')
}
);
And in you're express app.
app.get('/*', function(req, res, next) {
var url = req.originalUrl;
redis.rpush('history', url);
next();
});
It's cool because every history item that is played will be added again to the queue so it continually loops and every time you visit a new page, it will add that one to the queue.
maybe you have some computation somewhere using nextTick
that is trashing CPU constantly.
If you can't run profile then its hard to find out which method is trashing cpu. One more thing is to examine express log by using logger middleware http://senchalabs.github.com/connect/middleware-logger.html
You can profile your app with node-tick.
node-tick
by sudo npm -g install tick
node --prof ./app.js
node-tick-processor
and explain resultsI experienced also 100% CPU usage till i switched off supervisor mode (causing node to restart, when a file changes).
This probably does not answer this question, but in case some novice like me worries about CPU usage, this could be the case.