@@ -1052,8 +1052,9 @@ const { fork } = require('child_process');
|
1052 | 1052 | const normal = fork('subprocess.js', ['normal']); |
1053 | 1053 | const special = fork('subprocess.js', ['special']); |
1054 | 1054 | |
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 }); |
1057 | 1058 | server.on('connection', (socket) => { |
1058 | 1059 | |
1059 | 1060 | // If this is special priority |
@@ -1073,7 +1074,12 @@ passed to the event callback function:
|
1073 | 1074 | ```js |
1074 | 1075 | process.on('message', (m, socket) => { |
1075 | 1076 | 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 | + } |
1077 | 1083 | } |
1078 | 1084 | }); |
1079 | 1085 | ``` |
@@ -1083,6 +1089,10 @@ tracking when the socket is destroyed. To indicate this, the `.connections`
|
1083 | 1089 | property becomes `null`. It is recommended not to use `.maxConnections` when |
1084 | 1090 | this occurs. |
1085 | 1091 | |
| 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 | + |
1086 | 1096 | *Note: this function uses [`JSON.stringify()`][] internally to serialize the |
1087 | 1097 | `message`.* |
1088 | 1098 | |
|