Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node 4.1.2 on OS X: child_process emits exit/close to wrong instance #3236

Closed
minesworld opened this issue Oct 7, 2015 · 4 comments
Closed

Comments

@minesworld
Copy link

node --version => v4.1.2
uname -a => Darwin xxx 15.0.0 Darwin Kernel Version 15.0.0: Wed Aug 26 16:57:32 PDT 2015; root:xnu-3247.1.106~1/RELEASE_X86_64 x86_64

only the last spawned child_process instance will get the emitted 'exit' and 'close' events, but this also for OTHER instances too...

100% reproducable with this source:

var child_process = require('child_process');

ChildProcess = function(description, executable, arguments, callback) {
  this.description = description;
  this.exited = false;

  this.process = child_process.spawn(executable, arguments);

  self = this;

  this.process.on('exit', function(code, signal) {

    self.exited = true;
    console.log(self.description + " process.on:exit", code, signal);

  }).on('close', function(code, signal) {

    self.exited = true;
    console.log(self.description + " process.on:close", code, signal);

    if (callback) callback(code, signal);

  }).on('error', function(err) {

    console.log(self.description + " process.on:error", err);

  });
}

ChildProcess.prototype.kill = function(signal) {
  if (this.exited) return;

  console.log(this.description + " .kill", signal);

  this.process.kill(signal);
}


p1 = new ChildProcess("p1", "/bin/sleep", [ "11" ], function(code, signal) {
  console.log("p1 <= ", code, signal);
});

p2 = new ChildProcess("p2", "/bin/sleep", [ "12" ], function(code, signal) {
  console.log("p2 <= ", code, signal);
});

setTimeout(function() {
  p2.kill("SIGTERM");
  p1.kill("SIGTERM");
}, 1000);

Output is

p2 .kill SIGTERM
p1 .kill SIGTERM
p2 process.on:exit null SIGTERM
p2 process.on:exit null SIGTERM
p2 process.on:close null SIGTERM
p2 <=  null SIGTERM
p2 process.on:close null SIGTERM
p1 <=  null SIGTERM
@evanlucas
Copy link
Contributor

Hm, I cannot reproduce. I have this:

'use strict'

const spawn = require('child_process').spawn

function ChildProcess(desc, exe, args, cb) {
  this.desc = desc
  this.exited = false
  var self = this

  this.process = spawn(exe, args)
    .on('exit', function(code, signal) {
      self.exited = true
      console.log(self.desc, 'exit', code, signal)
    })
    .on('close', function(code, signal) {
      self.exited = true
      console.log(self.desc, 'close', code, signal)
      if (cb) cb(code, signal)
    })
    .on('error', function(err) {
      console.log(self.desc, 'error', err)
    })
}

ChildProcess.prototype.kill = function(signal) {
  if (this.exited)
    return

  console.log(this.desc, 'kill', signal)
  this.process.kill(signal)
}

var p1 = new ChildProcess('p1', '/bin/sleep', ['11'], function(code, signal) {
  console.log('p1 <=', code, signal)
})

var p2 = new ChildProcess('p2', '/bin/sleep', ['12'], function(code, signal) {
  console.log('p2 <=', code, signal)
})

setTimeout(function() {
  p2.kill('SIGTERM')
  p1.kill('SIGTERM')
}, 1000)

It looks like you have some global variables in that code, which could be causing an issue?

@evanlucas
Copy link
Contributor

With the above, I get the following output:

p2 kill SIGTERM
p1 kill SIGTERM
p1 exit null SIGTERM
p2 exit null SIGTERM
p1 close null SIGTERM
p1 <= null SIGTERM
p2 close null SIGTERM
p2 <= null SIGTERM

@minesworld
Copy link
Author

My fault. ( var self = this ) ...

@minesworld
Copy link
Author

You’re right.

Thanks a thousand times for the ‘use strict’ info…

Shame about me…

Michael

On 07 Oct 2015, at 15:34, Evan Lucas notifications@github.com wrote:

With the above, I get the following output:

p2 kill SIGTERM
p1 kill SIGTERM
p1 exit null SIGTERM
p2 exit null SIGTERM
p1 close null SIGTERM
p1 <= null SIGTERM
p2 close null SIGTERM
p2 <= null SIGTERM


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants