Helidium / Mitol

Lightweight NodeJS http server
Other
175 stars 5 forks source link

Causes Segmentation Fault under heavy load #9

Open udivankin opened 7 years ago

udivankin commented 7 years ago

In most cases even 'hello world' server quits with segfault when trying to reach smth like 2k rps on linux. Output on mac differs a bit, returning malloc and/or 100% CPU.

Helidium commented 7 years ago

Can you send some more details about the error, the script you are running, bech tool you are running with etc.

udivankin commented 7 years ago

Ok, here's sample pingback server

const http = require('mitol');
const port = process.argv[2];

function respond(response, code, data) {
  response.statusCode = code;
  response.setHeader('Content-Type', 'application/json; charset=utf-8');
  response.setHeader('Connection', 'keep-alive');
  response.setHeader('Content-Length', data.length);
  response.end(data);
}

function requestHandler(request, response) {
  switch (request.method) {
    case 'GET':
      respond(response, 200, Buffer.from(request.url));
      break;
    case 'POST': {
      var chunks = [];

      request.on('data', chunk => { chunks.push(Buffer.from(chunk)); });

      request.on('end', () => {
        var requestString = Buffer.concat(chunks).toString('utf-8');
        respond(response, 200, Buffer.from(requestString));
      });

      break;
    }
    default:
      respond(response, 404, Buffer.from(request.method + ' method not supported'));
  }
};

const server = http.createServer(requestHandler);

server.listen(port, (err) => {
  if (err) {
    return console.log('Error creating http server', err)
  }

  console.log('Listening on port', port);
});

and here is a simple load server

const autocannon = require('autocannon');
const port = process.argv[2];

const instance = autocannon({
  url: 'http://localhost:' + port,
  connections: 10,
  pipelining: 1,
  duration: 10,
  overallRate: 2000,
  requests: [
    { path: '/khasekufhsakufhslkefhasekluhflkusaehflkuahsf' },
    { path: '/fasgeflkugsaeflkhasekufhsakufhslkefhas' },
    { path: '/asefasefasefaesfaesf' },
    { path: '/ausghfuaesgfaesgfasgeflkugsaeflkhasekufhsakufhslkefhasekluhflkusaehflkuahsfk/asefasefasefaesfaesf/easfaesfaesfeas/fesffth/hfgjgfyjfgyjfgyjgfyjgfyjdsgjbxnvdsefseaf' },
  ]
}, console.log);

autocannon.track(instance, { renderProgressBar: true });

Given 10-seconds load causes a CPU to jump to 100% after(!) load has finished, while a serie of 100sec runs with pauses in between causes segfault.