Repository navigation
Conversation
…e command With two or more inspector clients attached to one process, each client received the responses to the commands of every other client. Two clients that use the same command id (each counts from 1) took each other's results. This happened on `bun --inspect`, on the unix socket transport of `BUN_INSPECT`, and on `inspector.open()` from node:inspector. JavaScriptCore sent each response to every frontend channel. The WebKit build that this commit pins (the preview build of oven-sh/WebKit#899) makes BackendDispatcher::dispatch() take the channel that sent the command, and sends the response to that channel only. BunInspectorConnection passes itself at its two dispatch calls.
…lient The error of a command that fails, at once or when an awaited promise settles, goes to the client that sent the command. A second client still receives Debugger.paused and Debugger.resumed. On inspector.open(), a client's successful command is not answered with another client's error.
|
Updated 12:33 PM PT - Oct 25th, 2026
✅ @robobun, your commit 396c4ff2a114072d328895877ab12196a664f1bb passed in 🧪 To try this PR locally: bunx bun-pr 46908That installs a local version of the PR into your bun-46908 --bun |
|
Status: reproduced on main (179664e).
The behaviour change is in oven-sh/WebKit#899. This pull request stays a draft until that one lands, and then the pin moves to its merge commit. |
|
A fourth answer for the "Decision needed" list in the description. It comes from the 4. Bun owns the inspector controller, one for each VM. No fork change.Today each global object has its own The branch
What a yes changes for the other open work
What it costs
The questionShould Bun own the inspector controller (this answer)? The alternative is that the fork's controller learns to move between the global objects of one VM.
The defects that this answersThe effects, all with
No issue tracks these four effects yet. Until one exists, this comment is the reference for them. #47113 repairs only the crashes in this area. Reproduction of effect 1
import { test } from "bun:test";
test("a", async () => {
globalThis.file = "a";
console.error("READY a");
await new Promise(resolve => (globalThis.release = resolve));
});
// bun driver.mjs [--isolate]
import { spawn } from "node:child_process";
const child = spawn(process.execPath, ["test", ...process.argv.slice(2), "--inspect=127.0.0.1:0", "./a.test.ts", "./b.test.ts"], {
stdio: ["ignore", "ignore", "pipe"],
});
let stderr = "";
child.stderr.on("data", chunk => (stderr += chunk));
const seen = async pattern => {
for (;;) {
const match = stderr.match(pattern);
if (match) return match;
await new Promise(resolve => setTimeout(resolve, 10));
}
};
const socket = new WebSocket((await seen(/^\s*(ws:\/\/\S+)$/m))[1]);
await new Promise(resolve => (socket.onopen = resolve));
const replies = new Map();
socket.onmessage = ({ data }) => {
const message = JSON.parse(data);
replies.get(message.id)?.(message.result?.result?.value);
};
let id = 0;
const evaluate = expression =>
Promise.race([
new Promise(resolve => {
replies.set(++id, resolve);
socket.send(JSON.stringify({ id, method: "Runtime.evaluate", params: { expression } }));
}),
new Promise(resolve => setTimeout(resolve, 2000, "no reply in 2 s")),
]);
await seen(/^READY a$/m);
console.log("in a.test.ts:", await evaluate("globalThis.file"));
await evaluate("globalThis.release()");
await seen(/^READY b$/m);
console.log("in b.test.ts:", await evaluate("globalThis.file"));
child.kill();
process.exit(0);Bun 1.4.4 and main give this output on Linux x64. Bun 1.4.4 and main give this output on Linux x64. |
|
Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-25. The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR. |
Problem
inspector.open(), one client and an in-processinspector.Sessionare enough. It also happens onbun --inspectand theBUN_INSPECTsocket.FrontendRouter::sendResponse()sends to every frontend channel.BunInspectorConnectiondispatches without naming itself (src/jsc/bindings/BunDebugger.cpp:370,:384).Fix
BackendDispatcher::dispatch()take the sending channel, and sends each response to that channel only. This PR pins its preview build and passes*thisat the two dispatch calls.test/cli/inspect/inspect.test.tsandtest/js/node/inspector/inspector.test.tsfail on main and pass here. Both directories: 139 pass, 0 fail.Background
Runtime.awaitPromise) answers later, possibly after its client left. So the pending command keeps the sender's identifier.internal/inspector/cdp.ts(node:inspector: route CDP responses to the right client when multiple are attached #35701, inspector: CDP on --inspect, node announce lines, Session, Profiler adapter, Network, exit handshake, async stacks (+9 tests, inspector 11%→22%) #34719). It coversinspector.open()only, and each client still receives each response.Downsides
USE(BUN_JSC_ADDITIONS)guard lines in 11 files..textgrows by 768 bytes. Per command with one client: 1,354 to 1,353 instructions, 68 to 68 allocator calls.Notes
Decision needed. The behaviour change is in the WebKit fork. There are three answers, and a maintainer chooses one:
inspector.open()gets one backend id counter for all adapters (node:inspector: route CDP responses to the right client when multiple are attached #35701, closed by the stale bot, or the id ranges in inspector: CDP on --inspect, node announce lines, Session, Profiler adapter, Network, exit handshake, async stacks (+9 tests, inspector 11%→22%) #34719).bun --inspectand theBUN_INSPECTsocket keep the defect.BunDebugger.cpp, stay.Designs considered.
cdp.ts(node:inspector: route CDP responses to the right client when multiple are attached #35701). It is 2 lines and exact forinspector.open(). The other transports have no adapter, and JavaScriptCore still sends each response to each channel.BunDebugger.cppin front of one JavaScriptCore channel. It needs no fork change. It must parse each command a second time to rewrite its id, and it copies connection state that JavaScriptCore already has.BackendDispatcher(this change). It adds nothing for one client and fixes every transport.Origin. A maintainer reported the
inspector.open()form in chat. No user issue reports crossed responses. #35701 fixed that form incdp.tsand was closed by the stale bot with no review.Repro on main (179664e). Two WebSocket clients on
bun --inspect, and each sends{"id":1,"method":"Runtime.evaluate",...}. Client A received["1:answer-for-A","1:answer-for-B"]and B received the same two, in 3 of 3 rounds. With ids 1 and 2, each client still received both responses. Oninspector.open():A got "answer-for-A", B got "answer-for-A", 3 of 3 rounds. On this branch: 0 of 3 in each form.Reach. A client that receives a response with an id it does not have pending usually drops it. It takes a foreign result only when the same id is pending. That is certain when two commands overlap, and each new test makes them overlap.
Debugger.enablefrom two clients shows the effect: on main each client receives{}anderror -32000 Debugger domain already enabled, so the second client can read a success. On this branch the first gets{}and the second gets the error.What is still wrong with two clients. This PR changes responses only.
Debugger.enablegetsDebugger domain already enabled.bun test --isolate, a client that attached in the first file gets no reply after the runner moves to the next file, on main and on this branch. On main a client that attaches later receives those replies. On this branch no client receives them.inspector.open(), a message that is not JSON gets no reply at all (cdp.tsreturns). Node answers the sender.Other open pull requests. #34719 adds a third
dispatchMessageFromRemotecall (BunDebugger.cpp:1143at its head 30097ed) for itsInProcessInspectorChannel. After this change that call must pass its channel, or it does not compile. Its id ranges for remote adapters are then not necessary. #36884 changesBunInspectorConnection::disconnectand does not conflict.The gate cannot run its fail-before step. oven-sh/WebKit#899 replaces the dispatch entries that carry no channel, so a caller that does not name its channel does not compile.
src/of main with this pin fails atBunDebugger.cpp:370and:384(too few arguments). The fail-before evidence is main's own release build: 14 of 14 new tests fail, in 20 of 20 runs. Withbun bd teston this branch, 14 of 14 pass.The new tests, and the result on main.
--inspect: each client gets the response to its own command, and an idle client gets noneanswer-for-A. The idle client receives both responses.--inspect: the response toRuntime.awaitPromisegoes to the client that awaitedsecond-for-B.--inspect: the response for a client that closed is not sent to the client that remains2:awaited-by-A.--inspect: a second client's command is answered to it while the first client's command is paused--inspect: the error of a command that fails goes to the client that sent it1:error -32000.--inspect: the error of an awaited promise that fails later goes to the client that awaited--inspect: the error for a message that is not JSON goes to its sender{"error":{"code":-32700,...},"id":null}.--inspect: a command from a client that closes before the inspected thread sees it is answered to no client1:answer-for-late.BUN_INSPECTunix socket and--inspectWebSocket on one processanswer-for-unix.inspector.open(): two clients each get the result of their ownRuntime.evaluatereleased, which is A's result.inspector.open(): the error of a failed command goes to the client that sent iterror -32000.inspector.open(): the result of an awaited promise goes to the client that awaitedsecond-for-B.inspector.open(): an in-processinspector.Sessiondoes not answer a clientDebugger.setBreakpointsActive.inspector.open(): a client of a reopened inspector does not get the result the previous client awaitedawaited-by-previous.An assertion that a message did not arrive follows a round trip on the same client (
settle()intest/cli/inspect/inspector-client.ts), not a sleep. The inspectee sends the responses and the events for one client through one ordered queue. The pause test also checks that the second client receivesDebugger.pausedandDebugger.resumed.Measurements. Linux x64, release builds with LTO: main 179664e (WebKit 680ef47c) against this branch (WebKit preview-pr-899-ba39e0db).
.textofbun-profilebunfilesizeofFrontendChannel/FrontendRouter/BackendDispatcher/CallbackBase--inspectclient for 20 responses of 8.4 MBBunInspectorConnection::receiveMessagesOnInspectorThreadto its return, forRuntime.evaluate "1". It counts that function,JSGlobalObjectDebuggable::dispatchMessageFromRemote,BackendDispatcher::*andRuntimeBackendDispatcher::*(with the inlinedFrontendRouter::sendResponse). Callees outside that set are the same code in both builds and are not counted. 12 of 12 samples were equal in each build.BunInspectorConnection::sendMessageToFrontend,ConsoleFrontendDispatcher::messageAdded(with the inlinedFrontendRouter::sendEventloop) andDebuggerFrontendDispatcher::scriptParsedis identical in both builds.mi_*entry points. The value for a command is the steady state.BackendDispatcher, with or without an inspector. mimalloc puts 72 and 80 bytes in the same 80-byte size class, so a process with no inspector uses the same heap bytes.CallbackBaseis one object for each pending asynchronous command.BunInspectorConnection::sendMessageToFrontendfor each channel, onbun --inspectand oninspector.open(). The idle client's channel went from 1,000 to 0.bun --inspect, 140 to 160 ms before and 90 to 110 ms after oninspector.open().Parity with Node. For 8
inspector.open()call orders whose transcript is stable under node v26.3.0, main gives node's transcript in 3 and this branch in 7. The one left differs by events: a client receivesDebugger.scriptParsedafter an in-process Session enabled the Debugger domain.Fork carry. The fork patch adds 30 guard lines, 19 of them with an
#elsethat keeps the upstream line. Upstream plans to removelegacy_async_callbacksfrom the inspector code generator (WebKit bug 288203). After that, the generated asynchronous commands do not compile in the fork until they capture the requester. That compile error is intended.Test runs.
bun bd test test/cli/inspect/ test/js/node/inspector/: 139 pass, 5 todo, 0 fail in 10 files. One test that this PR does not change,inspector.open() can be retried after a failed start, timed out in 3 of 30 runs of the debug build. In the hung process the debugger thread waits inJSC::Heap::collectSyncduringinspector.close(). No inspector client is attached in that test, so the change is not on that path. The release builds of main and of this branch passed it 60 of 60 times.Fork CI for oven-sh/WebKit#899 built every lane. One JSC stress test failed on one of the three tested lanes (
stress/tagged-templates-in-direct-eval-should-not-produce-same-site-object.js.dfg-eager, hardened lane). It does not use the inspector.