Repository navigation
Description
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 receivingcancelfor 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, ¶ms)(or similar) tosrc/rpc.rs's dispatch table. Per JSON-RPC convention this is a notification (noidin the request, no response expected) — confirm the exact envelope shape against whatever the host ships intabularis#832before wiring the dispatch. execute_query,execute_query_batch, andexplain_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 thedeadpool_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 existingPOOLScache pattern insrc/client.rs, is probably the simplest place for this — no async runtime changes needed since cancellation is fire-and-forget from the notification handler.
- For Postgres, the handle is a
- On receiving
cancelfor 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 normalErrpath inexec_query_on_clientand 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 viapg_stat_activity, mirroring the reporter's own repro intabularis-postgresql-plugin#120.
Not in scope here
- The host-side notification-sending itself (
tabularis#832/whatever PR followstabularis#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
aesslinger commentedon Sep 30, 2026
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_linealways returns a responseValueandrun_workeralways forwards it to the writer. There's no notion of a fire-and-forget JSON-RPC notification (a request with noid) yet.If the host's eventual
cancelmessage is a true id-less notification (likely, since it's fire-and-forget by design per the thread ontabularis#832), this needs a small addition beyond what's in the issue body above:handle_line(or its caller inmain.rs) needs to recognize "noidin 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 whateverid(ornull) was on the incoming line. The host'sJsonRpcResponse::idis a strictu64(plugins/rpc.rs), so anullid 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.feat: cancel the server-side statement on a host cancel notification (#…
chore: release 1.0.0-rc.6
fix: return -32600 for non-object JSON-RPC lines instead of swallowin…
fix: return -32600 for objects missing a string method field (#137) (#…
fix: bound plugin shutdown so an orphaned process can't hang forever (#…
fix: stop one-shot routing from evicting the batch cancel guard (#172)
fix: pre-resolve column types on batch/session pooler path (#169) (#172)
fix: route ping/connect/cancel to dedicated lanes so stuck queries do…
fix: surface prepare errors inside an open transaction instead of cas…
fix: surface prepare errors inside an open transaction instead of cas…
fix: address pre-handoff review findings on PR #184 (#181)
fix: close the 25P02 cascade for query_typed/execute_typed runtime er…