Skip to content

pgconn: do not report a failed pipeline flush as safe to retry - #2717

Merged
jackc merged 1 commit into
jackc:masterfrom
hcrosse:pipeline-flush-safe-to-retry
Nov 14, 2026
Merged

jackc merged 1 commit into
jackc:masterfrom
hcrosse:pipeline-flush-safe-to-retry

Conversation

@hcrosse

@hcrosse hcrosse commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

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. GetResults and Close return 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 AfterNetConnect to fail exactly one Write, covering both cases. They check that Sync, GetResults and Close report SafeToRetry false. 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 vet output 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.

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
Copilot AI balanced review requested due to automatic review settings November 14, 2026 04:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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, and Close report 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

@jackc
jackc merged commit b38eee8 into jackc:master Nov 14, 2026
22 checks passed
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.

pgconn: pipeline reports a committed request's error as safe to retry after a failed Flush

3 participants