Repository navigation
Python: fix(redis): close an owned RedisContextProvider index on exit - #9528
Open
Yufeng He (he-yufeng) wants to merge 1 commit into
Open
Yufeng He (he-yufeng) wants to merge 1 commit into
Yufeng He (he-yufeng) wants to merge 1 commit into
Conversation
RedisContextProvider built its own AsyncSearchIndex from redis_url when none was injected, but its __aexit__ was empty and it had no aclose, so the index's connection pool was never released and providers created per session leaked sockets. Track ownership the same way RedisHistoryProvider does for its client (_owns_client + aclose): the provider now closes the index (redisvl disconnect) on aclose/__aexit__ only when it built the index itself; an injected index stays under the caller's control. Fixes microsoft#9509
Yufeng He (he-yufeng)
requested review from
SergeyMenshykh,
Tao Chen (TaoChenOSU),
Eduard van Valkenburg (eavanvalkenburg),
Giles Odigwe (giles17),
Jose Alvarez (jpalvarezl),
Evan Mattson (moonbox3),
Roger Barreto (rogerbarreto),
sophia-ramsey and
westey (westey-m)
as code owners
October 19, 2026 09:19
Yufeng He (he-yufeng)
deployed
to
github-app-auth
October 19, 2026 09:19 — with
GitHub Actions
Active
Yufeng He (he-yufeng)
deployed
to
github-app-auth
October 19, 2026 09:19 — with
GitHub Actions
Active
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused fix preserves caller ownership and covers the intended cleanup paths with regression tests.
No open issues 🎉
What changed in this PR
Fixes #9509 by releasing Redis connections owned by RedisContextProvider, while leaving injected indexes under caller control.
Changes:
- Tracks index ownership and adds
aclose(). - Calls cleanup when the async context manager exits.
- Adds regression tests for owned and borrowed indexes.
| File | Description |
|---|---|
python/packages/redis/tests/test_providers.py |
Tests explicit cleanup, borrowed-index preservation, and context-manager exit. |
python/packages/redis/agent_framework_redis/_context_provider.py |
Adds ownership-aware cleanup for the Redis index. |
Review effort: Balanced
This branch was successfully deployed
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.
Fixes #9509
Root cause
RedisContextProviderbuilds its ownAsyncSearchIndexfromredis_urlwhen no index is injected, but its__aexit__was empty and there was noaclose— so the connection pool behind that index was never released. Providers created and discarded per session leak open sockets.RedisHistoryProviderin the same package already closes a client it owns (_owns_client+aclose); the context provider was the outlier.Fix
Mirror the sibling's ownership rule:
_owns_index = redis_index is Nonerecorded at construction;aclose()disconnects the index (redisvlAsyncSearchIndex.disconnect()) only when owned — an injected index stays under the caller's control;__aexit__now callsaclose().Test plan
New
TestRedisContextProviderClose(next to the history provider's close tests), reusing the existingpatch_index_from_dict/mock_indexfixtures:disconnect()awaited onaclose();async withexit → owned index disconnected.On the unpatched code both owned-index tests fail (nothing to assert against fires); the borrowed-index test passes both ways, as it should. Verified locally with the redis + core packages from this branch on the repo venv: