NodeJS Cluster unexpected assert.AssertionError
Asked Answered
P

2

6

I am facing an weird error, this is my main .js file

var cluster = require('cluster'),
  express = require('express'),
  http = require('http');

if (cluster.isMaster) {
    var cpuCount = require('os').cpus().length;
    for (var i = 0; i < cpuCount; i += 1) {
      cluster.fork();
    }
} else {
  var app = express(),
  server = http.createServer(app),
  io = require('socket.io').listen(server);
  io.set('log level', 2);
  server.listen(3000);
}

cluster.on('exit', function (worker) {
    console.log('Worker ' + worker.id + ' died :(');
    cluster.fork();
});

This is the error message i am getting..

Worker 1 died :(
Worker 2 died :(

assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: false == true
    at Cluster.cluster.fork (cluster.js:500:3)
    at Cluster.<anonymous> (/xxx/x/xxx/xxx/xxxxx.js:21:13)
    at Cluster.EventEmitter.emit (events.js:106:17)
    at process.<anonymous> (cluster.js:341:13)
    at process.g (events.js:180:16)
    at process.EventEmitter.emit (events.js:95:17)
    at process.exit (node.js:707:17)
    at process.<anonymous> (cluster.js:545:15)
    at process.g (events.js:180:16)
    at process.EventEmitter.emit (events.js:117:20)

assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: false == true
    at Cluster.cluster.fork (cluster.js:500:3)
    at Cluster.<anonymous> (/xxx/x/xxx/xxx/xxxxx.js:21:13)
    at Cluster.EventEmitter.emit (events.js:106:17)
    at process.<anonymous> (cluster.js:341:13)
    at process.g (events.js:180:16)
    at process.EventEmitter.emit (events.js:95:17)
    at process.exit (node.js:707:17)
    at process.<anonymous> (cluster.js:545:15)
    at process.g (events.js:180:16)
    at process.EventEmitter.emit (events.js:117:20)

No idea whats wrong there any help ?

Hardware Overview:

  Model Name:   MacBook
  Model Identifier: MacBook5,2
  Processor Name:   Intel Core 2 Duo
  Processor Speed:  2.13 GHz
  Number of Processors: 1
  Total Number of Cores:    2
  L2 Cache: 3 MB
  Memory:   2 GB
  Bus Speed:    1.07 GHz
  Boot ROM Version: MB52.0088.B05
  SMC Version (system): 1.38f5
  Sudden Motion Sensor:
  State:    Enabled

System Version: OS X 10.9.1 (13B42)

Kernel Version: Darwin 13.0.0

$ uname -a
Darwin Nikhils-MacBook.local 13.0.0 Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64
$ node --version
v0.10.24
$ npm --version
1.3.21
Prouty answered 19/1, 2014 at 13:22 Comment(2)
Just tried it and everything worked out fine. I'm on windows on node.js version 0.10.24Kistler
What environment (OS etc.) are you running in?Brockie
B
4

cluster.js line 500 is

assert(cluster.isMaster);

i.e. you're calling fork from another worker (or Node thinks you are).

If moving the cluster.on('exit' listener into the if (cluster.isWorker) block does not resolve the issue, then I think you should open an issue on Github, as I can't see why the event would be emitted in any workers.

Edit: It was indeed a bug.

Brockie answered 20/1, 2014 at 12:12 Comment(4)
will do is that the only way to get this resolved ? use cluster ? github.com/joyent/node/issues/6915Prouty
If you tried my suggestion first, then you didn't update your code for the github issue.Brockie
what are you suggestiong ? Can i get a more detailed answer ?Prouty
Move the listener into the if block.Brockie
B
0

Had same problem - this is now working:

cluster.on('disconnect', function(worker) {
  if (this.isMaster) {
    console.log('worker ' + worker.process.pid + ' disconnected');
    cluster.fork();
  }
});

as referenced by @OrangeDog.

Beanery answered 7/4, 2014 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.