Repository navigation
Add context manager support to ConnectionPool - #4167
Merged
petyaslavova merged 3 commits intoJul 9, 2026
Merged
Conversation
Support using ConnectionPool (and BlockingConnectionPool, plus the async
equivalents) as a context manager so the pool's lifecycle can be managed
explicitly and safely:
with ConnectionPool.from_url(url) as pool:
r = Redis(connection_pool=pool)
This gives users a thread-safe idiom for sharing a single pool across
multiple clients, avoiding the footgun in Redis.from_pool(), which takes
exclusive ownership of the pool and disconnects all of its connections --
including ones in use by other clients/threads -- when the owning client
is closed or garbage-collected (issue redis#3669).
Also clarifies the from_pool() docstrings (sync and async) to document
that the pool must not be shared, and points to the context manager as
the supported way to share a pool.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Collaborator
|
Hi @sean-kim05, thank you for your contribution! I'll provide a review shortly. |
petyaslavova
requested changes
Jul 8, 2026
petyaslavova
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the contribution β the context-manager support and the from_pool docstring clarification are both welcome, and the sync/async parity is done correctly. Before merging, please address these points:
BlockingConnectionPoolcoverage: it inherits the new context-manager methods in both stacks, but there's no test for it. Please add atest_pool_context_managertoTestBlockingConnectionPoolintests/test_connection_pool.pyand the async equivalent intests/test_asyncio/test_connection_pool.py.- Return type of
__enter__/__aenter__: they're annotated as"ConnectionPool", sowith BlockingConnectionPool(...) as pool:typespoolas the base class. Please use a Self-style annotation via aTypeVarbound toConnectionPool(the async client does this with_RedisTinredis/asyncio/client.py), keeping sync and async annotations identical. - Docstring accuracy: in the new
from_pooldocstrings, please also state explicitly that the plain constructor,Redis(connection_pool=pool), does not take ownership of the pool β that's what makes the recommended example safe, and it's worth spelling out for readers.
With those covered, this should be ready for another review.
β¦r__ with Self Address review on redis#4167: - add test_pool_context_manager for BlockingConnectionPool (sync + async) - annotate __enter__/__aenter__ return type via the _CP TypeVar so subclasses type as themselves, not the ConnectionPool base - note in from_pool docstrings that Redis(connection_pool=pool) does not take ownership of the pool
Contributor
Author
|
Thanks for the review! Addressed all three:
|
12 of 55 tasks
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.
Description of change
Adds context manager support to
ConnectionPool/BlockingConnectionPool(and the async equivalents inredis.asyncio), so a pool's lifecycle can be managed explicitly and safely:Motivation β issue #3669 (
Redis.from_pool(...)is not thread safe).Redis.from_pool()setsauto_close_connection_pool = True, so when the client is closed or garbage-collected it callsconnection_pool.disconnect(), which defaults toinuse_connections=Trueand disconnects every connection in the pool β including connections currently checked out by other clients that share the same pool object. In a threaded server (e.g. FastAPI running sync routes in a threadpool) where a client is created per request from a shared pool, one request's client being GC'd tears down connections other in-flight requests are still using, producing errors likeValueError: I/O operation on closed filemid-healthcheck. The async client hits the same problem viaaclose().There isn't currently a first-class, safe idiom for sharing one pool across multiple clients. This PR adds the context-manager protocol the issue author suggested, and clarifies the
from_pool()docstrings (sync + async) to document that the returned client takes exclusive ownership of the pool and that the pool must not be shared β pointing users at the context manager instead.This change is purely additive: no existing behavior changes.
Pull Request check-list
ruff check,ruff format --check, and the new tests pass locally)test_pool_context_managerin sync and async pool test suites)from_pooldocstrings)Closes #3669
Note
Low Risk
Additive API and documentation only; existing pool/client behavior is unchanged aside from optional context-manager cleanup.
Overview
ConnectionPool lifecycle: Sync and async
ConnectionPool(including blocking variants via inheritance) now supportwith/async with, exiting by callingclose()/aclose()and disconnecting pooled connectionsβincluding those still checked out in the new tests.Safer shared-pool guidance:
Redis.from_pooldocstrings (sync and asyncio) now state that the client owns the pool and must not be shared across multiple clients, and point toRedis(connection_pool=pool)plus explicit pool managementβdocumented with aConnectionPool.from_urlcontext-manager exampleβfor sharing one pool.Tests: New
test_pool_context_managercases in sync and async pool test suites verify disconnect-on-exit behavior.Reviewed by Cursor Bugbot for commit b96e110. Bugbot is set up for automated code reviews on this repo. Configure here.