Repository navigation
Description
Problem
QwpEgressQuery.cancel() and QwpEgressQuery.grantCredit() return promises, and so do the cancel() and grantCredit() methods of the queryViews() control handle. But once a query has ended, and the session has processed its terminal response (RESULT_END, EXEC_DONE or QUERY_ERROR), they throw synchronously instead:
QwpProtocolError: QWP response references inactive request ID 0
The throw happens before any promise exists, so a .catch() on the result never runs.
Impact
- If
cancel()is called from anAbortSignallistener or a timer that fires after the query has completed, it raises an uncaught exception, and Node terminates the process with exit code 1. A common shape of this issignal.addEventListener("abort", () => void query.cancel().catch(() => {})). In browsers the.catch()is bypassed the same way, and the error surfaces as an uncaught exception. - The error type points at a server protocol violation that did not happen.
- Both
@questdb/nodejs-clientand@questdb/browser-clientare affected, because the egress session is shared throughclient-core.
Reproduction
const query = await session.query("select 1");
for await (const _batch of query) {
// consume the whole result
}
await query.completion;
query.isDone(); // true
query.cancel(); // throws QwpProtocolError synchronously
query.grantCredit(1); // throws QwpProtocolError synchronously
const controller = new AbortController();
controller.signal.addEventListener("abort", () => {
void query.cancel().catch(() => undefined); // the throw still escapes
});
controller.abort(); // Node: uncaught exception, the process exits with code 1Reproduced on main (a2af159) and on the #75 branch, which leaves this behavior unchanged.
Cause
In packages/client-core/src/_qwp/egress-session.ts, QwpEgressQuery.cancel() and grantCredit() delegate to QwpEgressSession.cancel() and grantCredit(). Both call requireActive(requestId) before any promise exists. Once the terminal response has been handled, clearActive() removes the query, and requireActive() throws QwpProtocolError.
The session already treats cancelling a finished query that is still active as a no-op: query.isDone() returns Promise.resolve(). The throw appears only after the query has been cleared. Because requireActive() calls throwIfUnavailable() first, the same synchronous throw also happens with the session's failure, or with QwpEgressSessionClosedError, when the session has failed or is closing.
Suggested fix
- Make
cancel()resolve as a no-op once the query is done. MakegrantCredit()resolve, or reject, instead of throwing: a finished query is owed no credit. - More generally, turn synchronous
requireActive()andthrowIfUnavailable()failures in the promise-returning query control methods into rejected promises. - Add a regression test that calls
cancel()andgrantCredit()after completion, including from anAbortSignallistener.
Found while reviewing #75 (#75 (comment)).
Activity
fix(qwp): stop cancel() and grantCredit() throwing after a query ends