nodejs/node

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

Open

#27,679 创建于 2019年5月13日

在 GitHub 查看
 (6 评论) (0 反应) (0 负责人)JavaScript (117,218 star) (35,535 fork)batch import
clusterhelp wanted

描述

  • 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.

贡献者指南