Skip to content

fix(linux-postgres): wait for the server to take a cancel request - #3423

Open
datlechin wants to merge 2 commits into
linuxfrom
fix/linux-pg-late-cancel
Open

datlechin wants to merge 2 commits into
linuxfrom
fix/linux-pg-late-cancel

Conversation

@datlechin

Copy link
Copy Markdown
Member

Why

After a PostgreSQL query timed out or was stopped, the next query on the same connection could fail with "canceling statement due to user request". tokio-postgres' cancel_query_raw returns before the server has read the cancel, so the cancel could land on the next statement. It showed up as an intermittent deadline_1s_timeout_server_cancelled failure in CI.

Change

  • The cancel connection now waits for the server to close it, as libpq's PQcancel does, before the stop counts as delivered; the server closes it only after signalling the backend.
  • Covers pooled reads, writes and the editor session; the EngineStop::stop contract says so.

Verified

Two docker tests delay the cancel on a test route so the race happens every time; both failed on the old code with the CI error and pass now, along with the cancellation and conformance cases.

Not in this PR

  • An editor session whose cancel is not confirmed within the grace period still keeps its backend; pooled connections are discarded in that case.

tokio-postgres returns from a cancel once the request is written. A read's
statement_timeout could end the statement first, and the server then
applied the late request to the next statement on that backend. Wait for
the server to close the cancel connection, as libpq does.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant