Skip to content

Commit e313961

Browse files
tlhuntertargos
authored andcommitted
doc: add subprocess.ref() and subprocess.unref()
PR-URL: #22220 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Bryan English <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]>
1 parent 3666993 commit e313961

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

doc/api/child_process.md

+50-5
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,12 @@ process will be made the leader of a new process group and session. Note that
527527
child processes may continue running after the parent exits regardless of
528528
whether they are detached or not. See setsid(2) for more information.
529529

530-
By default, the parent will wait for the detached child to exit. To prevent
531-
the parent from waiting for a given `subprocess`, use the `subprocess.unref()`
532-
method. Doing so will cause the parent's event loop to not include the child in
533-
its reference count, allowing the parent to exit independently of the child,
534-
unless there is an established IPC channel between the child and parent.
530+
By default, the parent will wait for the detached child to exit. To prevent the
531+
parent from waiting for a given `subprocess` to exit, use the
532+
`subprocess.unref()` method. Doing so will cause the parent's event loop to not
533+
include the child in its reference count, allowing the parent to exit
534+
independently of the child, unless there is an established IPC channel between
535+
the child and the parent.
535536

536537
When using the `detached` option to start a long-running process, the process
537538
will not stay running in the background after the parent exits unless it is
@@ -1076,6 +1077,27 @@ console.log(`Spawned child pid: ${grep.pid}`);
10761077
grep.stdin.end();
10771078
```
10781079

1080+
### subprocess.ref()
1081+
<!-- YAML
1082+
added: v0.7.10
1083+
-->
1084+
1085+
Calling `subprocess.ref()` after making a call to `subprocess.unref()` will
1086+
restore the removed reference count for the child process, forcing the parent
1087+
to wait for the child to exit before exiting itself.
1088+
1089+
```js
1090+
const { spawn } = require('child_process');
1091+
1092+
const subprocess = spawn(process.argv[0], ['child_program.js'], {
1093+
detached: true,
1094+
stdio: 'ignore'
1095+
});
1096+
1097+
subprocess.unref();
1098+
subprocess.ref();
1099+
```
1100+
10791101
### subprocess.send(message[, sendHandle[, options]][, callback])
10801102
<!-- YAML
10811103
added: v0.5.9
@@ -1344,6 +1366,29 @@ then this will be `null`.
13441366
`subprocess.stdout` is an alias for `subprocess.stdio[1]`. Both properties will
13451367
refer to the same value.
13461368

1369+
### subprocess.unref()
1370+
<!-- YAML
1371+
added: v0.7.10
1372+
-->
1373+
1374+
By default, the parent will wait for the detached child to exit. To prevent the
1375+
parent from waiting for a given `subprocess` to exit, use the
1376+
`subprocess.unref()` method. Doing so will cause the parent's event loop to not
1377+
include the child in its reference count, allowing the parent to exit
1378+
independently of the child, unless there is an established IPC channel between
1379+
the child and the parent.
1380+
1381+
```js
1382+
const { spawn } = require('child_process');
1383+
1384+
const subprocess = spawn(process.argv[0], ['child_program.js'], {
1385+
detached: true,
1386+
stdio: 'ignore'
1387+
});
1388+
1389+
subprocess.unref();
1390+
```
1391+
13471392
## `maxBuffer` and Unicode
13481393

13491394
The `maxBuffer` option specifies the largest number of bytes allowed on `stdout`

0 commit comments

Comments
 (0)