Skip to content

Worker not freed when the server ignores pg_cancel_backend (cancel-then-grace Layer 3 for #167) #177

Open

Description

@aesslinger

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_GRACE drain (#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:

  1. Cancel arrives -> fire pg_cancel_backend (existing, server-side, unchanged).
  2. Race handle_line against a bounded grace timer (e.g. 5-10s), not the cancel signal. If handle_line resolves within the grace (server cooperated), emit its 57014 response. Contract satisfied, worker free.
  3. 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

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

  1. added
    bug
    on Oct 23, 2026
  2. self-assigned this
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions