Skip to content

pgxpool: don't destroy a healthy connection on caller cancel during ping - #2685

Closed
piyush295 wants to merge 1 commit into
jackc:masterfrom
piyush295:fix/pool-ping-caller-cancel
Closed

piyush295 wants to merge 1 commit into
jackc:masterfrom
piyush295:fix/pool-ping-caller-cancel

Conversation

@piyush295

Copy link
Copy Markdown

Bug (fixes #2680)

Pool.Acquire pings connections that have been idle longer than ShouldPing says. The ping was wired to the caller's context, so 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 before we got here: Ping returned 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 NewConnsCount went up).

Fix (three guards)

  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 cancel 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.

Test

TestPoolAcquirePingDoesNotDestroyOnCallerCancel runs with MaxConns=1 and ShouldPing: true, cancels the acquire context, and asserts that NewConnsCount is unchanged and the backend PID is the same on the next acquire. The test needs PGX_TEST_DATABASE just like the other pool tests.

go build ./... and go vet ./pgxpool/ are clean.

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
piyush295 force-pushed the fix/pool-ping-caller-cancel branch from fdbd070 to 11a9732 Compare October 16, 2026 09:26
@jackc

jackc commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Closed in favor of #2681

@jackc jackc closed this Oct 17, 2026
@piyush295
piyush295 deleted the fix/pool-ping-caller-cancel branch October 18, 2026 04:27
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.

pgxpool: Acquire destroys a healthy idle connection when the caller's context is cancelled during the liveness ping

2 participants