NodeJS Cluster unexpected assert.AssertionError

China☆狼群 提交于 2019-12-06 00:36:44

问题


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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/21217285/nodejs-cluster-unexpected-assert-assertionerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!