Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a0b3805

Browse filesBrowse files
cjihrigMylesBorins
authored andcommitted
doc: make socket IPC examples more robust
This commit aims to improve the documentation examples that send sockets over IPC channels. Specifically, pauseOnConnect is added to a server that inspects the socket before sending and a 'message' handler adds a check that the socket still exists. PR-URL: #13196 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 4030c7e commit a0b3805
Copy full SHA for a0b3805

File tree

Expand file treeCollapse file tree

1 file changed

+13
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+13
-3
lines changed
Open diff view settings
Collapse file

‎doc/api/child_process.md‎

Copy file name to clipboardExpand all lines: doc/api/child_process.md
+13-3Lines changed: 13 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1052,8 +1052,9 @@ const { fork } = require('child_process');
10521052
const normal = fork('subprocess.js', ['normal']);
10531053
const special = fork('subprocess.js', ['special']);
10541054

1055-
// Open up the server and send sockets to child
1056-
const server = require('net').createServer();
1055+
// Open up the server and send sockets to child. Use pauseOnConnect to prevent
1056+
// the sockets from being read before they are sent to the child process.
1057+
const server = require('net').createServer({ pauseOnConnect: true });
10571058
server.on('connection', (socket) => {
10581059

10591060
// If this is special priority
@@ -1073,7 +1074,12 @@ passed to the event callback function:
10731074
```js
10741075
process.on('message', (m, socket) => {
10751076
if (m === 'socket') {
1076-
socket.end(`Request handled with ${process.argv[2]} priority`);
1077+
if (socket) {
1078+
// Check that the client socket exists.
1079+
// It is possible for the socket to be closed between the time it is
1080+
// sent and the time it is received in the child process.
1081+
socket.end(`Request handled with ${process.argv[2]} priority`);
1082+
}
10771083
}
10781084
});
10791085
```
@@ -1083,6 +1089,10 @@ tracking when the socket is destroyed. To indicate this, the `.connections`
10831089
property becomes `null`. It is recommended not to use `.maxConnections` when
10841090
this occurs.
10851091

1092+
It is also recommended that any `'message'` handlers in the child process
1093+
verify that `socket` exists, as the connection may have been closed during the
1094+
time it takes to send the connection to the child.
1095+
10861096
*Note: this function uses [`JSON.stringify()`][] internally to serialize the
10871097
`message`.*
10881098

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.