Skip to content

Fix cache proxy connection unbalanced command send and read#3908

Open
tzongw wants to merge 5 commits intoredis:masterfrom
tzongw:fix-cache-proxy-connection
Open

Fix cache proxy connection unbalanced command send and read#3908
tzongw wants to merge 5 commits intoredis:masterfrom
tzongw:fix-cache-proxy-connection

Conversation

@tzongw
Copy link
Contributor

@tzongw tzongw commented Jan 9, 2026

Description of change

Fix CacheProxyConnection race conditions between send_command and read_response, as test cases described, covers issue #3600.

Test cases

  1. cache entry be in progress at all time.
  2. cache entry exitsts at first but gone later.
  3. cache entry not exists at first but filled later.

These cause unbalanced command send and read and the connection will hang forever or read a mismatched response.

@jit-ci
Copy link

jit-ci bot commented Jan 9, 2026

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@petyaslavova
Copy link
Collaborator

Hi @tzongw, thank you for your contribution! Can you please check test failures?

Copy link
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 PR fixes race conditions in CacheProxyConnection between send_command and read_response methods by changing from tracking cache keys to tracking cache entry objects. The core improvement is storing a reference to the entire CacheEntry object instead of just the cache key, which enables proper identity checking and prevents unbalanced command send/read operations.

Key changes:

  • Renamed _current_command_cache_key to _current_command_cache_entry to store full entry references
  • Added explicit status checks for CacheEntryStatus.VALID vs CacheEntryStatus.IN_PROGRESS to handle concurrent cache operations
  • Implemented identity-based comparison (is) to detect when cache entries are replaced by other threads

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
redis/connection.py Core fix: Changed from tracking cache keys to cache entry objects, added explicit cache entry status validation, and implemented identity checks to detect entry replacement during concurrent operations
tests/test_connection.py Updated existing test for variable rename; added three comprehensive test cases covering cache entry in-progress, entry disappearing between send/read, and entry appearing between send/read scenarios

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

Comment on lines 1478 to 1479
# Check if entry still exists.
if self._cache.get(cache_key) is not None:
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

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

The check at line 1479 only verifies that some entry exists in the cache, but doesn't verify it's the same entry object as cache_entry. If the entry was invalidated and a new entry was created by another thread between lines 1477 and 1479, the condition would pass but line 1480 would store a reference to the old invalidated entry. This could lead to using stale cached data. The check should verify the entry identity: if self._cache.get(cache_key) is cache_entry:

Suggested change
# Check if entry still exists.
if self._cache.get(cache_key) is not None:
# Check if the same entry still exists in the cache.
if self._cache.get(cache_key) is cache_entry:

Copilot uses AI. Check for mistakes.
@tzongw
Copy link
Contributor Author

tzongw commented Jan 9, 2026

Hi @tzongw, thank you for your contribution! Can you please check test failures?

@petyaslavova updated

@tzongw tzongw force-pushed the fix-cache-proxy-connection branch from 54ccb16 to 6c76951 Compare January 10, 2026 13:07
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