"IndexError: string index out of range" on get_message from PubSub
Version: What redis-py and what redis version is the issue happening on? redis-py 5.2.0 with redis 7.2.4
Platform: What platform / version? Python 3.10.12 on Ubuntu 22.04
Description:
Hey everyone,
I have an application that uses Redis in a production environment, but the service crashed during a call to the get_message method from PubSub with the following error (unfortunately I am not able to reproduce the error after several attempts):
File "/usr/local/lib/python3.10/dist-packages/redis/client.py", line 1056, in get_message
return self.handle_message(response, ignore_subscribe_messages)
File "/usr/local/lib/python3.10/dist-packages/redis/client.py", line 1098, in handle_message
"data": response[2],
IndexError: string index out of range
Do you have any ideas on how this is possible?
Best regards, Jorge
Hi @jmspereira, can you please share more details about how you are initialising your client and using the pubsub? Some small code example would be great!
Hey @petyaslavova, thank you for the response!
This minimal example should illustrate how I am initializing the connection with redis and using the pubsub:
import socket
from redis import Sentinel
from redis.backoff import ExponentialBackoff
from redis.retry import Retry
from redis.exceptions import ConnectionError as RedisConnectionError
from redis.exceptions import TimeoutError as RedisTimeoutError
def main():
sentinel = Sentinel(
[('localhost', 26379), ('localhost', 26380), ('localhost', 26381)],
min_other_sentinels=0,
encoding="utf-8",
decode_responses=True,
socket_keepalive=True,
socket_timeout=30,
socket_connect_timeout=30,
health_check_interval=5,
retry_on_error=(RedisConnectionError, RedisTimeoutError, socket.timeout),
retry=Retry(ExponentialBackoff(10, 0.5), 5),
db=0,
)
connection = sentinel.master_for("mymaster")
connection.config_set("notify-keyspace-events", "KEA")
pub_sub = connection.pubsub()
while True:
event = pub_sub.get_message(timeout=1, ignore_subscribe_messages=True)
if event:
print(event)
else:
print("No event")
if __name__ == '__main__':
main()
Additional notes:
- I was not able to replicate the error using the minimal example;
- In the production environment where the error occurs it happens once every week;
- The redis database in the production environment has around ~40k clients, ~75k keys and is receiving ~4k commands/per second.