Browse Source

Revert commits 12c8b27 and 88f416a, fixed properly in 2fe4558.

v0.7.4-release
Ben Noordhuis 14 years ago
parent
commit
30d20cf405
  1. 21
      doc/api/child_processes.markdown
  2. 18
      test/simple/test-child-process-kill.js

21
doc/api/child_processes.markdown

@ -234,24 +234,7 @@ be sent `'SIGTERM'`. See `signal(7)` for a list of available signals.
// send SIGHUP to process // send SIGHUP to process
grep.kill('SIGHUP'); grep.kill('SIGHUP');
Note that while the function is called `kill`, the signal delivered to the Note that while the function is called `kill`, the signal delivered to the child
child process may not actually kill it. `kill` really just sends a signal process may not actually kill it. `kill` really just sends a signal to a process.
to a process.
Please note that the example contains a potential race condition on
(at least) UNIX systems. Here is why:
The canonical approach to starting a child process is to call `fork()` to
create a copy of the current process, followed by a call to `execve()` to
replace the copy with the actual child process. This runs in tandem with
the parent process.
The time between `fork()` and `execve()` is short but it's not zero.
The child process may not have actually started when `spawn()` returns.
Thus, if you send a signal immediately after the call to `spawn()`, it may
end up being delivered to the copy of the current process and *not* the
actual child process.
The resulting behavior is undefined. It likely won't do what you want it to.
See `kill(2)` See `kill(2)`

18
test/simple/test-child-process-kill.js

@ -29,25 +29,11 @@ var termSignal;
var gotStdoutEOF = false; var gotStdoutEOF = false;
var gotStderrEOF = false; var gotStderrEOF = false;
var ping = "42\n";
var cat = spawn('cat'); var cat = spawn('cat');
//
// This test sends a signal to a child process.
//
// There is a potential race here where the signal is delivered
// after the fork() but before execve(). IOW, the signal is sent
// before the child process has truly been started.
//
// So we wait for a sign of life from the child (the ping response)
// before sending the signal.
//
cat.stdin.write(ping);
cat.stdout.addListener('data', function(chunk) { cat.stdout.addListener('data', function(chunk) {
assert.equal(chunk.toString(), ping); assert.ok(false);
cat.kill();
}); });
cat.stdout.addListener('end', function() { cat.stdout.addListener('end', function() {
@ -67,6 +53,8 @@ cat.addListener('exit', function(code, signal) {
termSignal = signal; termSignal = signal;
}); });
cat.kill();
process.addListener('exit', function() { process.addListener('exit', function() {
assert.strictEqual(exitCode, null); assert.strictEqual(exitCode, null);
assert.strictEqual(termSignal, 'SIGTERM'); assert.strictEqual(termSignal, 'SIGTERM');

Loading…
Cancel
Save