2

我应该在哪里/什么时候发现这个错误?

Error: IPC channel is already disconnected

当我多次调用时,这是从child_process(使用cluster)模块调用的。.disconect()我知道我不应该两次(或多次)调用它,但有时我无法控制它。最终如何防止多次调用它?此代码不起作用:

try {
  if (worker.state !== "disconnected" && worker.state !== "dead") {
    worker.disconnect();
  }
} catch (error) {}

编辑:

这是此错误的堆栈跟踪:

events.js:71
    throw arguments[1]; // Unhandled 'error' event
                   ^
Error: IPC channel is already disconnected
    at process.target.disconnect (child_process.js:392:26)
    at ProgressTracker.callback (cluster.js:437:20)
    at ProgressTracker.check (cluster.js:94:32)
    at Object.Worker.disconnect [as 44:2] (cluster.js:445:16)
    at handleResponse (cluster.js:149:41)
    at respond (cluster.js:170:5)
    at handleMessage (cluster.js:180:5)
    at process.EventEmitter.emit (events.js:126:20)
    at handleMessage (child_process.js:269:12)
    at Pipe.channel.onread (child_process.js:293:9)
4

3 回答 3

3

有点老帖子,但我在我的项目中遇到了这个问题。

我发现worker.suicideapi 现在已弃用(请参阅:https ://nodejs.org/api/cluster.html#cluster_worker_suicide )。

现在建议使用worker.exitedAfterDisconnect(见:https ://nodejs.org/api/cluster.html#cluster_worker_exitedafterdisconnect )。

因此,以 Kirill Zhirnov 为例:

if (!cluster.worker.suicide) {
    cluster.worker.disconnect();
}

会成为:

if (!cluster.worker.exitedAfterDisconnect) {
    cluster.worker.disconnect();
}

exitedAfterDisconnect将是未定义的,直到在工作人员上调用.kill().disconnect()调用,然后它将是true.

于 2016-12-01T15:50:26.107 回答
2

您应该使用 worker.suicide 来防止多次调用 .disconnect() :

if (!cluster.worker.suicide) {
    cluster.worker.disconnect();
}

http://nodejs.org/api/cluster.html#cluster_worker_suicide

通过调用 .kill() 或 .disconnect() 设置,在此之前它是未定义的。

于 2015-01-09T16:40:53.247 回答
1

在 NodeJS 7.10 和 NodeJS 8.0 中测试

如何检查:

if (child.connected) {
    child.disconnect();
}

未断开连接的 ChildProcess:

ChildProcess {
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] },
  _events: { internalMessage: [Function] },
  _eventsCount: 1,
  _maxListeners: undefined,
  _closesNeeded: 2,
  _closesGot: 0,
  connected: true,
  signalCode: null,
  exitCode: null,
  killed: false,
  spawnfile: '/usr/bin/nodejs',
  _handle: Process { owner: [Circular], onexit: [Function], pid: 25424 },
  spawnargs: [ '/usr/bin/nodejs', './dist/child.js' ],
  pid: 25424,
  stdin: null,
  stdout: null,
  stderr: null,
  stdio: [ null, null, null, null ],
  channel:
   Pipe {
     bytesRead: 0,
     _externalStream: {},
     fd: 14,
     writeQueueSize: 0,
     buffering: false,
     onread: [Function],
     sockets: { got: {}, send: {} } },
  _channel: [Getter/Setter],
  _handleQueue: null,
  _pendingHandle: null,
  send: [Function],
  _send: [Function],
  disconnect: [Function],
  _disconnect: [Function] }

ChildProcess 已断开连接:

ChildProcess {
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] },
  _events: { internalMessage: [Function] },
  _eventsCount: 1,
  _maxListeners: undefined,
  _closesNeeded: 2,
  _closesGot: 1,
  connected: false,
  signalCode: null,
  exitCode: 0,
  killed: false,
  spawnfile: '/usr/bin/nodejs',
  _handle: null,
  spawnargs: [ '/usr/bin/nodejs', './dist/child.js' ],
  pid: 25424,
  stdin: null,
  stdout: null,
  stderr: null,
  stdio: [ null, null, null, null ],
  channel: null,
  _channel: [Getter/Setter],
  _handleQueue: null,
  _pendingHandle: null,
  send: [Function],
  _send: [Function],
  disconnect: [Function],
  _disconnect: [Function] }
于 2017-06-01T03:10:14.553 回答