{{ message }}
Pool: prevent trimming the last idle connection under load#1271
Open
mamfyou wants to merge 6 commits into
Open
Pool: prevent trimming the last idle connection under load#1271mamfyou wants to merge 6 commits into
mamfyou wants to merge 6 commits into
Conversation
added 4 commits
August 18, 2025 19:42
Previously, the inactivity timer could terminate idle connections even when doing so left the pool effectively empty. Under heavy load this forced the pool to create new connections, causing extra overhead and occasional TimeoutErrors during acquire(). This change adds a guard in PoolConnectionHolder so that idle deactivation only happens when it is safe: - never below pool min_size - never if there are waiters - never removing the last idle connection This ensures the pool retains at least one ready connection and avoids spurious connection churn under load.
Previously, the inactivity timer could terminate idle connections even when doing so left the pool effectively empty. Under heavy load or after inactivity for a few minutes, this forced the pool to create new connections, causing extra overhead and occasional TimeoutErrors during acquire(). This change adds a guard in PoolConnectionHolder so that idle deactivation only happens when it is safe: - never below pool min_size - never if there are waiters - never removing the last idle connection This ensures the pool retains at least one ready connection and avoids spurious connection after minutes of inactivity or heavy loads.
…fix-empty-connection-pool
Previously, the inactivity timer could terminate idle connections even when doing so left the pool effectively empty. Under heavy load or after inactivity for a few minutes, this forced the pool to create new connections, causing extra overhead and occasional TimeoutErrors during acquire(). This change adds a guard in PoolConnectionHolder so that idle deactivation only happens when it is safe: - never below pool min_size - never if there are waiters - never removing the last idle connection This ensures the pool retains at least one ready connection and avoids spurious connection after minutes of inactivity or heavy loads.
elprans
reviewed
Oct 11, 2025
Member
There was a problem hiding this comment.
These attributes are basically always available, so getattr is superfluous, just access the attributes directly.
elprans
reviewed
Oct 11, 2025
| waiters = 0 | ||
|
|
||
| # Include tasks currently in the process of acquiring. | ||
| waiters += int(getattr(pool, "_acquiring", 0) or 0) |
Member
There was a problem hiding this comment.
Except this one does not seem to actually exist?
Member
There was a problem hiding this comment.
Let's add that and it'll let us to drop the queue._getters fiddling above.
elprans
reviewed
Oct 11, 2025
| self._inactive_callback.cancel() | ||
| self._inactive_callback = None | ||
|
|
||
| def _can_deactivate_inactive_connection(self) -> bool: |
Member
There was a problem hiding this comment.
It would probably make sense to move the method to the Pool class given how much we fiddle with internals here.
| waiters = 0 | ||
|
|
||
| # Include tasks currently in the process of acquiring. | ||
| waiters += int(getattr(pool, "_acquiring", 0) or 0) |
Member
There was a problem hiding this comment.
Let's add that and it'll let us to drop the queue._getters fiddling above.
Author
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.

Related To #1268
Under heavy load, or when max_inactive_connection_lifetime exceeds the pool’s configured connection lifetime (like after inactivity for few minutes when lifetime is set to 300),
the inactivity timer may deactivate idle connections too aggressively.
In rare cases this leaves the pool empty, forcing new connections to be created just as clients are waiting. Since connection setup is expensive, this can result in extra overhead and even TimeoutErrors when acquiring a connection.
This change adds a guard to PoolConnectionHolder._deactivate_inactive_connection so that idle deactivation only happens when it is safe:
1- never below min_size
2- never if there are waiters in the pool queue
3- never removing the last idle connection
With these rules, the pool always keeps at least one ready connection, avoiding spurious churn and reducing the risk of TimeoutErrors under load.
The implementation only uses existing private fields (_holders, _queue, _minsize, etc.), so it doesn’t introduce new API surface.
This makes the pool’s behavior more predictable under load without changing existing configuration knobs.