Skip to content

Commit

Permalink
child_process: support Symbol.dispose
Browse files Browse the repository at this point in the history
PR-URL: #48551
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
MoLow authored and juanarbol committed Jul 13, 2023
1 parent e2d0195 commit 4e08160
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,16 @@ setTimeout(() => {
}, 2000);
```

### `subprocess[Symbol.dispose]()`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
Calls [`subprocess.kill()`][] with `'SIGTERM'`.

### `subprocess.killed`

<!-- YAML
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 @@ -12,6 +12,7 @@ const {
ReflectApply,
StringPrototypeSlice,
Symbol,
SymbolDispose,
Uint8Array,
} = primordials;

Expand Down Expand Up @@ -516,6 +517,12 @@ ChildProcess.prototype.kill = function(sig) {
return false;
};

ChildProcess.prototype[SymbolDispose] = function() {
if (!this.killed) {
this.kill();
}
};


ChildProcess.prototype.ref = function() {
if (this._handle) this._handle.ref();
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-child-process-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const spawn = require('child_process').spawn;
const cat = spawn(common.isWindows ? 'cmd' : 'cat');

cat.stdout.on('end', common.mustCall());
cat.stderr.on('data', common.mustNotCall());
cat.stderr.on('end', common.mustCall());

cat.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(code, null);
assert.strictEqual(signal, 'SIGTERM');
assert.strictEqual(cat.signalCode, 'SIGTERM');
}));
cat.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(code, null);
assert.strictEqual(signal, 'SIGTERM');
assert.strictEqual(cat.signalCode, 'SIGTERM');
}));

assert.strictEqual(cat.signalCode, null);
assert.strictEqual(cat.killed, false);
cat[Symbol.dispose]();
assert.strictEqual(cat.killed, true);

0 comments on commit 4e08160

Please sign in to comment.