what happens to orphan processes while using cluster

353 views
Skip to first unread message

Tushar Jain

unread,
Aug 13, 2014, 2:08:34 AM8/13/14
to nod...@googlegroups.com
Hi, I am trying to figure out what actually happens when we re-execute the master process without killing the child processes of the previous run using cluster.
When i re-execute my code, it seems the previous workers also run again and sometimes master process is also executed twice.

I am sharing both input and output
  1. var cluster = require('cluster');
  2. var cpus = require('os').cpus().length;

  3. if(cluster.isMaster){
  4.   console.log('In Master');
  5.   for(var i = 0; i < 3 ; i++) {
  6.     var worker = cluster.fork();
  7.   }  
  8.   cluster.on('online',function(worker){
  9.     console.log("Worker Live:",worker.id);
  10.   });  
  11.   cluster.workers[1].send("Bazingaa");
  12.   //console.log(cluster.Workers.length);
  13. }
  14. if(cluster.isWorker){
  15.   console.log("In Worker",cluster.worker.id);
  16.   process.on('message',function(msg){
  17.     console.log("Message from server:", msg, cluster.worker.id);
  18.   });
  19.  // cluster.worker.kill();
  20. }
  Output:
  1. In Master
  2. Worker Live: 1
  3. Worker Live: 2
  4. Worker Live: 3
  5. In Worker 1
  6. Message from server: Bazingaa 1
  7. In Worker 2
  8. In Worker 3
  9. In Master
  10. Worker Live: 1
  11. Worker Live: 2
  12. Worker Live: 3
  13. In Worker 1
  14. In Worker 2
  15. Message from server: Bazingaa 1
  16. In Master
  17. In Worker 1
  18. Message from server: Bazingaa 1
  19. In Worker 2
  20. In Worker 3
please help me figure out whats happening behind the scenes.

Sam Roberts

unread,
Aug 13, 2014, 5:13:06 PM8/13/14
to nod...@googlegroups.com
On Tue, Aug 12, 2014 at 11:08 PM, Tushar Jain <mytu...@gmail.com> wrote:
> Hi, I am trying to figure out what actually happens when we re-execute the
> master process without killing the child processes of the previous run using
> cluster.
> When i re-execute my code, it seems the previous workers also run again and
> sometimes master process is also executed twice.

Impossible to know without you defining "rexecute".

do you mean:

node example.js &
node example.js &

?

do you mean

node example.js
(ctrl-c node)
node example.js

?

something else?

Tushar Jain

unread,
Aug 13, 2014, 11:53:02 PM8/13/14
to nod...@googlegroups.com
sorry for the ambiguity.. 
I mean  node example.js 
            (ctrl-c node) 
            node example.js

Jimb Esser

unread,
Aug 14, 2014, 2:17:26 PM8/14/14
to nod...@googlegroups.com
Depending on your platform this behavior may be different.  I believe on Windows ctrl+c sends a signal to all processes bound to the console, so might kill all of the children (depending on how cluster.fork() is implemented the child processes may or may not be detached).  A kill signal sent from task manager or taskkill.exe just kills the process you request.  I'm not on Windows at the moment to test, however, so I might be misremembering some details.  From your log it looks like you killed all processes and restarted twice, nothing unexpected.

Sam Roberts

unread,
Aug 14, 2014, 11:30:43 PM8/14/14
to nod...@googlegroups.com
On Wed, Aug 13, 2014 at 8:53 PM, Tushar Jain <mytu...@gmail.com> wrote:
>> sorry for the ambiguity..
>
> I mean node example.js
> (ctrl-c node)

master is killed (assuming default SIGINT behaviour)
ipc connections to all workers closed by OS
all workers notice IPC connection closed
all workers exit

> node example.js

master starts
worker starts

Jimb Esser

unread,
Aug 15, 2014, 12:35:15 PM8/15/14
to nod...@googlegroups.com
Hmm, on my OSX machine, it seems the behavior is roughly the same as I recall from Windows - hitting Ctrl+C delivers SIGINT to all of the processes, not just the master, so the cluster children are exiting immediately because of that.  If you kill just the master, then the children presumably exit because they notice the IPC connection being closed.  If you want to change that behavior, catch the appropriate signals and disconnect your children appropriately.  Here's a modified version of your code using the logic we use for hot-restarting our servers (master exits immediately, to be replaced with a new one, children hang around until they're done servicing all of their requests, children ignore SIGINT in this case).

var cluster = require('cluster');


if(cluster.isMaster){
  console
.log('In Master');
 
for(var i = 0; i < 3 ; i++) {

    cluster
.fork();
 
}
  cluster
.on('online',function(worker){
    console
.log('Worker Live:',worker.id);
 
});
  cluster
.workers[1].send('Bazingaa');

  process
.on('SIGINT', function () {
   
for (var id in cluster.workers) {
      cluster
.workers[id].send('mySIGINT');
   
}
    cluster
.disconnect();
    process
.stdout.write('Received SIGINT, forwarded to workers', function () {
      process
.exit(0);
   
});
 
});
} else {
  console
.log('In Worker',cluster.worker.id);
 
var interval = setInterval(function () {
    console
.log('worker ' + cluster.worker.id + ' alive');
 
}, 1000);
  process
.on('message',function(msg){
   
if (msg === 'mySIGINT') {
      console
.log('Master wants me to exit');
      setTimeout
(function () {
       
// will cause process to exit, no outstanding operations
        clearInterval
(interval);
     
}, 2500);
   
} else {

      console
.log('Message from server:', msg, cluster.worker.id);
   
}
 
});

  process
.on('exit', function () {
    console
.log('worker ' + cluster.worker.id + ' exiting');
 
});
  process
.on('SIGINT', function () {
    console
.log('Received SIGINT as child, ignoring');
 
});
}

Sam Roberts

unread,
Aug 15, 2014, 8:07:03 PM8/15/14
to nod...@googlegroups.com
On Fri, Aug 15, 2014 at 9:35 AM, Jimb Esser <wast...@gmail.com> wrote:
> Hmm, on my OSX machine, it seems the behavior is roughly the same as I
> recall from Windows - hitting Ctrl+C delivers SIGINT to all of the
> processes, not just the master, so the cluster children are exiting
> immediately because of that.

Sounds like the shell is delivering SIGINT to the forground process
group. If you `kill -INT` the master process from another terminal,
the behaviour will be different from ctrl-C in terminal.

Tushar Jain

unread,
Aug 19, 2014, 4:46:45 AM8/19/14
to nod...@googlegroups.com
Thanks Jimb, your code definitely gonna help me understand the concept. 
Reply all
Reply to author
Forward
0 new messages