From a3017992e42dfccdeb5d3c94d27656b99c3ceaeb Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 16 Feb 2016 22:18:10 -0500 Subject: [PATCH] child_process: add keepOpen option to send() This option allows an instance of net.Socket to be kept open in the sending process. Fixes: https://github.com/nodejs/node/issues/4271 PR-URL: https://github.com/nodejs/node/pull/5283 Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis --- doc/api/child_process.markdown | 7 ++- lib/internal/child_process.js | 14 +++-- .../test-child-process-send-keep-open.js | 52 +++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 test/parallel/test-child-process-send-keep-open.js diff --git a/doc/api/child_process.markdown b/doc/api/child_process.markdown index 3a161b78a7134e..248834a64c9975 100644 --- a/doc/api/child_process.markdown +++ b/doc/api/child_process.markdown @@ -803,7 +803,12 @@ receive the object as the second argument passed to the callback function registered on the `process.on('message')` event. The `options` argument, if present, is an object used to parameterize the -sending of certain types of handles. +sending of certain types of handles. `options` supports the following +properties: + + * `keepOpen` - A Boolean value that can be used when passing instances of + `net.Socket`. When `true`, the socket is kept open in the sending process. + Defaults to `false`. The optional `callback` is a function that is invoked after the message is sent but before the child may have received it. The function is called with a diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 6b0831f21ca1d1..ac43c22a82a733 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -78,21 +78,25 @@ const handleConversion = { if (firstTime) socket.server._setupSlave(socketList); // Act like socket is detached - socket.server._connections--; + if (!options.keepOpen) + socket.server._connections--; } + var handle = socket._handle; + // remove handle from socket object, it will be closed when the socket // will be sent - var handle = socket._handle; - handle.onread = function() {}; - socket._handle = null; + if (!options.keepOpen) { + handle.onread = function() {}; + socket._handle = null; + } return handle; }, postSend: function(handle, options) { // Close the Socket handle after sending it - if (handle) + if (handle && !options.keepOpen) handle.close(); }, diff --git a/test/parallel/test-child-process-send-keep-open.js b/test/parallel/test-child-process-send-keep-open.js new file mode 100644 index 00000000000000..5771f4677c992a --- /dev/null +++ b/test/parallel/test-child-process-send-keep-open.js @@ -0,0 +1,52 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const cp = require('child_process'); +const net = require('net'); + +if (process.argv[2] !== 'child') { + // The parent process forks a child process, starts a TCP server, and connects + // to the server. The accepted connection is passed to the child process, + // where the socket is written. Then, the child signals the parent process to + // write to the same socket. + let result = ''; + + process.on('exit', () => { + assert.strictEqual(result, 'childparent'); + }); + + const child = cp.fork(__filename, ['child']); + + // Verify that the child exits successfully + child.on('exit', common.mustCall((exitCode, signalCode) => { + assert.strictEqual(exitCode, 0); + assert.strictEqual(signalCode, null); + })); + + const server = net.createServer((socket) => { + child.on('message', common.mustCall((msg) => { + assert.strictEqual(msg, 'child_done'); + socket.end('parent', () => { + server.close(); + child.disconnect(); + }); + })); + + child.send('socket', socket, {keepOpen: true}, common.mustCall((err) => { + assert.ifError(err); + })); + }); + + server.listen(common.PORT, () => { + const socket = net.connect(common.PORT, common.localhostIPv4); + socket.setEncoding('utf8'); + socket.on('data', (data) => result += data); + }); +} else { + // The child process receives the socket from the parent, writes data to + // the socket, then signals the parent process to write + process.on('message', common.mustCall((msg, socket) => { + assert.strictEqual(msg, 'socket'); + socket.write('child', () => process.send('child_done')); + })); +}