Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deno run doesn't terminate when node:child_process.fork() is used #24756

Closed
uasi opened this issue Jul 26, 2024 · 2 comments · Fixed by #24763
Closed

deno run doesn't terminate when node:child_process.fork() is used #24756

uasi opened this issue Jul 26, 2024 · 2 comments · Fixed by #24763
Assignees
Labels
bug Something isn't working correctly node compat

Comments

@uasi
Copy link

uasi commented Jul 26, 2024

// parent.mjs
import { fork } from "node:child_process";
fork("child.js");

// child.js
console.log("hello");
% node parent.mjs
hello

% deno run -A parent.mjs
hello
# ...deno hangs

Versions

% node --version
v20.12.2

% deno --version
deno 1.45.2 (release, aarch64-apple-darwin)
v8 12.7.224.12
typescript 5.5.2
@uasi
Copy link
Author

uasi commented Jul 26, 2024

Apparently, setting "ipc" to the stdio option of node:child_process.spawn() causes Deno to hang.

https://github.com/denoland/deno/blob/v1.45.4/ext/node/polyfills/child_process.ts#L145-L156

// spawn.mjs
import { spawn } from "node:child_process";
spawn("/bin/echo", ["hello"], { stdio: ["inherit", "inherit", "inherit"] });

// spawn_ipc.mjs
import { spawn } from "node:child_process";
spawn("/bin/echo", ["hello"], { stdio: ["inherit", "inherit", "inherit", "ipc"] });
% deno run -A spawn.mjs
hello

% deno run -A spawn_ipc.mjs
hello
# deno hangs

@nathanwhit
Copy link
Member

I'm currently working on this - should have a PR to fix this (and some other child_process ipc related issues) soon

@nathanwhit nathanwhit self-assigned this Jul 26, 2024
@nathanwhit nathanwhit added bug Something isn't working correctly node compat labels Jul 26, 2024
crowlKats pushed a commit that referenced this issue Jul 31, 2024
Fixes #24756. Fixes
#24796.

This also gets vitest working when using
[`--pool=forks`](https://vitest.dev/guide/improving-performance#pool)
(which is the default as of vitest 2.0). Ref
#23882.

---

This PR resolves a handful of issues with child_process IPC. In
particular:

- We didn't support sending typed array views over IPC
- Opening an IPC channel resulted in the event loop never exiting
- Sending a `null` over IPC would terminate the channel
- There was some UB in the read implementation (transmuting an `&[u8]`
to `&mut [u8]`)
- The `send` method wasn't returning anything, so there was no way to
signal backpressure (this also resulted in the benchmark
`child_process_ipc.mjs` being misleading, as it tried to respect
backpressure. That gave node much worse results at larger message sizes,
and gave us much worse results at smaller message sizes).
- We weren't setting up the `channel` property on the `process` global
(or on the `ChildProcess` object), and also didn't have a way to
ref/unref the channel
- Calling `kill` multiple times (or disconnecting the channel, then
calling kill) would throw an error
- Node couldn't spawn a deno subprocess and communicate with it over IPC

(cherry picked from commit cd59fc5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly node compat
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants