Skip to content

Commit 6b97a9a

Browse files
committed
doc: add subprocess.ref() and subprocess.unref() methods
1 parent c7962dc commit 6b97a9a

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

doc/api/child_process.md

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,12 @@ process will be made the leader of a new process group and session. Note that
536536
child processes may continue running after the parent exits regardless of
537537
whether they are detached or not. See setsid(2) for more information.
538538

539-
By default, the parent will wait for the detached child to exit. To prevent
540-
the parent from waiting for a given `subprocess`, use the `subprocess.unref()`
541-
method. Doing so will cause the parent's event loop to not include the child in
542-
its reference count, allowing the parent to exit independently of the child,
543-
unless there is an established IPC channel between the child and parent.
539+
By default, the parent will wait for the detached child to exit. To prevent the
540+
parent from waiting for a given `subprocess` to exit, use the
541+
`subprocess.unref()` method. Doing so will cause the parent's event loop to not
542+
include the child in its reference count, allowing the parent to exit
543+
independently of the child, unless there is an established IPC channel between
544+
the child and the parent.
544545

545546
When using the `detached` option to start a long-running process, the process
546547
will not stay running in the background after the parent exits unless it is
@@ -1094,6 +1095,27 @@ console.log(`Spawned child pid: ${grep.pid}`);
10941095
grep.stdin.end();
10951096
```
10961097

1098+
### subprocess.ref()
1099+
<!-- YAML
1100+
added: v0.7.10
1101+
-->
1102+
1103+
Calling `subprocess.ref()` after making a call to `subprocess.unref()` will
1104+
restore the removed reference count for the child process, forcing the parent
1105+
to wait for the child to exit before exiting itself.
1106+
1107+
```js
1108+
const { spawn } = require('child_process');
1109+
1110+
const subprocess = spawn(process.argv[0], ['child_program.js'], {
1111+
detached: true,
1112+
stdio: 'ignore'
1113+
});
1114+
1115+
subprocess.unref();
1116+
subprocess.ref();
1117+
```
1118+
10971119
### subprocess.send(message[, sendHandle[, options]][, callback])
10981120
<!-- YAML
10991121
added: v0.5.9
@@ -1362,6 +1384,29 @@ then this will be `null`.
13621384
`subprocess.stdout` is an alias for `subprocess.stdio[1]`. Both properties will
13631385
refer to the same value.
13641386

1387+
### subprocess.unref()
1388+
<!-- YAML
1389+
added: v0.7.10
1390+
-->
1391+
1392+
By default, the parent will wait for the detached child to exit. To prevent the
1393+
parent from waiting for a given `subprocess` to exit, use the
1394+
`subprocess.unref()` method. Doing so will cause the parent's event loop to not
1395+
include the child in its reference count, allowing the parent to exit
1396+
independently of the child, unless there is an established IPC channel between
1397+
the child and the parent.
1398+
1399+
```js
1400+
const { spawn } = require('child_process');
1401+
1402+
const subprocess = spawn(process.argv[0], ['child_program.js'], {
1403+
detached: true,
1404+
stdio: 'ignore'
1405+
});
1406+
1407+
subprocess.unref();
1408+
```
1409+
13651410
## `maxBuffer` and Unicode
13661411

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

0 commit comments

Comments
 (0)