Skip to content

Commit

Permalink
fix: implement child_process IPC (#21490)
Browse files Browse the repository at this point in the history
This PR implements the Node child_process IPC functionality in Deno on
Unix systems.

For `fd > 2` a duplex unix pipe is set up between the parent and child
processes. Currently implements data passing via the channel in the JSON
serialization format.
  • Loading branch information
littledivy authored Dec 13, 2023
1 parent bbf8f69 commit 5a91a06
Show file tree
Hide file tree
Showing 22 changed files with 1,158 additions and 32 deletions.
145 changes: 145 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,17 @@ impl CliOptions {
.map(Some)
}

pub fn node_ipc_fd(&self) -> Option<i32> {
let maybe_node_channel_fd = std::env::var("DENO_CHANNEL_FD").ok();
if let Some(node_channel_fd) = maybe_node_channel_fd {
// Remove so that child processes don't inherit this environment variable.
std::env::remove_var("DENO_CHANNEL_FD");
node_channel_fd.parse::<i32>().ok()
} else {
None
}
}

pub fn resolve_main_module(&self) -> Result<ModuleSpecifier, AnyError> {
match &self.flags.subcommand {
DenoSubcommand::Bundle(bundle_flags) => {
Expand Down
1 change: 1 addition & 0 deletions cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ impl CliFactory {
self.maybe_lockfile().clone(),
self.feature_checker().clone(),
self.create_cli_main_worker_options()?,
self.options.node_ipc_fd(),
))
}

Expand Down
1 change: 1 addition & 0 deletions cli/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ pub async fn run(
unstable: metadata.unstable,
maybe_root_package_json_deps: package_json_deps_provider.deps().cloned(),
},
None,
);

v8_set_flags(construct_v8_flags(&[], &metadata.v8_flags, vec![]));
Expand Down
18 changes: 7 additions & 11 deletions cli/tests/node_compat/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
"test-child-process-execfile.js",
"test-child-process-execsync-maxbuf.js",
"test-child-process-exit-code.js",
// TODO(littledivy): windows ipc streams not yet implemented
"test-child-process-fork-ref.js",
"test-child-process-fork-ref2.js",
"test-child-process-ipc-next-tick.js",
"test-child-process-ipc.js",
"test-child-process-spawnsync-env.js",
"test-child-process-stdio-inherit.js",
Expand Down Expand Up @@ -109,9 +113,7 @@
"test-zlib-zero-windowBits.js"
],
"pummel": [],
"sequential": [
"test-child-process-exit.js"
]
"sequential": ["test-child-process-exit.js"]
},
"tests": {
"common": [
Expand All @@ -138,11 +140,7 @@
"print-chars.js",
"x.txt"
],
"fixtures/keys": [
"agent1-cert.pem",
"agent1-key.pem",
"ca1-cert.pem"
],
"fixtures/keys": ["agent1-cert.pem", "agent1-key.pem", "ca1-cert.pem"],
"internet": [
"test-dns-any.js",
"test-dns-idna2008.js",
Expand Down Expand Up @@ -695,9 +693,7 @@
"test-tty-stdout-end.js"
],
"pummel": [],
"sequential": [
"test-child-process-exit.js"
]
"sequential": ["test-child-process-exit.js"]
},
"windowsIgnore": {
"parallel": [
Expand Down
72 changes: 72 additions & 0 deletions cli/tests/node_compat/test/parallel/test-child-process-fork-ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 18.12.1
// This file is automatically generated by `tools/node_compat/setup.ts`. Do not modify this file manually.

// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';

// Ignore on Windows.
if (process.platform === 'win32') {
process.exit(0);
}

require('../common');
const assert = require('assert');
const fork = require('child_process').fork;

if (process.argv[2] === 'child') {
process.send('1');

// Check that child don't instantly die
setTimeout(function() {
process.send('2');
}, 200);

process.on('disconnect', function() {
process.stdout.write('3');
});

} else {
const child = fork(__filename, ['child'], { silent: true });

const ipc = [];
let stdout = '';

child.on('message', function(msg) {
ipc.push(msg);

if (msg === '2') child.disconnect();
});

child.stdout.on('data', function(chunk) {
stdout += chunk;
});

child.once('exit', function() {
assert.deepStrictEqual(ipc, ['1', '2']);
assert.strictEqual(stdout, '3');
});
}
Loading

0 comments on commit 5a91a06

Please sign in to comment.