Skip to content

Commit

Permalink
child_process: add ChildProcess 'spawn' event
Browse files Browse the repository at this point in the history
The new event signals that the subprocess has spawned successfully and
no 'error' event will be emitted from failing to spawn.

Fixes: #35288
PR-URL: #35369
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
  • Loading branch information
zenflow authored and targos committed Mar 3, 2021
1 parent 772afde commit a2a3b70
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,21 @@ child process, the `message` argument can contain data that JSON is not able
to represent.
See [Advanced serialization][] for more details.

### Event: `'spawn'`
<!-- YAML
added: REPLACEME
-->

The `'spawn'` event is emitted once the child process has spawned successfully.

If emitted, the `'spawn'` event comes before all other events and before any
data is received via `stdout` or `stderr`.

The `'spawn'` event will fire regardless of whether an error occurs **within**
the spawned process. For example, if `bash some-command` spawns successfully,
the `'spawn'` event will fire, though `bash` may fail to spawn `some-command`.
This caveat also applies when using `{ shell: true }`.

### `subprocess.channel`
<!-- YAML
added: v7.1.0
Expand Down
7 changes: 7 additions & 0 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ ChildProcess.prototype.spawn = function(options) {
this._handle.close();
this._handle = null;
throw errnoException(err, 'spawn');
} else {
process.nextTick(onSpawnNT, this);
}

this.pid = this._handle.pid;
Expand Down Expand Up @@ -466,6 +468,11 @@ function onErrorNT(self, err) {
}


function onSpawnNT(self) {
self.emit('spawn');
}


ChildProcess.prototype.kill = function(sig) {

const signal = sig === 0 ? sig :
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-child-process-spawn-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ assert.strictEqual(enoentChild.stdio[0], enoentChild.stdin);
assert.strictEqual(enoentChild.stdio[1], enoentChild.stdout);
assert.strictEqual(enoentChild.stdio[2], enoentChild.stderr);

enoentChild.on('spawn', common.mustNotCall());

enoentChild.on('error', common.mustCall(function(err) {
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(getSystemErrorName(err.errno), 'ENOENT');
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-child-process-spawn-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common');
const spawn = require('child_process').spawn;
const assert = require('assert');

const subprocess = spawn('echo', ['ok']);

let didSpawn = false;
subprocess.on('spawn', function() {
didSpawn = true;
});
function mustCallAfterSpawn() {
return common.mustCall(function() {
assert.ok(didSpawn);
});
}

subprocess.on('error', common.mustNotCall());
subprocess.on('spawn', common.mustCall());
subprocess.stdout.on('data', mustCallAfterSpawn());
subprocess.stdout.on('end', mustCallAfterSpawn());
subprocess.stdout.on('close', mustCallAfterSpawn());
subprocess.stderr.on('data', common.mustNotCall());
subprocess.stderr.on('end', mustCallAfterSpawn());
subprocess.stderr.on('close', mustCallAfterSpawn());
subprocess.on('exit', mustCallAfterSpawn());
subprocess.on('close', mustCallAfterSpawn());

0 comments on commit a2a3b70

Please sign in to comment.