Skip to content

Commit

Permalink
child_process: add hasRef to ChildProcess
Browse files Browse the repository at this point in the history
Refs: nodejs#42091

The issue above reported a specific use case about `Timer.hasRef()`.
After reading the issue, I started thinking users may naturally expect
`hasRef` method if `ref()` and `unref()` exist on an object they use.
As of now, however, `hasRef()` exists on `Timer` only.

This commit suggests adding `hasRef` method to `ChildProcess` first.
There are more similar cases. (fs.FSWatcher, fs.StatWatcher, net.Socket,
and so on)
  • Loading branch information
daeyeon committed Apr 14, 2022
1 parent e61b62b commit bd43be7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
26 changes: 26 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,32 @@ console.log(`Spawned child pid: ${grep.pid}`);
grep.stdin.end();
```

### `subprocess.hasRef()`

<!-- YAML
added: REPLACEME
-->

* Returns: {boolean}

Indicates whether a `subprocess` is "ref'ed" by the parent process's event
loop. If `true`, the parent waits for the child to exit before exiting itself.

```js
const { spawn } = require('child_process');

const subprocess = spawn(process.argv[0], ['child_program.js'], {
detached: true,
stdio: 'ignore',
});

subprocess.on('exit', (code) => {
subprocess.hasRef(); // false
});

subprocess.hasRef(); // true
```

### `subprocess.ref()`

<!-- YAML
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,11 @@ ChildProcess.prototype.unref = function() {
if (this._handle) this._handle.unref();
};


ChildProcess.prototype.hasRef = function() {
return this._handle ? this._handle.hasRef() : false;
};

class Control extends EventEmitter {
#channel = null;
#refs = 0;
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-child-process-hasref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { spawn, ChildProcess } = require('child_process');

const command = common.isWindows ? 'dir' : 'ls';
const child = spawn(command);

assert.strictEqual(child.hasRef(), true);

child.unref();
assert.strictEqual(child.hasRef(), false);

child.ref();
assert.strictEqual(child.hasRef(), true);

function mustHasRef() {
return common.mustCall(() => assert.strictEqual(child.hasRef(), true));
}

function mustHasNoRef() {
return common.mustCall(() => assert.strictEqual(child.hasRef(), false));
}

child.stdout.on('data', mustHasRef());
child.stdout.on('end', mustHasRef());
child.stdout.on('close', mustHasNoRef());
child.on('exit', mustHasNoRef());
child.on('close', mustHasNoRef());

0 comments on commit bd43be7

Please sign in to comment.