Repository navigation
pgconn: do not report a failed pipeline flush as safe to retry - #2717
Merged
Merged
Conversation
If Pipeline.Flush fails while an earlier flush has already sent a request whose results were not fully read, the server may have executed it. The write error was still reported with SafeToRetry true, and GetResults and Close returned it, so a caller that retries on SafeToRetry could run a committed request twice. This includes a request whose ResultReader was returned but not consumed, which is no longer in the pipeline's request queue. Wrap the error with safeToRetry false in those cases. Fixes jackc#2716
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the pipeline flush retry-safety change.
No open issues 🎉
What changed in this PR
This PR addresses #2716 by preventing pipeline flush errors from reporting retries as safe when earlier requests may already have executed, avoiding duplicate operations.
Changes:
- Mark flush errors as unsafe to retry when previously sent requests have unread or partially consumed results.
- Add fault-injection tests checking that
Sync,GetResults, andClosereport unsafe retries in both cases.
| File | Description |
|---|---|
pgconn/pgconn.go |
Detect outstanding sent requests and unfinished results when determining flush retry safety. |
pgconn/pgconn_test.go |
Add a one-write failure helper and regression tests for both unread-result cases. |
Review effort: Balanced
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.
A pipeline flush can fail after earlier requests have already reached the server, yet the error can still say retrying is safe. A caller that retries can run those requests twice.
GetResultsandClosereturn the same error.The fix marks a flush error as not safe to retry when an earlier request has results the caller hasn't finished reading. It covers both results not yet handed to the caller and results the caller has started reading but not finished. That earlier request may have committed even if the latest write sent no bytes.
Two subtests use
AfterNetConnectto fail exactly oneWrite, covering both cases. They check thatSync,GetResultsandClosereportSafeToRetryfalse. On PostgreSQL 18, each passes 10/10 with the fix and fails 10/10 without its part of the fix.go test -race ./pgconn/...passes.go vetoutput is identical to master's, including its existing unkeyed-field diagnostics.AI proposal: the analysis, reproducer and patch were produced by AI coding agents in OpenCode (Claude, Anthropic) during a simulation-testing hunt. Its brief asked them to look for retry-safety, cancellation and pool-lifecycle bugs in pgx and to confirm findings against real servers.
Fixes #2716.