davidmarkclements / 0x

🔥 single-command flamegraph profiling 🔥
MIT License
3.23k stars 106 forks source link

Crashing with clustering (RangeError: Invalid string length) #63

Closed vkupar closed 7 years ago

vkupar commented 7 years ago

My environment: OS: Linux Node: v6.9.1 Already installed perf

error: Caught SIGINT, generating flamegraph /home/valeri/.nvm/versions/node/v6.9.1/lib/node_modules/0x/index.js:384 fs.writeFileSync(folder + '/stacks.' + pid + '.json', JSON.stringify(json, 0, 2)) ^

RangeError: Invalid string length at join (native) at Object.stringify (native) at write (/home/valeri/.nvm/versions/node/v6.9.1/lib/node_modules/0x/index.js:384:66) at /home/valeri/.nvm/versions/node/v6.9.1/lib/node_modules/0x/index.js:366:7 at ConcatStream. (/home/valeri/.nvm/versions/node/v6.9.1/lib/node_modules/0x/node_modules/concat-stream/index.js:36:43) at emitNone (events.js:91:20) at ConcatStream.emit (events.js:185:7) at finishMaybe (/home/valeri/.nvm/versions/node/v6.9.1/lib/node_modules/0x/node_modules/readable-stream/lib/_stream_writable.js:513:14) at endWritable (/home/valeri/.nvm/versions/node/v6.9.1/lib/node_modules/0x/node_modules/readable-stream/lib/_stream_writable.js:523:3) at ConcatStream.Writable.end (/home/valeri/.nvm/versions/node/v6.9.1/lib/node_modules/0x/node_modules/readable-stream/lib/_stream_writable.js:493:41)

davidmarkclements commented 7 years ago

hey @vatex - interesting error - it may be that json is just super large, and it's causing JSON.stringify to choke... - can you go to that line and try to console.log json (and also json.length)

vkupar commented 7 years ago

@davidmarkclements json.length: undefined json: { name: '', value: 6766, top: 0, children: [ { name: '1000000 cpu-clock', value: 6766, top: 1, children: [Object] } ] }

vkupar commented 7 years ago

@davidmarkclements It was problem of clusters. I disabled clusters in my application and now 0x works

davidmarkclements commented 7 years ago

ohhhh ... interesting

for posterity would you mind adding a little snippet here to demonstrate the problem - naturally 0x is single process only, but if we can catch the condition and provide an informative error that'd be nice

vkupar commented 7 years ago
'use strict';

const express = require('express');
const path = require('path');
const compression = require('compression');
const helmet = require('helmet');
const fs = require('fs');
const bodyParser = require('body-parser');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    for (var i = 0; i < numCPUs; i++) {
        // Create a worker
        cluster.fork();
    }
} else {

    const app = express();
    const environment = process.env.NODE_ENV;
    app.use(compression());
    app.use(helmet.frameguard({ action: 'sameorigin' }));
    app.disable('x-powered-by');
    app.enable('trust proxy');

    app.use(bodyParser.json({
        limit: '50mb'
    }));

    app.use(bodyParser.urlencoded({
        extended: true
    }));

    app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        next();
    });

    // Host static files
    app.use(express.static(path.join(__dirname, 'public')));

    app.listen(process.env.PORT || 3001);
}