Repository navigation
Description
Summary
Follow-up to #167 / PR #176. PR #176 shipped Layers 1+2 (a dedicated cancel lane + a lifecycle lane) so a stuck general query no longer blocks ping/connect or the cancel notification itself. It deliberately did not ship Layer 3 (a worker-abort on cancel), because the naive implementation broke the #126 contract — and the viable mechanism costs more than the reported #167 reproduction justified.
This issue tracks the remaining gap and the mechanism that can close it without breaking the contract.
The remaining gap
cancel::cancel(request_id) fires pg_cancel_backend over a fresh connection (src/cancel.rs). The stuck worker is unblocked only indirectly — when the server returns 57014 query_canceled, the worker's handle_line await resolves and the worker frees. If the server ignores pg_cancel_backend (network partition, a pooler that accepts the TCP connection but never responds to the cancel request, a wedged backend), the worker's await never resolves, so:
- the worker stays parked for the full host call timeout (120s default),
- the held
deadpool::Object(DB connection) is not released until then, - and only the stdin-EOF
SHUTDOWN_GRACEdrain (#155) finally aborts the worker — which fires only when the whole host is gone, not on a per-call timeout.
This is the same behavior as the builtin (no plugin-side bound), so it's not a regression vs. the builtin. It IS a structural gap: a single uncooperative server can pin a worker for the full timeout, and since pg_cancel_backend is the only thing linking the cancel to the stuck worker, a server that ignores it leaves the plugin no recourse until the host gives up entirely.
Why PR #176 did not ship a worker-abort (the #126 contract)
The first Layer 3 attempt raced handle_line against a per-RPC CancellationToken in a select!, and on cancel dropped the handle_line future. The cancel_notification_stops_the_server_side_statement live test (tests/live_db.rs) proved this breaks the documented #126 contract: a cancelled query MUST return its 57014 error response, because the host's flow is send query (id N) -> send cancel (no id) -> read query N's response. Dropping the future discarded the in-progress 57014 response, so the host hung waiting for a reply that never came. The naive drop-on-cancel was reverted; the contract is the binding constraint.
The viable mechanism: cancel-then-grace (not drop-on-cancel)
The contract ("return the response") and the goal ("free the worker even when the server ignores cancel") are not actually opposed — the worker can free after producing a response. The mechanism that satisfies both:
- Cancel arrives -> fire
pg_cancel_backend(existing, server-side, unchanged). - Race
handle_lineagainst a bounded grace timer (e.g. 5-10s), not the cancel signal. Ifhandle_lineresolves within the grace (server cooperated), emit its57014response. Contract satisfied, worker free. - If the grace elapses (server ignored the cancel), abort the future AND emit a synthetic error response with the query's id (e.g.
{"error": {"code": -32603, "message": "cancelled: server did not respond to pg_cancel_backend"}, "id": N}) so the host still gets a response, not a hang. Worker free, contract satisfied.
Why this is regression-safe for #166
The grace timer fires only after a cancel (the host already gave up on the call — the cancel is the signal), not on every query. A query the host is willing to wait for (a large enum-table result the #166/#168 fix lets complete, a slow CREATE INDEX CONCURRENTLY) is never cut: the host hasn't sent a cancel, so no grace timer is started. This avoids the fixed-per-RPC-timeout risk that would re-regress #166 — the bound is scoped to the "host gave up but server won't release the worker" path, not to query duration.
Tradeoffs to weigh when implementing
- A new bound to size (the grace): too short cuts a server that's slow to honor the cancel; too long barely helps (the worker parks for the grace before the synthetic response). Probably 5-10s, but needs a justification.
- A new synthetic-response code path: a response the host didn't get from the server. The host must tolerate an error shape it didn't see before (verify against
tabularis's host-side cancel handling —plugins/call_timeout.rs/plugins/driver.rs). - A
select!-with-timer on every cancellable worker (general + lifecycle), which is more machinery than the lane routing in fix: route ping/connect/cancel to dedicated lanes so stuck queries don't starve them (#167) #176. - The cancel-lane worker itself must stay uncancellable (it's the relief valve — aborting it re-deadlocks the lane it relieves), as in fix: route ping/connect/cancel to dedicated lanes so stuck queries don't starve them (#167) #176.
When to revisit
This is defense-in-depth for a rarer failure (server ignores pg_cancel_backend), not a symptom-fix for the reported #167 reproduction (which #176's lane routing already handles). Revisit when there's a field report of a pooler/server that accepts the connection but never responds to pg_cancel_backend, leaving a worker parked for the full host timeout. If/when that surfaces, the cancel-then-grace mechanism above is the design — it closes the gap without the #126 contract break that killed the naive drop-on-cancel.
\cc @aesslinger
Activity