Setthase
Setthase

Reputation: 14418

Error: IPC channel is already disconnected

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:

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)

Upvotes: 2

Views: 6447

Answers (3)

Timothy Zorn
Timothy Zorn

Reputation: 3229

Tested in NodeJS 7.10 and NodeJS 8.0

How to check:

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

ChildProcess which hasn't disconnected:

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

Upvotes: 1

julianwyz
julianwyz

Reputation: 3182

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

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

So, to use Kirill Zhirnov's example:

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.

Upvotes: 3

Kirill Zhirnov
Kirill Zhirnov

Reputation: 375

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

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

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

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

Upvotes: 2

Related Questions