错误:IPC通道已断开连接

时间:2013-08-08 13:13:40

标签: node.js

我应该在何时/何时发现此错误?

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 个答案:

答案 0 :(得分:2)

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

我发现worker.suicide api现已弃用(请参阅: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

答案 1 :(得分:1)

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

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

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

  

通过调用.kill()或.disconnect()进行设置,直到那时它是未定义的。

答案 2 :(得分: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] }

答案 3 :(得分:0)

此错误应在worker文件中处理。例如,使用此代码:

process.on("uncaughtException", function (error) {
  if (error.toString() !== 'Error: IPC channel is already disconnected') {
    process.stderr.write(error.stack);
    process.exit(1);
  }
}); 

所有尝试从父脚本中捕获它都是不成功的 我希望这有助于某人处理这个问题。