Skip to content

Cancel the server-side statement when a host call-timeout notification arrives #126

Closed

Description

@aesslinger

Background

tabularis#832 reported that a long-running execute_query fails with Plugin call 'execute_query' timed out after 120s while the statement keeps running server-side — the host abandons the pending JSON-RPC slot on timeout but never tells Postgres to stop the backend.

tabularis#833 fixed the "not configurable" half: the host's plugin-call timeout is now a global setting with a per-plugin override, live-reloaded, 0 = no limit. That's merged. The "orphaned query" half was explicitly left out of scope there.

On tabularis#832, the direction for that half was proposed and agreed between @aesslinger and @debba: a host→plugin cancel notification, not a plugin-local timeout/CancelToken setting. Rationale (copied from that thread):

The host already knows, precisely, the moment a call has timed out... What the host doesn't know, and shouldn't need to know, is how to actually stop a specific in-flight database operation — that's inherently driver-specific. So the cleanest split of responsibility is: Host owns when and, on timeout, fires a fire-and-forget JSON-RPC notification to the plugin — {"method": "cancel", "params": {"id": <the timed-out request's id>}}. Plugin owns how — it keeps a map of in-flight request ids to a cancel handle and, on receiving cancel for a known id, fires the driver-native cancel and drops the future.

For context: the builtin postgres driver has the identical gap today — cancel_query_impl (tabularis/src-tauri/src/commands.rs) only aborts its local tokio task handle, it never calls pg_cancel_backend either. So this plugin doing real server-side cancellation would be ahead of builtin parity for this one behavior, not behind it — worth flagging per this repo's "don't improve on the builtin silently" rule, but the improvement is scoped to a bug the reporter explicitly asked to have fixed, not a stylistic difference in SQL output.

This repo's half of the work

Blocked on the host side actually sending the cancel notification (not yet built — tracked in tabularis#832). This issue is the plugin-side implementation so it's ready to wire up as soon as that lands; until then a cancel notification simply never arrives and this is a safe no-op addition.

  • Add "cancel" => handlers::query::cancel(id, &params) (or similar) to src/rpc.rs's dispatch table. Per JSON-RPC convention this is a notification (no id in the request, no response expected) — confirm the exact envelope shape against whatever the host ships in tabularis#832 before wiring the dispatch.
  • execute_query, execute_query_batch, and explain_query (src/handlers/query.rs) need to register a cancel handle for their request id before starting the query, and deregister it when the query finishes (success or error) so the map doesn't leak.
    • For Postgres, the handle is a tokio_postgres::CancelToken, obtained from the deadpool_postgres::Object (pg_client.cancel_token()) before calling .query()/.execute() — the token carries the backend's PID/secret and needs a fresh connection (matching TLS mode) to actually send the cancel request.
    • A process-wide Mutex<HashMap<RequestId, CancelToken>> (or similar), analogous to the existing POOLS cache pattern in src/client.rs, is probably the simplest place for this — no async runtime changes needed since cancellation is fire-and-forget from the notification handler.
  • On receiving cancel for a known id: send the cancel request via the stored token, then just let the in-flight .query()/.execute() future resolve however it resolves (it'll likely fail with a Postgres-side "canceling statement due to user request" error, which flows through the normal Err path in exec_query_on_client and gets returned as a normal error response for that original call). An unknown id (already finished, or arrived after cleanup) is a no-op — never an error.
  • Unit-test coverage should follow this repo's existing TDD-for-parity-bugs pattern (see CLAUDE.md's "TDD for parity bugs without a live database" note): a fake/mock the cancel-token registration and lookup logic can be exercised without a live database; a live-database integration test (per the existing #[ignore]-gated pattern in this repo) can confirm the backend PID actually stops via pg_stat_activity, mirroring the reporter's own repro in tabularis-postgresql-plugin#120.

Not in scope here

  • The host-side notification-sending itself (tabularis#832/whatever PR follows tabularis#833).
  • Any change to execute_query_batch's single-connection-for-the-whole-batch semantics — cancelling one statement mid-batch should not tear down the shared client the remaining queued statements need.

Activity

  1. self-assigned this
    on Sep 30, 2026
  2. added
    enhancement
    on Sep 30, 2026
  3. aesslinger commented on Sep 30, 2026

    @aesslinger
    CollaboratorAuthor

    One more implementation detail, found while confirming this is safe to build/release ahead of the host side:

    Today main.rs/rpc.rs's loop writes exactly one response line for every line it reads — handle_line always returns a response Value and run_worker always forwards it to the writer. There's no notion of a fire-and-forget JSON-RPC notification (a request with no id) yet.

    If the host's eventual cancel message is a true id-less notification (likely, since it's fire-and-forget by design per the thread on tabularis#832), this needs a small addition beyond what's in the issue body above: handle_line (or its caller in main.rs) needs to recognize "no id in the incoming JSON" and skip writing a response line entirely, rather than falling through to the normal per-request path.

    Checked what happens today without that change, in case the host side ships before this issue is picked up: the plugin's existing not_implemented(id, other) fallback would echo back an error response using whatever id (or null) was on the incoming line. The host's JsonRpcResponse::id is a strict u64 (plugins/rpc.rs), so a null id fails to deserialize there — logged (log::error!("Failed to parse plugin response...")), not fatal, no protocol corruption. So building this repo's half first and releasing it independently of the host's PR is confirmed safe either direction; this note is just to get the notification-suppression right when the consumer side is actually implemented, so the exchange stays clean end-to-end once both halves are live.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions