Repository navigation
Python: fix(redis): close an owned RedisContextProvider index on exit - #9528
Yufeng He (he-yufeng) wants to merge 2 commits into
Conversation
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
… of failing every turn MemoryFileStore.read_state parsed state.json without a guard, so a corrupt state file (interrupted editor, partial external write) raised out of the per-turn memory bookkeeping path forever. Move the unreadable file aside (the session store's corrupt-snapshot convention) and restart from a clean state; the next write recreates the file.
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
95ac1bc to
a4f739b
Compare
Eduard van Valkenburg (eavanvalkenburg)
left a comment
There was a problem hiding this comment.
The RedisContextProvider ownership fix itself is focused and the cleanup semantics look correct, but this PR currently also includes the unrelated memory-state quarantine commit (68bc218). Please rebase onto current main and retain only the Redis cleanup (a4f739b, plus any conflict-only adjustments) so the PR returns to the intended two-file scope, then rerun CI.
|
Closing in favor of #9510, which predates this PR, links the same issue, and contains the focused two-file ownership fix on a clean branch. Thanks for the alternative implementation and tests. |
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: