Repository navigation
Conversation
Pool.Acquire pings a conn that has been idle long enough per ShouldPing.
The ping was wired to the caller's context, so a cancellation mid-ping
tripped pgconn's context watcher, broke the socket, failed the ping, and
res.Destroy() took out a connection the pool had no reason to lose. The
same thing happened when the caller's context was already cancelled by
the time we got here: Ping returned immediately and the untouched
connection was destroyed. In code that fires parallel short queries and
cancels the losers this produced a steady stream of reconnects that
Stat()'s destroy counters did not record (only NewConnsCount went up).
Fix in two guards and a context detach:
1. If ctx is already cancelled before the ping, release the resource
and return ctx.Err() instead of pinging. The ping would fail anyway
and we would blame the connection.
2. Run the ping on context.WithoutCancel(ctx) (still bounded by
pingTimeout when configured) so a cancellation mid-ping cannot
reach into pgconn and tear down the connection.
3. After a successful ping, re-check ctx.Err(). The caller may have
cancelled while the ping was in flight; the connection is healthy
so release it rather than destroy it, and surface the caller's
error.
Behaviour for genuine ping failures (unreachable backend, auth error,
etc.) is unchanged: the connection is still destroyed and the acquire
loop retries. The caller still gets context.Canceled on cancel.
Regression test TestPoolAcquirePingDoesNotDestroyOnCallerCancel runs
with MaxConns=1 and ShouldPing: true, cancels the acquire context, and
asserts NewConnsCount is unchanged and the backend PID is the same on
the next acquire.
Fixes jackc#2680.
piyush295
force-pushed
the
fix/pool-ping-caller-cancel
branch
from
October 16, 2026 09:26
fdbd070 to
11a9732
Compare
Owner
|
Closed in favor of #2681 |
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.
Bug (fixes #2680)
Pool.Acquirepings connections that have been idle longer thanShouldPingsays. The ping was wired to the caller's context, so cancellation mid-ping tripped pgconn's context watcher, broke the socket, failed the ping, andres.Destroy()took out a connection the pool had no reason to lose. The same thing happened when the caller's context was already cancelled before we got here:Pingreturned immediately and the untouched connection was destroyed.In code that fires parallel short queries and cancels the losers once one branch answers (an OpenFGA-style authorization server was the reported case) this produced a steady stream of reconnects that none of the destroy counters recorded (only
NewConnsCountwent up).Fix (three guards)
ctxis already cancelled before the ping, release the resource and returnctx.Err()instead of pinging. The ping would fail anyway and we would blame the connection.context.WithoutCancel(ctx)(still bounded bypingTimeoutwhen configured) so a cancel mid-ping cannot reach into pgconn and tear down the connection.ctx.Err(). The caller may have cancelled while the ping was in flight; the connection is healthy so release it rather than destroy it, and surface the caller's error.Behaviour for genuine ping failures (unreachable backend, auth error, etc.) is unchanged: the connection is still destroyed and the acquire loop retries. The caller still gets
context.Canceled.Test
TestPoolAcquirePingDoesNotDestroyOnCallerCancelruns withMaxConns=1andShouldPing: true, cancels the acquire context, and asserts thatNewConnsCountis unchanged and the backend PID is the same on the next acquire. The test needsPGX_TEST_DATABASEjust like the other pool tests.go build ./...andgo vet ./pgxpool/are clean.