How to stop the process in node.js?

Good day to all.

In the master process node that launches several child. Each child is a parser that receives data from the master. Everything works fine, but I noticed one peculiarity - when the parsing is finished, the child process is not closed. Day struggled with the problem and found out that node is closed if in the stack there are incomplete handlers. He began to dig, what I have hanging on the stack so far. And found that this is a handler for incoming messages from the master process:
if (cluster.isMaster) {

 for (var i = 0; i < numCPUs; i++) {
 let worker = cluster.fork();
worker.send(i);
}

 cluster.on('exit', (worker, code, signal) => {
 console.log(`Worker ${worker.process.pid} has finished his job.`);

--numberOfProcesses;
 if(numberOfProcesses <= 0) {
 console.log(`Parsing was completed.`);
process.exit(0);
}
});


} else {
 process.once('message', msg => {
 // it is here because of this, the handler process continues to run
 console.log(process.pid, msg)
})
}


Even when you specify "once", that is, to get the message and reset the processor. Still does not finish the work.
And now the question is, why it's not closed and how to kill child process?
p.s.: do not offer process.exit(), because the process must wait for completion of all tasks.
July 2nd 19 at 13:22
2 answers
July 2nd 19 at 13:24
Solution
Have to offer process.exit()
The hung process handlers, so he is not going to fail myself.
Cause after all tasks are completed - Promise to help.
On the other is unlikely.
Yes, of course, and through promises and stuff, that's just in the process of running over9000 tasks and other asynchronous operations, which with each other are not connected(the load file data record in the database, etc.). Track their perfection is extremely problematic. Therefore looking for some other way, do not climb into the depths of logic. - Miguel.Kautzer89 commented on July 2nd 19 at 13:27
You are right, nothing to do, will have to calculate when the process will end. I even tried to remove orphaned handlers confused the devil-still continues to hang. - Miguel.Kautzer89 commented on July 2nd 19 at 13:30
July 2nd 19 at 13:26
And child_process.exec you than not podhodit?

Find more questions by tags Node.js