Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Fix health check after unsubscribe #1207

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGES/1207.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix #1206 health check message after unsubscribing
13 changes: 8 additions & 5 deletions aioredis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3937,16 +3937,14 @@ def __init__(
# we need to know the encoding options for this connection in order
# to lookup channel and pattern names for callback handlers.
self.encoder = self.connection_pool.get_encoder()
self.health_check_message_b = self.encoder.encode(self.HEALTH_CHECK_MESSAGE)
if self.encoder.decode_responses:
self.health_check_response: Iterable[Union[str, bytes]] = [
"pong",
self.HEALTH_CHECK_MESSAGE,
]
else:
self.health_check_response = [
b"pong",
self.encoder.encode(self.HEALTH_CHECK_MESSAGE),
]
self.health_check_response = [b"pong", self.health_check_message_b]
self.channels: Dict[ChannelT, PubSubHandler] = {}
self.pending_unsubscribe_channels: Set[ChannelT] = set()
self.patterns: Dict[ChannelT, PubSubHandler] = {}
Expand Down Expand Up @@ -4049,7 +4047,12 @@ async def parse_response(self, block: bool = True, timeout: float = 0):
return None
response = await self._execute(conn, conn.read_response)

if conn.health_check_interval and response == self.health_check_response:
# The response depends on whether there were any subscriptions
# active at the time the PING was issued.
if conn.health_check_interval and response in (
self.health_check_response, # If there was at least one subscription
self.health_check_message_b, # If there wasn't
):
# ignore the health check message as user might not expect it
return None
return response
Expand Down