From fb8365b1697508021501c933ce10876e33748c99 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Thu, 14 Apr 2022 10:13:24 +0900 Subject: [PATCH 1/6] child_process: add hasRef to ChildProcess Refs: https://github.com/nodejs/node/issues/42091 The issue above reported a specific use case about `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) --- doc/api/child_process.md | 26 +++++++++++++++++++ lib/internal/child_process.js | 5 ++++ test/parallel/test-child-process-hasref.js | 30 ++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 test/parallel/test-child-process-hasref.js diff --git a/doc/api/child_process.md b/doc/api/child_process.md index ad712eb8786f59..e30576aea75ee3 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -1434,6 +1434,32 @@ console.log(`Spawned child pid: ${grep.pid}`); grep.stdin.end(); ``` +### `subprocess.hasRef()` + + + +* 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()` +> Stability: 1 - Experimental + * Returns: {boolean} Indicates whether a `subprocess` is "ref'ed" by the parent process's event From 0318f8f920d4963e8289815ccc78129f51646669 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Sat, 16 Apr 2022 23:48:53 +0900 Subject: [PATCH 6/6] fix: add `node:` prefix --- doc/api/child_process.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 079a8f9605f5be..c90e206ca77cda 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -1448,7 +1448,7 @@ 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 { spawn } = require('node:child_process'); const subprocess = spawn(process.argv[0], ['child_program.js'], { detached: true,