1

this is my code:

pool = aioredis.ConnectionPool.from_url("redis://localhost:6379")
redis = await aioredis.Redis(connection_pool=pool, ssl=False, ssl_cert_reqs="None")

async with redis.lock("hello_lock") as lock:
    print("hello")
    await redis.hset("hello", "1", "2")

the Context Manager never gets entered and it seems like the lock is not being acquired at all. The program stays running/stuck on this line: async with redis.lock("hello_lock") as lock:

Acquiring the lock should be a very quick thing so i'm confused whats happening here. Any help would be appreciated.

3awny
  • 319
  • 1
  • 2
  • 10
  • Your bug seem to be in the Redis server configuration, if you have other clients connected to Redis and they are executing long-running commands, the lock acquisition could be blocked – Saxtheowl Mar 22 '23 at 00:59
  • 1
    my redis server is local and im testing with it. Im 100% sure there are no other clients connected – 3awny Mar 22 '23 at 01:26
  • @Saxtheowl whats weird is that locking works with other libraries such as aioredlock. But not with aioredis built-in locking – 3awny Mar 22 '23 at 01:28
  • 1
    I made an answer with a possible solution – Saxtheowl Mar 22 '23 at 01:32

1 Answers1

0

It might be a problem with the Redis lock implementation, try the following commands to manually acquire and release a lock:

127.0.0.1:6379> SET my_lock "1" NX
OK
127.0.0.1:6379> GET my_lock
"1"

If that still dont work try another method of lock like RedLock or RLock

Saxtheowl
  • 4,136
  • 5
  • 23
  • 32