Skip to content

Commit 4e08160

Browse files
MoLowjuanarbol
authored andcommitted
child_process: support Symbol.dispose
PR-URL: #48551 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent e2d0195 commit 4e08160

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

doc/api/child_process.md

+10
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,16 @@ setTimeout(() => {
14021402
}, 2000);
14031403
```
14041404

1405+
### `subprocess[Symbol.dispose]()`
1406+
1407+
<!-- YAML
1408+
added: REPLACEME
1409+
-->
1410+
1411+
> Stability: 1 - Experimental
1412+
1413+
Calls [`subprocess.kill()`][] with `'SIGTERM'`.
1414+
14051415
### `subprocess.killed`
14061416

14071417
<!-- YAML

lib/internal/child_process.js

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
ReflectApply,
1313
StringPrototypeSlice,
1414
Symbol,
15+
SymbolDispose,
1516
Uint8Array,
1617
} = primordials;
1718

@@ -516,6 +517,12 @@ ChildProcess.prototype.kill = function(sig) {
516517
return false;
517518
};
518519

520+
ChildProcess.prototype[SymbolDispose] = function() {
521+
if (!this.killed) {
522+
this.kill();
523+
}
524+
};
525+
519526

520527
ChildProcess.prototype.ref = function() {
521528
if (this._handle) this._handle.ref();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const spawn = require('child_process').spawn;
5+
const cat = spawn(common.isWindows ? 'cmd' : 'cat');
6+
7+
cat.stdout.on('end', common.mustCall());
8+
cat.stderr.on('data', common.mustNotCall());
9+
cat.stderr.on('end', common.mustCall());
10+
11+
cat.on('exit', common.mustCall((code, signal) => {
12+
assert.strictEqual(code, null);
13+
assert.strictEqual(signal, 'SIGTERM');
14+
assert.strictEqual(cat.signalCode, 'SIGTERM');
15+
}));
16+
cat.on('exit', common.mustCall((code, signal) => {
17+
assert.strictEqual(code, null);
18+
assert.strictEqual(signal, 'SIGTERM');
19+
assert.strictEqual(cat.signalCode, 'SIGTERM');
20+
}));
21+
22+
assert.strictEqual(cat.signalCode, null);
23+
assert.strictEqual(cat.killed, false);
24+
cat[Symbol.dispose]();
25+
assert.strictEqual(cat.killed, true);

0 commit comments

Comments
 (0)