Skip to content

Fix Redis client lifecycle to prevent connection leaks in concurrent requests#653

Merged
codingjoe merged 2 commits intoissues/643/redis-clientfrom
copilot/sub-pr-651
Feb 18, 2026
Merged

Fix Redis client lifecycle to prevent connection leaks in concurrent requests#653
codingjoe merged 2 commits intoissues/643/redis-clientfrom
copilot/sub-pr-651

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 18, 2026

Redis health check reused a single client instance across multiple requests, causing connection leaks and race conditions when the client was closed after each check.

Changes:

  • Client lifecycle: Create fresh client per request in run() method instead of __post_init__, matching RabbitMQ/Kafka pattern
  • Factory pattern: Add client_factory parameter that's called per health check request
  • Deprecation handling: Preserve backward compatibility—user-provided clients are not closed by the health check during deprecation period
  • Validation: Require exactly one of client or client_factory with clear error messages in __post_init__

Before:

# Client created once, reused and closed after each check
Redis(client=RedisClient.from_url('redis://localhost:6379'))

After:

# Factory creates new client per request
Redis(client_factory=lambda: RedisClient.from_url('redis://localhost:6379'))

The health check instance can now safely handle concurrent requests without client reuse issues.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Create client per request in run() method, not in __post_init__
- Add validation requiring exactly one of client or client_factory
- Only close factory-created clients, not user-provided ones
- Move deprecation warning to __post_init__ for early feedback
- Update tests to verify new behavior and lifecycle management

Co-authored-by: codingjoe <1772890+codingjoe@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 18, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 192.0.2.1
    • Triggering command: REDACTED, pid is -1 (packet block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Fix connection leakage by adding client factory to Redis check Fix Redis client lifecycle to prevent connection leaks in concurrent requests Feb 18, 2026
Copilot AI requested a review from codingjoe February 18, 2026 10:41
@codingjoe codingjoe marked this pull request as ready for review February 18, 2026 12:31
Copilot AI review requested due to automatic review settings February 18, 2026 12:31
@codingjoe codingjoe merged commit 067351c into issues/643/redis-client Feb 18, 2026
29 checks passed
@codingjoe codingjoe deleted the copilot/sub-pr-651 branch February 18, 2026 12:31
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (2fa30c8) to head (3227a8f).
⚠️ Report is 1 commits behind head on issues/643/redis-client.

Additional details and impacted files
@@                     Coverage Diff                      @@
##           issues/643/redis-client      #653      +/-   ##
============================================================
+ Coverage                    95.88%   100.00%   +4.11%     
============================================================
  Files                           13        13              
  Lines                          729       737       +8     
============================================================
+ Hits                           699       737      +38     
+ Misses                          30         0      -30     
Flag Coverage Δ
python-3.10-django-5.2 56.71% <0.00%> (+5.13%) ⬆️
python-3.10-django-5.2-celery ?
python-3.10-django-5.2-kafka ?
python-3.10-django-5.2-psutil ?
python-3.10-django-5.2-rabbitmq ?
python-3.10-django-5.2-rss ?
python-3.11-django-5.2 56.71% <0.00%> (+5.13%) ⬆️
python-3.12-django-5.2 51.01% <0.00%> (-0.56%) ⬇️
python-3.12-django-6.0 51.01% <0.00%> (-0.56%) ⬇️
python-3.13-django-5.2 51.01% <0.00%> (-0.56%) ⬇️
python-3.13-django-5.2-celery 56.17% <0.00%> (?)
python-3.13-django-5.2-kafka 54.13% <0.00%> (?)
python-3.13-django-5.2-psutil 62.95% <0.00%> (?)
python-3.13-django-5.2-rabbitmq 53.86% <0.00%> (?)
python-3.13-django-5.2-redis 55.90% <100.00%> (?)
python-3.13-django-5.2-rss 71.64% <0.00%> (?)
python-3.13-django-6.0 51.01% <0.00%> (-0.56%) ⬇️
python-3.14-django-5.2 51.29% <0.00%> (-0.57%) ⬇️
python-3.14-django-5.2-celery ?
python-3.14-django-5.2-kafka ?
python-3.14-django-5.2-psutil ?
python-3.14-django-5.2-rabbitmq ?
python-3.14-django-5.2-rss ?
python-3.14-django-6.0 51.29% <0.00%> (-0.57%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a critical bug in the Redis health check where a single client instance was reused across multiple requests, causing connection leaks and race conditions when the client was closed after each check. The fix aligns the Redis health check with the patterns already established in RabbitMQ and Kafka health checks.

Changes:

  • Moved client creation from __post_init__ to the run() method to create fresh clients per request
  • Added client_factory parameter that's called per health check request (similar to RabbitMQ/Kafka patterns)
  • Added parameter validation in __post_init__ to ensure exactly one of client or client_factory is provided
  • Implemented proper deprecation handling where user-provided clients are not closed during the deprecation period
  • Added comprehensive test coverage for all lifecycle and validation scenarios

Reviewed changes

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

File Description
health_check/contrib/redis.py Implements factory pattern for client creation per request, adds validation logic in __post_init__, and ensures proper lifecycle management with should_close flag
tests/contrib/test_redis.py Updates and adds tests to verify factory is called per request, user-provided clients aren't closed, validation works correctly, and each client is properly closed after use

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.

3 participants