Repository navigation
ipc: report an undecodable message instead of closing the channel silently #41791
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunExe } from "harness"; | ||
| import { bunEnv, bunExe, nodeExe } from "harness"; | ||
| import path from "path"; | ||
|
|
||
| test("ipc with json serialization still works when bun is not the parent and the child", async () => { | ||
|
|
@@ -19,3 +19,63 @@ p I am your father | |
| `, | ||
| ); | ||
| }); | ||
|
|
||
| // A Node.js parent that sends one message to a Bun child with no | ||
| // uncaughtException handler. Returns what the parent saw of the child. | ||
| async function bunChildOfNodeParent(serialization: "advanced" | "json", message: string) { | ||
| const parentSource = ` | ||
| const { spawn } = require("node:child_process"); | ||
| const child = spawn(process.argv[1], ["-e", 'process.on("message", msg => console.log("UNEXPECTED_IPC_MESSAGE", msg));'], { | ||
| stdio: ["ignore", "pipe", "pipe", "ipc"], | ||
| serialization: "${serialization}", | ||
| }); | ||
| let stdout = "", stderr = ""; | ||
| child.stdout.setEncoding("utf8").on("data", chunk => (stdout += chunk)); | ||
| child.stderr.setEncoding("utf8").on("data", chunk => (stderr += chunk)); | ||
| child.on("close", (exitCode, signalCode) => { | ||
| console.log(JSON.stringify({ stdout, stderr, exitCode, signalCode })); | ||
| }); | ||
| child.send(${message}); | ||
| `; | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [nodeExe()!, "-e", parentSource, bunExe()], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| expect(exitCode).toBe(0); | ||
| return JSON.parse(stdout) as { stdout: string; stderr: string; exitCode: number | null; signalCode: string | null }; | ||
| } | ||
|
|
||
| test.skipIf(!nodeExe())( | ||
| 'a bun child of a node parent using serialization: "advanced" reports the mismatch', | ||
| async () => { | ||
| // Node's "advanced" serialization is v8's wire format, which Bun cannot decode. | ||
|
Comment on lines
+55
to
+57
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 nit (optional): the new subprocess tests run serially, so the suite takes longer than it needs to on the debug/ASAN lanes. sweep: Why this was flaggedtest/CLAUDE.md says to prefer test.concurrent when multiple tests in the same file spawn processes. The four tests added at test/js/bun/spawn/spawn.ipc.node-bun.test.ts:55 and :72 and test/js/bun/spawn/spawn.ipc.bun-node.test.ts:46 and :66 each spawn a node process that in turn spawns or is spawned by a bun process, and none of them share files, ports or globals. They are declared with plain test.skipIf(!nodeExe()), so each file's two-process startups run one after another; on debug+ASAN CI lanes (10-100x slower per test/CLAUDE.md) that is avoidable wall time. The base branch had only one serial test per file, so the cost is new with this change. No functional failure; this is a convention slip. Verification: nit. Triggering condition: whenever the suite runs, the four new two-process tests execute one after another. Mechanism verified: test/js/bun/spawn/spawn.ipc.node-bun.test.ts:54 and :71 and test/js/bun/spawn/spawn.ipc.bun-node.test.ts:46 and :64 are all declared
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. |
||
| // The child must report the first such message instead of dropping the channel | ||
| // silently. | ||
| const child = await bunChildOfNodeParent("advanced", `{ hello: "from node" }`); | ||
|
|
||
| // No uncaughtException handler in the child: it exits 1 at the report. | ||
| expect(child.stdout).toBe(""); | ||
| expect(child.stderr).toContain( | ||
| `The parent process sent an IPC message that is not in Bun's "advanced" serialization format, so Bun closed the IPC channel. "advanced" serialization only works between two Bun processes. For IPC between Bun and Node.js, use serialization: "json".`, | ||
| ); | ||
| expect({ exitCode: child.exitCode, signalCode: child.signalCode }).toEqual({ exitCode: 1, signalCode: null }); | ||
| }, | ||
| ); | ||
|
|
||
| test.skipIf(!nodeExe())("a bun child reports a line from a node parent that is not JSON", async () => { | ||
| // JSON.stringify() of an object whose toJSON() returns undefined is undefined, | ||
| // so Node.js writes the line "undefined" to the channel. | ||
| const child = await bunChildOfNodeParent("json", `{ toJSON() {} }`); | ||
|
|
||
| expect(child.stdout).toBe(""); | ||
| expect(child.stderr).toContain( | ||
| "The parent process sent an IPC message that is not valid JSON, so Bun closed the IPC channel.", | ||
| ); | ||
| expect({ exitCode: child.exitCode, signalCode: child.signalCode }).toEqual({ exitCode: 1, signalCode: null }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 nit (optional): a user debugging a bad "json" IPC line gets only "not valid JSON" and never learns what was rejected or why. The parser's SyntaxError is discarded by
global_this.clear_exception()at src/runtime/ipc.rs:533 before the InvalidFormat report is built, and the report at src/runtime/ipc.rs:2314-2316 echoes neither that message nor any part of the line. Fix: carry the cause into the report, e.g. keep the SyntaxError's message (or a bounded prefix of the offending line) alongsideInvalidFormatand append it to the "json" error text, so the same report also covers the empty-line and lone-tag-byte rejections at ipc.rs:491-492.Why this was flagged
A peer in serialization "json" writes a line such as "undefined\n" or "{not json\n"; it arrives through on_data2 -> decode_next_json -> json::decode_ipc_message (src/runtime/ipc.rs:456). to_js_by_parse_json throws a SyntaxError that names the bad token; the Err(JsError::Thrown) arm at src/runtime/ipc.rs:531-534 calls clear_exception() and returns InvalidFormat, so the cause is gone. finish_decode then calls report_undecodable_message (src/runtime/ipc.rs:2304), whose Mode::Json arm at 2314-2316 formats only the peer name and the fixed sentence "sent an IPC message that is not valid JSON, so Bun closed the IPC channel." The user sees a report (the base branch printed nothing) but cannot tell which line or which parse failure caused it; Node, whose behaviour the PR cites, surfaces the SyntaxError itself with the unexpected token. REVIEW.md asks that error messages carry the rejected value and the underlying cause; this one carries neither. No safeguard prevents it: the exception is cleared before any caller can read it.
Verification: nit. Any peer in serialization "json" writes a line that JSON.parse rejects. /home/claude/bun/src/runtime/ipc.rs:521 throws a SyntaxError; the
Err(JsError::Thrown)arm at lines 531-535 doesglobal_this.clear_exception(); return Err(IPCDecodeError::InvalidFormat);, so the parser's message is discarded. The message at 2314-2316 is built solely from{peer}and fixed text.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
SyntaxErroris cleared injson::decode_ipc_messageon main too, so the report in this PR has no cause to show. To carry it (for example as thecauseof the reported error) changes the shape of that error, and the shape is the open question on this PR. I will add it in the shape that a maintainer picks.