Add CancelAndDrainContextWatcherHandler#2537
Open
sean- wants to merge 1 commit into
Open
Conversation
0d36e97 to
5efb321
Compare
Replace the racy 100ms sleep added in 93a5797 with a deterministic single-";" drain that absorbs any pending SQLSTATE 57014 (query_canceled) before the connection is reused. Key changes: - Add CancelAndDrainContextWatcherHandler implementing ctxwatch.Handler. HandleUnwatchAfterCancel sends exactly one ";" after a successful cancel, sufficient because PostgreSQL's QueryCancelPending is a single flag, not a queue. - Add mutex-guarded three-state cancel machine (idle/inFlight/sent) on CancelRequest. This prevents double-send from concurrent callers (ctxwatch, asyncClose, direct user code) which would produce multiple 57014 responses that a single drain cannot reconcile. - Fix data race on pid and secretKey in CancelRequest by grouping both into a backendKeyData struct behind atomic.Pointer. - Extract cancel protocol constants (cancelRequestCode, negotiateSSLCode, packet field offsets) from magic numbers, referencing CancelRequestPacket in src/include/libpq/pqcomm.h. AI: Claude Code (Anthropic) used for drafting and code generation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace the racy 100ms sleep added in 93a5797 with a deterministic single-";" drain that absorbs any pending SQLSTATE
57014(query_canceled) before the connection is reused.Key changes:
Add
CancelAndDrainContextWatcherHandlerimplementingctxwatch.Handler.HandleUnwatchAfterCancelsends exactly one";"after a successful cancel: sufficient because PostgreSQL'sQueryCancelPendingis a single flag, not a queue.Add mutex-guarded three-state cancel machine (idle/inFlight/sent) on
CancelRequest. This prevents double-send from concurrent callers (ctxwatch,asyncClose, direct user code) which would produce multiple57014responses that a single drain cannot reconcile.Fix data race on pid and secretKey in
CancelRequestby grouping both into abackendKeyDatastruct behindatomic.Pointer.Extract cancel protocol constants (
cancelRequestCode,negotiateSSLCode, packet field offsets) from magic numbers, referencingCancelRequestPacketinsrc/include/libpq/pqcomm.h.AI disclosure
Per the contributing guidelines: Claude Code (Anthropic, Opus 4.6) was used for drafting and code generation. I understand the code, traced the protocol flows through the PostgreSQL source, and can answer questions about the change directly.
Fixes: #2534