fix(pgxpool): treat MaxConnLifetime 0 as unlimited in expiry check#2574
Open
AurelienPillevesse wants to merge 1 commit into
Open
fix(pgxpool): treat MaxConnLifetime 0 as unlimited in expiry check#2574AurelienPillevesse wants to merge 1 commit into
AurelienPillevesse wants to merge 1 commit into
Conversation
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.
Problem
When
MaxConnLifetime == 0(whose documented and conventional meaning is "unlimited lifetime", consistent withdatabase/sql.SetConnMaxLifetime(0)),Pool.Acquirealways fails with:Instead of returning a connection, it repeatedly creates and destroys connections until the retry limit is reached.
Root Cause
Commit
20578f57("feat: check if conn is expired before acquire") introduced anisExpiredcheck inAcquire.At connection creation time,
maxAgeTimeis computed as:When
MaxConnLifetime == 0,maxAgeTimeis effectively the connection creation timestamp. As a result, the expiration check:becomes true almost immediately.
Consequently, every newly created connection is considered expired as soon as it is acquired.
Acquiredestroys the connection, creates a new one, and repeats this process up tomaxConns + 1times before eventually returning:The same
isExpiredlogic is also used by the background health checker (checkConnsHealth), causing idle connections to be destroyed immediately as well. This behavior contradicts the expected semantics ofMaxConnLifetime == 0, which should represent an unlimited lifetime.Fix
Treat non-positive values of
MaxConnLifetimeas meaning "unlimited" by guarding theisExpiredfunction, which is the single source of truth used by bothAcquireandcheckConnsHealth.With this change, connections are never considered expired due to age when
MaxConnLifetime <= 0, restoring the behavior that existed before the regression and ensuring thatMaxConnLifetime == 0consistently means unlimited lifetime.