Repository navigation
Description
Problem
In a browser, the QWP client closes some WebSockets with status codes 1001 and 1011. For example, QwpClient.close() uses 1001, and a cancelled query that does not end on a reconnect: false session uses 1011. The browser's WebSocket.close(code) accepts only 1000 or 3000–4999 and throws InvalidAccessError for anything else.
closeSocket() in websocket-connection.ts catches that error and falls back to socket.terminate?.(), which browser sockets do not have. It then records a synthetic 1006 close and removes its listeners. The client treats the connection as closed, but the browser socket was never closed. It stays open until the page unloads or the browser garbage-collects it.
Impact
QwpClient.close()in a browser leaves its pooled query sockets open. Observed: the query socket was stillOPEN2.5 s afterclient.close()resolved.- Take a query session opened with
reconnect: falsewhose cancelled query does not end withincancelDrainTimeoutMs. The session fails as documented, but its socket stays open, and the server keeps sending the cancelled result to the tab. Observed: 294 more frames in the 3 s after the session failed (QuestDB 10.1.0, 500M-row query). - The server keeps the connection open. For a query that is still sending results, it keeps executing it and using server resources and bandwidth.
- Only
@questdb/browser-clientis affected.ws, which the Node client uses, accepts these codes, and its sockets haveterminate().
Reproduction
Chromium via Playwright, QuestDB 10.1.0. The page is served from the QuestDB origin, because QuestDB rejects a cross-origin WebSocket upgrade.
// The browser API on its own
const raw = new WebSocket("ws://127.0.0.1:9000/read/v1");
raw.close(1011, "x"); // throws InvalidAccessError
// QwpClient.close()
const sockets = [];
const webSocketFactory = (url, protocols) => {
const socket = new WebSocket(url, protocols);
sockets.push(socket);
return socket;
};
const client = createQwpBrowserClient({
cluster: { url: "ws://127.0.0.1:9000" },
egress: { webSocketFactory },
});
const lease = await client.borrowQuery();
for await (const _batch of await lease.query("select 1")) {
// consume
}
await lease.close();
await client.close();
await new Promise((resolve) => setTimeout(resolve, 2500));
sockets.map((socket) => socket.readyState); // [1]: still OPEN
// A cancel that does not end, without reconnect
const session = await connectQwpBrowserEgress({
url: "ws://127.0.0.1:9000/read/v1",
reconnect: false,
cancelDrainTimeoutMs: 300,
closeTimeoutMs: 1000,
webSocketFactory,
});
const query = await session.query(
"select x, rnd_str(64,64,0) s from long_sequence(500000000)",
);
await query[Symbol.asyncIterator]().next();
await query.cancel();
await query.completion.catch(() => undefined); // QwpEgressQueryCancelTimeoutError
// 3 s later the socket is still OPEN and keeps receiving frames.Reproduced on main (a2af159) and on the #75 branch (61ffd2b). On main every cancelled query that did not end took this path, because the session always failed with code 1011. #75 replaces the connection with code 1000 on reconnecting sessions, and that close works, so on that branch only reconnect: false sessions still hit it.
Cause
closeSocket() in packages/client-core/src/_qwp/_internal/websocket-connection.ts passes the caller's code straight to the native socket (socket.close(code, reason)). Two callers pass codes the browser rejects, and both were reproduced:
QwpEgressSession.shutdownForClientClose()passesclose(1001, "QWP client shutting down").- When a cancelled query does not end in time, the session passes
close(1011, "QWP cancellation drain timed out").
Other sites pass 1002 or 1011: protocol errors, send failures, failed reconnects, and poisoned ingress frames. They reach the same line, but I did not reproduce them separately.
The catch block falls back to socket.terminate?.(), which only Node's ws sockets have, so in a browser nothing closes the socket. settleClosed() then removes the listeners, so the client stops seeing the frames that keep arriving.
Suggested fix
- When a socket has no
terminate(), as in a browser, close with 1000, or map the code into the 4000–4999 application range. Do the mapping incloseSocket()so every caller gets it. - If a coded close still throws, retry
socket.close()without a code before settling. - Add a browser e2e test that checks the native socket's
readyStateafterQwpClient.close()and after areconnect: falsecancel times out.
Found while reviewing #75.
Activity