2
In [16]: r                                                                                                                                                                                                                                                                      
Out[16]: Redis<ConnectionPool<Connection<host=****,port=*****,db=3>>>

In [17]: r.set('zaza', 'king')                                                                                                                                                                                                                                                  
---------------------------------------------------------------------------
ConnectionError                           Traceback (most recent call last)
<ipython-input-17-8126d1846970> in <module>
----> 1 r.set('zaza', 'king')

/usr/local/lib/python3.7/site-packages/redis/client.py in set(self, name, value, ex, px, nx, xx)
   1517         if xx:
   1518             pieces.append('XX')
-> 1519         return self.execute_command('SET', *pieces)
   1520 
   1521     def __setitem__(self, name, value):

/usr/local/lib/python3.7/site-packages/redis/client.py in execute_command(self, *args, **options)
    834         pool = self.connection_pool
    835         command_name = args[0]
--> 836         conn = self.connection or pool.get_connection(command_name, **options)
    837         try:
    838             conn.send_command(*args)

/usr/local/lib/python3.7/site-packages/redis/connection.py in get_connection(self, command_name, *keys, **options)
   1069         try:
   1070             # ensure this connection is connected to Redis
-> 1071             connection.connect()
   1072             # connections that the pool provides should be ready to send
   1073             # a command. if not, the connection was either returned to the

/usr/local/lib/python3.7/site-packages/redis/connection.py in connect(self)
    545         self._sock = sock
    546         try:
--> 547             self.on_connect()
    548         except RedisError:
    549             # clean up after any error in on_connect

/usr/local/lib/python3.7/site-packages/redis/connection.py in on_connect(self)
    615             # to check the health prior to the AUTH
    616             self.send_command('AUTH', self.password, check_health=False)
--> 617             if nativestr(self.read_response()) != 'OK':
    618                 raise AuthenticationError('Invalid Password')
    619 

/usr/local/lib/python3.7/site-packages/redis/connection.py in read_response(self)
    697         "Read the response from a previously sent command"
    698         try:
--> 699             response = self._parser.read_response()
    700         except socket.timeout:
    701             self.disconnect()

/usr/local/lib/python3.7/site-packages/redis/connection.py in read_response(self)
    307 
    308     def read_response(self):
--> 309         response = self._buffer.readline()
    310         if not response:
    311             raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)

/usr/local/lib/python3.7/site-packages/redis/connection.py in readline(self)
    239         while not data.endswith(SYM_CRLF):
    240             # there's more data in the socket that we need
--> 241             self._read_from_socket()
    242             buf.seek(self.bytes_read)
    243             data = buf.readline()

/usr/local/lib/python3.7/site-packages/redis/connection.py in _read_from_socket(self, length, timeout, raise_on_timeout)
    184                 # an empty string indicates the server shutdown the socket
    185                 if isinstance(data, bytes) and len(data) == 0:
--> 186                     raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
    187                 buf.write(data)
    188                 data_length = len(data)

This started happening after a move to hosted redis from a local instance

Oleg Belousov
  • 9,981
  • 14
  • 72
  • 127

1 Answers1

0

So, the problem is that the Redis connection string on those hosted solutions has to start with rediss:// as in redis + SSL as per the official documentation: https://redislabs.com/lp/python-redis/

If you use hosted Redis from AWS or Digital Ocean this might as well happen to you :)

if you are using Celery you would also need to modify your app config in app.py as per https://github.com/celery/celery/issues/5371

Oleg Belousov
  • 9,981
  • 14
  • 72
  • 127