Skip to content

pgconn: do not send cancel request without BackendKeyData - #2727

Open
tony1233200 wants to merge 1 commit into
jackc:masterfrom
tony1233200:fix-2726-no-cancel-without-key
Open

tony1233200 wants to merge 1 commit into
jackc:masterfrom
tony1233200:fix-2726-no-cancel-without-key

Conversation

@tony1233200

Copy link
Copy Markdown

Fixes #2726.

When a server completes startup without sending BackendKeyData, pgConn.secretKey is empty and a cancel request cannot match anything on the server: the server matches a cancel by PID and secret key, and under protocol 3.0 a cancel request is exactly 16 bytes. CancelRequest still dialled the server and sent a 12-byte, key-less message, and asyncClose did the same on every interrupted query of such a connection.

  • CancelRequest now returns an error (no cancel key: server sent no BackendKeyData) before dialling when no secret key was received.
  • asyncClose only sends the cancel request when a key is present, extending the sendCancelRequest gate added by pgconn: do not send cancel request when connect fails #2715 for connections still starting up.

A connection to a server that does send BackendKeyData, as PostgreSQL always does, is unaffected. The new test TestNoCancelRequestWithoutBackendKeyData mirrors TestConnectFailureDoesNotSendCancelRequest: a fake server completes startup without BackendKeyData, then the test asserts an explicit CancelRequest fails without a second dial, and that an interrupted query goes through asyncClose without one either. The test fails on the parent commit (the explicit cancel dials the server) and passes with this change.

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.

CancelRequest sends a 12-byte, key-less message when the server sent no BackendKeyData

2 participants