Skip to content

fix(pgxpool): handle context cancellation race during acquire to prevent deadlocks - #8

Open
parastejpal987-cmyk wants to merge 1 commit into
jaasielitaigq:mainfrom
parastejpal987-cmyk:fix/pgxpool-deadlock
Open

fix(pgxpool): handle context cancellation race during acquire to prevent deadlocks#8
parastejpal987-cmyk wants to merge 1 commit into
jaasielitaigq:mainfrom
parastejpal987-cmyk:fix/pgxpool-deadlock

Conversation

@parastejpal987-cmyk

Copy link
Copy Markdown

Description

This PR fixes a severe connection pool drain (deadlock) that occurs under extreme concurrency when a waiting Acquire(ctx) call has its context canceled at the exact millisecond a connection is handed off to it.

Previously, if p.p.Acquire(ctx) returned successfully but the caller's context had just been canceled, the connection was returned to the caller along with the context error, effectively leaking the connection since the caller assumes failure. Over time, these phantom connections drain the pool capacity to zero, resulting in a permanent deadlock for all future Acquire calls.

Fix

Added a safety check in pgxpool/pool.go immediately after acquiring the underlying pool connection. If ctx.Err() != nil at that exact moment, the connection is safely released back into the pool via res.Release().

Related Issue

Resolves the pgxpool deadlock/context cancellation bug.

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.

1 participant