Skip to content

Commit ed186c9

Browse files
sam-githubtrevnorris
authored andcommitted
doc: child_process corrections and cleanups
- Make explicit that .disconnected is set before the disconnect event, and it is not allowed to send messages after calling .disconnect(), even while waiting for a delayed disconect event. - Remove obsolete claim that explicit exit is required - Describe silent: in the options for fork() - Describe .connected as the property it is, not just as an aside in the disconnect() method
1 parent 5bc5210 commit ed186c9

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

doc/api/child_process.markdown

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ of the signal, otherwise `null`.
5252

5353
Note that the child process stdio streams might still be open.
5454

55+
Also, note that node establishes signal handlers for `'SIGINT'` and `'SIGTERM`',
56+
so it will not terminate due to receipt of those signals, it will exit.
57+
5558
See `waitpid(2)`.
5659

5760
### Event: 'close'
@@ -66,10 +69,9 @@ might share the same stdio streams.
6669

6770
### Event: 'disconnect'
6871

69-
This event is emitted after using the `.disconnect()` method in the parent or
70-
in the child. After disconnecting it is no longer possible to send messages.
71-
An alternative way to check if you can send messages is to see if the
72-
`child.connected` property is `true`.
72+
This event is emitted after calling the `.disconnect()` method in the parent
73+
or in the child. After disconnecting it is no longer possible to send messages,
74+
and the `.connected` property is false.
7375

7476
### Event: 'message'
7577

@@ -121,6 +123,12 @@ Example:
121123
console.log('Spawned child pid: ' + grep.pid);
122124
grep.stdin.end();
123125

126+
### child.connected
127+
128+
* {Boolean} Set to false after `.disconnect' is called
129+
130+
If `.connected` is false, it is no longer possible to send messages.
131+
124132
### child.kill([signal])
125133

126134
* `signal` {String}
@@ -265,12 +273,15 @@ It is also recommended not to use `.maxConnections` in this condition.
265273

266274
### child.disconnect()
267275

268-
To close the IPC connection between parent and child use the
269-
`child.disconnect()` method. This allows the child to exit gracefully since
270-
there is no IPC channel keeping it alive. When calling this method the
271-
`disconnect` event will be emitted in both parent and child, and the
272-
`connected` flag will be set to `false`. Please note that you can also call
273-
`process.disconnect()` in the child process.
276+
Close the IPC channel between parent and child, allowing the child to exit
277+
gracefully once there are no other connections keeping it alive. After calling
278+
this method the `.connected` flag will be set to `false` in both the parent and
279+
child, and it is no longer possible to send messages.
280+
281+
The 'disconnect' event will be emitted when there are no messages in the process
282+
of being received, most likely immediately.
283+
284+
Note that you can also call `process.disconnect()` in the child process.
274285

275286
## child_process.spawn(command, [args], [options])
276287

@@ -465,7 +476,7 @@ See also: `child_process.exec()` and `child_process.fork()`
465476
* `env` {Object} Environment key-value pairs
466477
* `encoding` {String} (Default: 'utf8')
467478
* `timeout` {Number} (Default: 0)
468-
* `maxBuffer` {Number} (Default: 200*1024)
479+
* `maxBuffer` {Number} (Default: `200*1024`)
469480
* `killSignal` {String} (Default: 'SIGTERM')
470481
* `callback` {Function} called with the output when process terminates
471482
* `error` {Error}
@@ -531,7 +542,7 @@ subshell but rather the specified file directly. This makes it slightly
531542
leaner than `child_process.exec`. It has the same options.
532543

533544

534-
## child\_process.fork(modulePath, [args], [options])
545+
## child_process.fork(modulePath, [args], [options])
535546

536547
* `modulePath` {String} The module to run in the child
537548
* `args` {Array} List of string arguments
@@ -540,20 +551,15 @@ leaner than `child_process.exec`. It has the same options.
540551
* `env` {Object} Environment key-value pairs
541552
* `encoding` {String} (Default: 'utf8')
542553
* `execPath` {String} Executable used to create the child process
554+
* `silent` {Boolean} If true, prevent stdout and stderr in the spawned node
555+
process from being associated with the parent's (default is false)
543556
* Return: ChildProcess object
544557

545558
This is a special case of the `spawn()` functionality for spawning Node
546559
processes. In addition to having all the methods in a normal ChildProcess
547560
instance, the returned object has a communication channel built-in. See
548561
`child.send(message, [sendHandle])` for details.
549562

550-
By default the spawned Node process will have the stdout, stderr associated
551-
with the parent's. To change this behavior set the `silent` property in the
552-
`options` object to `true`.
553-
554-
The child process does not automatically exit once it's done, you need to call
555-
`process.exit()` explicitly. This limitation may be lifted in the future.
556-
557563
These child Nodes are still whole new instances of V8. Assume at least 30ms
558564
startup and 10mb memory for each new Node. That is, you cannot create many
559565
thousands of them.

0 commit comments

Comments
 (0)