Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions, redis and asyncio #2727

Open
registiy opened this issue Apr 28, 2023 · 1 comment
Open

Exceptions, redis and asyncio #2727

registiy opened this issue Apr 28, 2023 · 1 comment
Labels

Comments

@registiy
Copy link

registiy commented Apr 28, 2023

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?

@uglide uglide added the async label Jun 15, 2023
@talhazafarjutt
Copy link

i am facing the same problem, did you find out any solution????

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants