how to debug node.js causing 100% cpu usage?
You can profile your app with node-tick.
- Install
node-tick
bysudo npm -g install tick
- Run your app with enabled profile
node --prof ./app.js
- After some time with CPU 100% usage stop your app
- You can see v8.log in your app directory, now you can read it with node-tick-processor
- Run
node-tick-processor
and explain results - Load v8.log into chrome://tracing to analyse as tree.
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.
If you are using UI app with webpack, pay attention on watchOptions
or watch
.For me, disabling poll solve the problem
watchOptions: { poll: false }
Or you can set a time, when poll will be triggered like poll: 3000
(once in 3sec)https://webpack.js.org/configuration/watch/#watchoptionsignored