错误:IPC通道已经断开连接

时间:2022-04-29 17:12:50

Where/when should I catch this error?

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

Error: IPC channel is already disconnected

This is called from child_process (using cluster) module when I call .disconect() multiple times. I know that I shouldn't call this twice (or more), but sometimes I don't have control over this. Eventually how to prevent calling this multiple times? This code doesn't works:

当我多次调用.disconect()()时,这将从child_process(使用群集)模块调用。我知道我不应该打两次电话(或更多),但有时我无法控制。最终如何避免多次调用?这段代码不工作:

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

EDIT:

编辑:

This is stack trace of this 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 个解决方案

#1


2  

Kind of an old post, but I ran into this issue in my project.

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

I found the the worker.suicide api is now deprecated (see: https://nodejs.org/api/cluster.html#cluster_worker_suicide).

我找到了那个工人。自杀api现在已被弃用(参见:https://nodejs.org/api/cluster.html#cluster_worker_suicide)。

It is now suggested to use worker.exitedAfterDisconnect (see: https://nodejs.org/api/cluster.html#cluster_worker_exitedafterdisconnect).

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

So, to use Kirill Zhirnov's example:

因此,用Kirill Zhirnov的例子:

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

would become:

将成为:

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

exitedAfterDisconnect will be undefined until either .kill() or .disconnect() is called on a worker, then it will be true.

exitedAfterDisconnect将不被定义,直到在worker上调用.kill()或.disconnect(),它将是true。

#2


1  

You should use worker.suicide to prevent calling .disconnect() multiple times:

您应该使用工人。避免多次调用.disconnect():

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

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

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

Set by calling .kill() or .disconnect(), until then it is undefined.

通过调用.kill()或.disconnect()设置,直到它未被定义。

#3


1  

Tested in NodeJS 7.10 and NodeJS 8.0

在NodeJS 7.10和NodeJS 8.0测试。

How to check:

如何检查:

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

ChildProcess which hasn't disconnected:

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 which has disconnected:

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] }

#4


0  

This error should be handled in worker file. For example using this code:

这个错误应该在工作文件中处理。例如使用此代码:

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

All tries for catching it from parent script was unsuccessful.
I have hope that this help someone deal with this problem.

所有试图从父脚本捕获它的尝试都失败了。我希望这能帮助人们解决这个问题。

#1


2  

Kind of an old post, but I ran into this issue in my project.

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

I found the the worker.suicide api is now deprecated (see: https://nodejs.org/api/cluster.html#cluster_worker_suicide).

我找到了那个工人。自杀api现在已被弃用(参见:https://nodejs.org/api/cluster.html#cluster_worker_suicide)。

It is now suggested to use worker.exitedAfterDisconnect (see: https://nodejs.org/api/cluster.html#cluster_worker_exitedafterdisconnect).

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

So, to use Kirill Zhirnov's example:

因此,用Kirill Zhirnov的例子:

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

would become:

将成为:

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

exitedAfterDisconnect will be undefined until either .kill() or .disconnect() is called on a worker, then it will be true.

exitedAfterDisconnect将不被定义,直到在worker上调用.kill()或.disconnect(),它将是true。

#2


1  

You should use worker.suicide to prevent calling .disconnect() multiple times:

您应该使用工人。避免多次调用.disconnect():

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

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

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

Set by calling .kill() or .disconnect(), until then it is undefined.

通过调用.kill()或.disconnect()设置,直到它未被定义。

#3


1  

Tested in NodeJS 7.10 and NodeJS 8.0

在NodeJS 7.10和NodeJS 8.0测试。

How to check:

如何检查:

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

ChildProcess which hasn't disconnected:

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 which has disconnected:

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] }

#4


0  

This error should be handled in worker file. For example using this code:

这个错误应该在工作文件中处理。例如使用此代码:

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

All tries for catching it from parent script was unsuccessful.
I have hope that this help someone deal with this problem.

所有试图从父脚本捕获它的尝试都失败了。我希望这能帮助人们解决这个问题。