Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

process still alive, when fork channel closed #2344

@AndreasMadsen

Description

@AndreasMadsen

I know this use internal API, but the cluster 2.0 module ( see: #2038 ) use this.

Here is a simpel master. It will fork a new "worker" just using child_process and when it gets a message from the worker it will disconnect it. The master should now die since there can't happen anything more, but it dose not!

var child = require('child_process');
var util = require('util');

var worker = child.fork('worker.js');

worker.once('message', function () {
  console.log('now disconnecing');

  worker._channel.close();

  //Lets se the content
  console.log(util.inspect(worker._channel));

  //try sending a message to the worker
  try {
    worker.send('say hi');
  } catch (e) {
    console.log('got an error, because the channel is closed :');
    console.log(e);
  }
});

And this is the worker. It will create a simpel http server just to keep it alive.

var http = require('http');
var util = require('util');

//Keep the worker alive
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('PID: ' + process.pid + "\n");

  //Lets see process._channel
  res.write(util.inspect(process._channel));

  res.end();

}).listen(8000, "127.0.0.1", function () {
  process.send('hey master, please disconnect me');
});

Also when the master disconnect its worker the worker._channel is not null and the worker._channel.destroyed is false. However in in the worker the worker._channel property is null.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions