Skip to content

exception in listen #1788

@hzliu

Description

@hzliu
import time
import redis
db = redis.from_url('redis://redis_server', health_check_interval=1.0)
ps = db.pubsub(ignore_subscribe_messages=True)
ps.subscribe('CHANNEL_NOT_EXISTS')
time.sleep(2)
for msg in ps.listen():
    print(msg)

will raise exception:

Traceback (most recent call last):
  File "test_listen.py", line 7, in <module>
    for msg in ps.listen():
  File ".../lib/python3.9/site-packages/redis/client.py", line 3605, in listen
    response = self.handle_message(self.parse_response(block=True))
  File ".../lib/python3.9/site-packages/redis/client.py", line 3635, in handle_message
    message_type = nativestr(response[0])
TypeError: 'NoneType' object is not subscriptable

parse_response returns None, since the first response is a health-check response.

Happens on 3.5.2 and 4.0.2

fix is straight forward:

    def listen(self):
        "Listen for messages on channels this client has been subscribed to"
        while self.subscribed:
            response = self.parse_response(block=True)
            if response is None:
                continue
            response = self.handle_message(response)
            if response is not None:
                yield response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions