nodejs/node

Calling disconnect causes processes spawned by cluster module to exit too early

Open

#27,679 opened on May 13, 2019

View on GitHub
 (6 comments) (0 reactions) (0 assignees)JavaScript (117,218 stars) (35,535 forks)batch import
clusterhelp wanted

Description

  • Version: 12.2.0 but could also reproduce on 10.15.3
  • Platform: Darwin 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64
  • Subsystem:

Consider the following script:

const cluster = require('cluster');
const childProcess = require('child_process');

const useCluster = false;
const isMaster = useCluster ? cluster.isMaster : !process.argv.includes('worker');

if (isMaster) {
  if (useCluster) {
    cluster.fork(__filename);
  } else {
    childProcess.fork(__filename, ['worker']);
  }
} else {
  setTimeout(() => console.log('hi from worker'), 1000);
  process.disconnect();
}

When useCluster is true node exits immediately and nothing is printed to the console. When useCluster is false you see "hi from worker" logged after 1s and then node will exit which is the expected behavior. The documentation makes it sound like calling process.disconnect should only close the IPC channel between the master and worker process and should not cause either to exit early if there is still work to do.

Contributor guide