Skip to content

Python: fix(redis): close an owned RedisContextProvider index on exit - #9528

Open
Yufeng He (he-yufeng) wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/redis-context-provider-close
Open

Yufeng He (he-yufeng) wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/redis-context-provider-close

Conversation

@he-yufeng

Copy link
Copy Markdown
Contributor

Fixes #9509

Root cause

RedisContextProvider builds its own AsyncSearchIndex from redis_url when no index is injected, but its __aexit__ was empty and there was no aclose — so the connection pool behind that index was never released. Providers created and discarded per session leak open sockets. RedisHistoryProvider in 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 None recorded at construction;
  • new aclose() disconnects the index (redisvl AsyncSearchIndex.disconnect()) only when owned — an injected index stays under the caller's control;
  • __aexit__ now calls aclose().

Test plan

New TestRedisContextProviderClose (next to the history provider's close tests), reusing the existing patch_index_from_dict/mock_index fixtures:

  • owned index → disconnect() awaited on aclose();
  • injected index → not disconnected;
  • async with exit → 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:

python -m pytest packages/redis/tests/     # 328 passed, 48 skipped
ruff check / ruff format --check           # clean

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

1 active deployment
github-app-auth — 95ac1bc6 Deployed Oct 19, 2026 by he-yufeng via team_check #7049
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: RedisContextProvider never closes the index connection it creates

2 participants