Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"IndexError: string index out of range" on get_message from PubSub #3444

Open
jmspereira opened this issue Nov 27, 2024 · 2 comments
Open

"IndexError: string index out of range" on get_message from PubSub #3444

jmspereira opened this issue Nov 27, 2024 · 2 comments
Assignees

Comments

@jmspereira
Copy link

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

@petyaslavova
Copy link
Collaborator

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!

@petyaslavova petyaslavova self-assigned this Mar 12, 2025
@jmspereira
Copy link
Author

jmspereira commented Mar 14, 2025

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.

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

No branches or pull requests

2 participants