Fix deadlock in pgxpool under concurrent context cancellation - #2
Open
zhuyh1606-oss wants to merge 1 commit into
Open
Fix deadlock in pgxpool under concurrent context cancellation#2zhuyh1606-oss wants to merge 1 commit into
zhuyh1606-oss wants to merge 1 commit into
Conversation
Fixes concurrent context cancellation + semaphore contention causing pool to livelock. Two-part fix: 1. puddle/pool.go acquire(): check ctx.Done() after semaphore acquire to prevent goroutine leak when context is canceled between TryAcquire success and resource creation. 2. pgxpool/pool.go Acquire(): check ctx.Err() between retry iterations so a cancelled context terminates the retry loop immediately instead of cycling through all maxConns attempts. Includes regression test TestPoolAcquireStressCancelContention that exercises high-concurrency acquire with rapid context cancellation.
|
😅 Unfortunately there are no rewards left to claim in this issue! |
Author
|
Hi @jaasielitaigq, any chance to review this PR? The fix addresses the deadlock in pgxpool under context cancellation with a regression test. Also noticed the Opire reward needs to be + minimum — if you'd like to set it up, I can re-claim the issue. |
Author
|
Hi @jaasielitaigq, just following up on this PR. It's been open for a while and is ready for merge. Could you please review and merge when you have time? Thanks! |
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.
/claim #1
Problem
Under high-concurrency with rapid context cancellation, pgxpool can deadlock/livelock when:
Fix
Two-part fix:
puddle/pool.go acquire()
Added ctx.Done() check after semaphore acquire succeeds but before resource creation. If the context was cancelled between TryAcquire and the check, the semaphore is released immediately instead of starting a doomed constructor goroutine.
pgxpool/pool.go Acquire()
Added ctx.Err() check in the retry loop after Destroy() calls for expired/unhealthy connections. This terminates the retry loop immediately when context is cancelled, instead of cycling through all maxConns+1 attempts.
Testing
Added TestPoolAcquireStressCancelContention - runs 50 goroutines each acquiring with tight context cancellation (5ms timeout), exercising the semaphore contention + cancel path.