Jonathan Livni
Jonathan Livni

Reputation: 107062

redis-py "ConnectionError: Socket closed on remote end"

Using redis-py's PubSub class I sometimes get the following exception:

Exception in thread listener_2013-10-24 12:50:31.687000:
Traceback (most recent call last):
  File "c:\Python27\Lib\threading.py", line 551, in __bootstrap_inner
    self.run()
  File "c:\Python27\Lib\threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\Administrator\Documents\my_proj\my_module.py", line 69, in _listen
    for message in _pubsub.listen():
  File "C:\Users\Administrator\virtual_environments\spyker\lib\site-packages\redis\client.py", line 1555, in listen
    r = self.parse_response()
  File "C:\Users\Administrator\virtual_environments\spyker\lib\site-packages\redis\client.py", line 1499, in parse_response
    response = self.connection.read_response()
  File "C:\Users\Administrator\virtual_environments\spyker\lib\site-packages\redis\connection.py", line 306, in read_response
    response = self._parser.read_response()
  File "C:\Users\Administrator\virtual_environments\spyker\lib\site-packages\redis\connection.py", line 106, in read_response
    raise ConnectionError("Socket closed on remote end")
ConnectionError: Socket closed on remote end

What would cause such an event?
If I catch this exception, what would be a reasonable handling logic? Would retrying listen() be futile?

The reason for asking and not simply trying is that I do not know how to reproduce this problem. It's rare but it's detrimental, so I must create some logic before this error strikes again.

Upvotes: 2

Views: 2600

Answers (2)

Marcin
Marcin

Reputation: 49816

The cause is that your redis server is unreachable, either because it shutdown, or there is a network failure. When this happens, check the status of your redis server and the network connection to find out what is causing this.

However, it is inevitable that this will happen. You need to figure out how to handle it, even if by asking the user to wait while redis is respawned.

Upvotes: 0

dstromberg
dstromberg

Reputation: 7167

Possible sources of this:

  1. An error in the code on other side of the connection caused the connection to be dropped
  2. The remote computer was rebooted or it crashed
  3. Someone is playing around with their network interface using ipconfig or similar
  4. A transient network error lasted long enough that the connection was dropped

It's probably better not to worry why this happens (there are likely reasons I've not listed), and instead focus on how to deal with it when it does.

You'd probably best find a place in your code to add a try/except and either process the partial data that was received, or ignore the partial data that was received.

Upvotes: 2

Related Questions