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

child_process: add ChildProcess 'spawn' event #35369

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,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 @@ -400,6 +400,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 @@ -465,6 +467,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());