Skip to content

Improve Redis connection pool recovery after Redis outages#22481

Open
makhdoombilawal wants to merge 3 commits into
PrefectHQ:mainfrom
makhdoombilawal:redis-recovery
Open

Improve Redis connection pool recovery after Redis outages#22481
makhdoombilawal wants to merge 3 commits into
PrefectHQ:mainfrom
makhdoombilawal:redis-recovery

Conversation

@makhdoombilawal

Copy link
Copy Markdown

Summary

This PR improves Redis connection recovery in the prefect-redis integration after temporary Redis outages.

Changes

  • Properly disconnect and close cached Redis clients before clearing the client cache.
  • Improve recovery by handling socket-level (OSError) failures in the consumer retry loop.
  • Add regression tests covering Redis client cleanup and connection recovery scenarios.

Motivation

When Redis becomes unavailable, cached clients can remain in a broken state, preventing automatic recovery and requiring a process restart. These changes improve cleanup and retry behavior to make recovery more reliable.

Testing

  • Added regression tests for client cleanup.
  • Added tests covering retry behavior during connection failures.
  • Ran the existing prefect-redis test suite successfully.

Copilot AI review requested due to automatic review settings July 11, 2026 23:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR strengthens the prefect-redis integration’s ability to recover from temporary Redis outages by ensuring cached Redis clients are properly torn down and by broadening the consumer retry behavior to include socket-level failures.

Changes:

  • Add explicit connection-pool disconnect + client close steps before clearing the cached Redis client map.
  • Treat OSError as a retryable connection failure in the Redis consumer loop (in addition to RedisError).
  • Add regression tests for cached-client cleanup and consumer retry behavior during connection failures.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/integrations/prefect-redis/prefect_redis/client.py Adds best-effort disconnect/close of cached clients before clearing the cache to avoid “stuck” broken connections.
src/integrations/prefect-redis/prefect_redis/messaging.py Broadens retry handling to include OSError and tightens a few type hints to match server messaging protocols.
src/integrations/prefect-redis/tests/test_connection_recovery.py New tests covering client cleanup and consumer retry on OSError/connection failures.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1 to +7
import asyncio
from unittest.mock import AsyncMock, MagicMock, patch
import pytest

from prefect_redis.client import _client_cache, clear_cached_clients
from prefect_redis.messaging import Consumer
from prefect.server.utilities.messaging import StopConsumer
Comment on lines +9 to +18
@pytest.mark.asyncio
async def test_clear_cached_clients_disconnects_pools():
mock_client = MagicMock()
mock_client.connection_pool = AsyncMock()
mock_client.aclose = AsyncMock()

mock_loop = asyncio.get_running_loop()
cache_key = (MagicMock(), (), (), mock_loop)

_client_cache[cache_key] = mock_client

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 07b769351e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

continue
try:
if hasattr(client, "connection_pool") and client.connection_pool:
await client.connection_pool.disconnect()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clear cache before awaiting client shutdown

During recovery the cache remains populated while disconnect()/aclose() are awaited, so any concurrent publisher/consumer that calls get_async_redis_client() during this window will be handed the same stale client that is being disconnected instead of a fresh one. This regresses the recovery path under concurrent Redis users; snapshot the clients and clear _client_cache before awaiting shutdown so future lookups immediately create new connections.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants