Skip to content

QWP egress: cancel() and grantCredit() throw synchronously once a query has ended #76

Open

Description

@glasstiger

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 an AbortSignal listener 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 is signal.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-client and @questdb/browser-client are affected, because the egress session is shared through client-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 1

Reproduced 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. Make grantCredit() resolve, or reject, instead of throwing: a finished query is owed no credit.
  • More generally, turn synchronous requireActive() and throwIfUnavailable() failures in the promise-returning query control methods into rejected promises.
  • Add a regression test that calls cancel() and grantCredit() after completion, including from an AbortSignal listener.

Found while reviewing #75 (#75 (comment)).

Activity

  1. added a commit that references this issue on Nov 10, 2026
    87442b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions