Repository navigation
child_process: report a failed child stdin write with syscall write, as Node does #40935
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1dd795c
io: report a child stdin peer that went away as EPIPE from write
robobun 395583f
test: settle the stdin fixture when stdin closes without an error
robobun c16d116
io: shorten the write_to_socket comment
robobun ee245e7
test: use describe.each for the send errno cases
robobun ce6f365
io: name the Node divergence in the write_to_socket comment
robobun 92ab1d1
io: keep the kernel errno for child stdin writes, report the syscall …
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
test/js/node/child_process/child-process-stdin-send-errno.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // On POSIX, Bun backs a "pipe" child stdio with socketpair(2) and writes to it | ||
| // with send(2). When the child closes its end while a send is copying data in, | ||
| // the macOS kernel fails the send with ENOTCONN where a pipe reports EPIPE. Bun | ||
| // reports the socket the way a pipe would: EPIPE from "write". The race is | ||
| // timing dependent, so an LD_PRELOAD shim makes send() on the child's stdin | ||
| // socket fail with a chosen errno instead. | ||
| import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isLinux, tempDir } from "harness"; | ||
| import { constants } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const cc = Bun.which("cc") || Bun.which("gcc") || Bun.which("clang"); | ||
|
|
||
| // STDIN_FAIL_SEND_ERRNO=<n>: every send() on a non-stdio AF_UNIX socket fails | ||
| // with errno n. The child's stdin is the only such socket the fixture writes. | ||
| const SHIM_C = /* c */ ` | ||
| #define _GNU_SOURCE | ||
| #include <dlfcn.h> | ||
| #include <errno.h> | ||
| #include <stdlib.h> | ||
| #include <sys/socket.h> | ||
| #include <sys/stat.h> | ||
| #include <sys/types.h> | ||
| #include <unistd.h> | ||
|
|
||
| static ssize_t (*real_send)(int, const void *, size_t, int); | ||
| static int fail_errno = -1; | ||
|
|
||
| static int is_unix_sock(int fd) { | ||
| struct stat st; | ||
| if (fstat(fd, &st) != 0 || !S_ISSOCK(st.st_mode)) return 0; | ||
| int domain = 0; | ||
| socklen_t len = sizeof(domain); | ||
| return getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &len) == 0 && domain == AF_UNIX; | ||
| } | ||
|
|
||
| ssize_t send(int fd, const void *buf, size_t len, int flags) { | ||
| if (!real_send) { | ||
| real_send = (ssize_t (*)(int, const void *, size_t, int))dlsym(RTLD_NEXT, "send"); | ||
| const char *value = getenv("STDIN_FAIL_SEND_ERRNO"); | ||
| fail_errno = value ? atoi(value) : 0; | ||
| } | ||
| if (fail_errno > 0 && fd > 2 && is_unix_sock(fd)) { | ||
| errno = fail_errno; | ||
| return -1; | ||
| } | ||
| return real_send(fd, buf, len, flags); | ||
| } | ||
| `; | ||
|
|
||
| // The child only has to exist long enough to own the other end of the stdin | ||
| // socketpair. It runs without the shim so the fault stays in the parent. | ||
| const FIXTURE = /* js */ ` | ||
| import { spawn } from "node:child_process"; | ||
| const env = { ...process.env }; | ||
| delete env.LD_PRELOAD; | ||
| delete env.STDIN_FAIL_SEND_ERRNO; | ||
| const child = spawn(process.execPath, ["-e", "setTimeout(() => {}, 30_000)"], { | ||
| stdio: ["pipe", "ignore", "ignore"], | ||
| env, | ||
| }); | ||
| // "error" precedes "close", so a close without an error reports as such. | ||
| const error = await new Promise(resolve => { | ||
| child.stdin.on("error", err => resolve({ code: err.code, syscall: err.syscall, errno: err.errno })); | ||
| child.stdin.on("close", () => resolve("stdin closed without an error")); | ||
| child.on("error", err => resolve("spawn failed: " + err.code)); | ||
| child.stdin.end(Buffer.alloc(64 * 1024, 0x61)); | ||
| }); | ||
| child.kill(); | ||
| console.log(JSON.stringify(error)); | ||
| `; | ||
|
|
||
| describe.skipIf(!isLinux || !cc)("node:child_process stdin write error on a socketpair", () => { | ||
| let shimPath: string; | ||
| let dir: ReturnType<typeof tempDir>; | ||
|
|
||
| beforeAll(async () => { | ||
| dir = tempDir("child-stdin-send-errno", { | ||
| "shim.c": SHIM_C, | ||
| "fixture.mjs": FIXTURE, | ||
| }); | ||
| shimPath = join(String(dir), "shim.so"); | ||
| await using ccProc = Bun.spawn({ | ||
| cmd: [cc!, "-shared", "-fPIC", "-o", shimPath, join(String(dir), "shim.c"), "-ldl"], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [ccOut, ccErr, ccExit] = await Promise.all([ccProc.stdout.text(), ccProc.stderr.text(), ccProc.exited]); | ||
| if (ccExit !== 0) { | ||
| throw new Error(`shim compile failed: ${ccErr || ccOut}`); | ||
| } | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| dir?.[Symbol.dispose](); | ||
| }); | ||
|
|
||
| // Node names every stream write failure "write". The errno is the one a pipe | ||
| // gives for a reader that is gone, whatever the socket said. | ||
| describe.concurrent.each(["ENOTCONN", "EPIPE"] as const)("send() failing with %s", sendErrno => { | ||
| test("surfaces as EPIPE from write", async () => { | ||
| const existing = bunEnv.LD_PRELOAD; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "fixture.mjs"], | ||
| cwd: String(dir), | ||
| env: { | ||
| ...bunEnv, | ||
| LD_PRELOAD: existing ? `${shimPath}:${existing}` : shimPath, | ||
| STDIN_FAIL_SEND_ERRNO: String(constants.errno[sendErrno]), | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| let error: unknown; | ||
| try { | ||
| error = JSON.parse(stdout.trim().split("\n").pop() ?? ""); | ||
| } catch { | ||
| error = stdout; | ||
| } | ||
| expect({ error, stderr, exitCode }).toEqual({ | ||
| error: { code: "EPIPE", syscall: "write", errno: -constants.errno.EPIPE }, | ||
| stderr: "", | ||
| exitCode: 0, | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.