Skip to content

Exceptions, redis and asyncio #2727

Closed as not planned
Closed as not planned
@registiy

Description

@registiy

I'm working on a web-project and a few days ago I diagnosed a problem: after restarting the Redis service some urls were throwing a 500 error for multiple requests in a row though Redis was already running.

Version:
redis==4.5.4

Platform: What platform / version? (For example Python 3.5.1 on Windows 7 / Ubuntu 15.10 / Azure)
Python 3.10.10
Redis 7.0.5

Description:

`
import asyncio
import redis.asyncio as redis

      aioredis_conn = redis.from_url(
          url=f"redis://127.0.0.1:6379",
          decode_responses=True
      )
      async def main():
          while True:
      
              print('*******')
              try:
                  await aioredis_conn.get('something')
                  tasks = []
                  tasks.append(aioredis_conn.get('something'))
                  tasks.append(aioredis_conn.get('something'))
                  tasks.append(aioredis_conn.get('something'))
                  tasks.append(aioredis_conn.get('something'))
      
                  results = await asyncio.gather(*tasks, return_exceptions=True)
                  print(f'Gather results: {results}')
      
              except Exception as e:
                  print(f'Caught general exception: {e}')
      
              await asyncio.sleep(3)
      
      asyncio.run(main())

`

What to do:

  1. Start redis

  2. Run script

  3. Every 3 seconds you will see
    Gather results: [None, None, None, None]

  4. Stop redis and wait for message in terminal
    Caught general exception: Connection closed by server.

If you wait for more than 3 seconds, you will see also
Caught general exception: Error 61 connecting to 127.0.0.1:6379. 61.

  1. Start redis and in terminal you will see
    Gather results: [None, ConnectionError('Connection closed by server.'), ConnectionError('Connection closed by server.'), ConnectionError('Connection closed by server.')]

  2. On the next iteration of script the result is as expected:
    Gather results: [None, None, None, None]

I don't expect to see exceptions in gather results, and also I'm very confused that these exceptions are from previous iteration.

Also I don't see this problem while using aioredis==2.0.1 and I assume that the root of the problem is in redis library.

Am I right and this is a bug. Or am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions