From e65bed1b7e273d1db6ba5905c8f68338b06cd27a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 29 Oct 2019 20:15:31 +0100 Subject: [PATCH] child_process: create proper public API for `channel` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of exposing the C++ bindings object as `subprocess.channel` or `process.channel`, provide the “control” object that was previously used internally as the public-facing variant of it. This should be better than returning the raw pipe object, and matches the original intention (when the `channel` property was first added) of providing a proper way to `.ref()` or `.unref()` the channel. PR-URL: https://github.com/nodejs/node/pull/30165 Refs: https://github.com/nodejs/node/pull/9322 Refs: https://github.com/nodejs/node/issues/9313 Reviewed-By: Jeremiah Senkpiel Reviewed-By: Denys Otrishko Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- doc/api/child_process.md | 20 +++++++ doc/api/process.md | 28 ++++++++++ lib/child_process.js | 4 +- .../bootstrap/switches/is_main_thread.js | 3 +- lib/internal/child_process.js | 56 ++++++++++++++----- .../test-child-process-recv-handle.js | 9 ++- test/parallel/test-child-process-silent.js | 3 +- 7 files changed, 100 insertions(+), 23 deletions(-) diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 6f74038b3c9c90..f9b0ebf1f92a05 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -1018,6 +1018,10 @@ See [Advanced Serialization][] for more details. ### `subprocess.channel` * {Object} A pipe representing the IPC channel to the child process. @@ -1025,6 +1029,22 @@ added: v7.1.0 The `subprocess.channel` property is a reference to the child's IPC channel. If no IPC channel currently exists, this property is `undefined`. +#### `subprocess.channel.ref()` + + +This method makes the IPC channel keep the event loop of the parent process +running if `.unref()` has been called before. + +#### `subprocess.channel.unref()` + + +This method makes the IPC channel not keep the event loop of the parent process +running, and lets it finish even while the channel is open. + ### `subprocess.connected` * {Object} @@ -635,6 +639,30 @@ If the Node.js process was spawned with an IPC channel (see the property is a reference to the IPC channel. If no IPC channel exists, this property is `undefined`. +### `process.channel.ref()` + + +This method makes the IPC channel keep the event loop of the process +running if `.unref()` has been called before. + +Typically, this is managed through the number of `'disconnect'` and `'message'` +listeners on the `process` object. However, this method can be used to +explicitly request a specific behavior. + +### `process.channel.unref()` + + +This method makes the IPC channel not keep the event loop of the process +running, and lets it finish even while the channel is open. + +Typically, this is managed through the number of `'disconnect'` and `'message'` +listeners on the `process` object. However, this method can be used to +explicitly request a specific behavior. + ## `process.chdir(directory)`