question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pubsub.run_in_thread throws exceptions in case of lost redis connection

See original GitHub issue

I started the thread for keyspace events:

self._pubsub = self._redis.pubsub()
self._pubsub.psubscribe(**{'__keyspace@0__:*': self._redis_notification})
self._pubsub_thread = self._pubsub.run_in_thread(sleep_time=0.5)

If the connection broke with redis (redis process stopped), lot of exceptions appeared. And I do not know, how my main thread could handle this (or get notified about it).

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 129, in _read_from_socket
    raise socket.error(SERVER_CLOSED_CONNECTION_ERROR)
OSError: Connection closed by server.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/redis/client.py", line 2165, in _execute
    return command(*args)
  File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 577, in read_response
    response = self._parser.read_response()
  File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 238, in read_response
    response = self._buffer.readline()
  File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 168, in readline
    self._read_from_socket()
  File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 143, in _read_from_socket
    (e.args,))
redis.exceptions.ConnectionError: Error while reading from socket: ('Connection closed by server.',)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 439, in connect
    sock = self._connect()
  File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 494, in _connect
    raise err
  File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 482, in _connect
    sock.connect(socket_address)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.4/dist-packages/redis/client.py", line 2345, in run
    timeout=sleep_time)
  File "/usr/local/lib/python3.4/dist-packages/redis/client.py", line 2260, in get_message
    response = self.parse_response(block=False, timeout=timeout)
  File "/usr/local/lib/python3.4/dist-packages/redis/client.py", line 2183, in parse_response
    return self._execute(connection, connection.read_response)
  File "/usr/local/lib/python3.4/dist-packages/redis/client.py", line 2172, in _execute
    connection.connect()
  File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 442, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 111 connecting to 172.18.0.4:6379. Connection refused.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
wiwat-tharateeraparbcommented, Aug 15, 2016

I have worked around this by polling the pubsub thread’s health e.g. self._pubsub_thread.is_alive() and stop the thread when it’s not alive anymore. But it will be awesome if we can register a callback or handler for run_in_thread’s exception.

1reaction
dmarsh19commented, Jul 30, 2020

I am having similar issues. Its additionally problematic because ConnectionError exceptions bubble up all the way from SocketBuffer to PubSubWorkerThread.run() without being handled, which eventually kills the thread unnoticed. threading.excepthook() can be used in Python3.8 to catch exceptions as a workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pubsub.run_in_thread throws exceptions in case of lost redis ...
If the connection broke with redis (redis process stopped), lot of exceptions appeared. And I do not know, how my main thread could...
Read more >
java - Spring-Boot 2.7 Redis PUB/SUB fails startup on missing ...
Where or how can I configure that it catches the thrown exception on startup and just retries it again until the connection to...
Read more >
Exceptions — redis-py dev documentation
This error only happens in the case where the connection pool will try to fetch what node that is covered by a given...
Read more >
Redis Pub/Sub
When the last argument is zero, we are no longer subscribed to any channel, and the client can issue any kind of Redis...
Read more >
Handle message failures | Cloud Pub/Sub
In this case, it's possible that Pub/Sub will resend multiple messages that can't be acknowledged. To address immediate redelivery issues, Pub/Sub lets you ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found