Hello folks at CKB DevRel,
I noticed that two clients sharing one JsonRpcTransportWebSocket can get each other's responses. Sharing a transport is what ClientPublicTestnet.new is for: "Creates a Client that borrows an existing Transport."
The Cause
Each client sends its requests through its own requestor. Then:
So two requestors both send a request number 0. While the first one is still waiting, the second one replaces it in the table:
Minimal Reproduction
Requestors a and b stand in for two clients. A local server stands in for the node.
import { ccc } from "@ckb-ccc/core";
import { WebSocketServer } from "ws";
// A server that answers each request with its method name after 100 ms.
new WebSocketServer({ port: 18114 }).on("connection", (socket) =>
socket.on("message", (raw) => {
const { id, method } = JSON.parse(raw.toString());
setTimeout(() => socket.send(JSON.stringify({ jsonrpc: "2.0", id, result: method })), 100);
}),
);
// Requestors sharing one transport with a 1 s timeout.
const transport = ccc.JsonRpcTransportWebSocket.open("ws://127.0.0.1:18114", 1000).value;
const request = (method: string) => ccc.RequestorJsonRpc.new({ transport }).request(method, []);
await request("connect"); // Open the socket first.
for (const method of ["a", "b"]) {
request(method).then(
(result) => console.log(method, "got", result),
(err) => console.log(method, "failed:", err.message),
);
await new Promise((resolve) => setTimeout(resolve, 10)); // Send `a` before `b`.
}
setTimeout(() => (console.log("3 s passed"), process.exit()), 3000);
Behavior
b got the response meant for a. Meanwhile a never settles, not even after three times its 1 s timeout.
Environment
@ckb-ccc/core@1.23.0, ws@8.22.0 for the local server
- Node.js 24.18.0, Linux
Keep up the Great Work,
Phroi %43
Hello folks at CKB DevRel,
I noticed that two clients sharing one
JsonRpcTransportWebSocketcan get each other's responses. Sharing a transport is whatClientPublicTestnet.newis for: "Creates a Client that borrows an existing Transport."The Cause
Each client sends its requests through its own requestor. Then:
So two requestors both send a request number 0. While the first one is still waiting, the second one replaces it in the table:
Minimal Reproduction
Requestors
aandbstand in for two clients. A local server stands in for the node.Behavior
bgot the response meant fora. Meanwhileanever settles, not even after three times its 1 s timeout.Environment
@ckb-ccc/core@1.23.0,ws@8.22.0for the local serverKeep up the Great Work,
Phroi %43