aiorwlock icon indicating copy to clipboard operation
aiorwlock copied to clipboard

Lock not properly acquired when using `asyncio.wait_for()`

Open marcmonfort opened this issue 10 months ago • 0 comments

When trying to use asyncio.wait_for() to add a timeout to acquire(), seems that it is not being properly acquired for the current task.

lock = RWLock()
try:
    await asyncio.wait_for(lock.writer_lock.acquire(), timeout=5)
    lock.writer_lock.release()
except asyncio.TimeoutError:
    pass

Traceback

Traceback (most recent call last):
  File "/home/marc/test/rwlock/venv/lib/python3.10/site-packages/aiorwlock/__init__.py", line 147, in _release
    self._owning.remove((me, lock_type))
ValueError: list.remove(x): x not in list

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/marc/test/rwlock/test.py", line 61, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/marc/test/rwlock/test.py", line 52, in main
    await asyncio.gather(
  File "/home/marc/test/rwlock/test.py", line 26, in access_object
    lock.reader_lock.release()
  File "/home/marc/test/rwlock/venv/lib/python3.10/site-packages/aiorwlock/__init__.py", line 217, in release
    self._lock.release_read()
  File "/home/marc/test/rwlock/venv/lib/python3.10/site-packages/aiorwlock/__init__.py", line 136, in release_read
    self._release(self._RL)
  File "/home/marc/test/rwlock/venv/lib/python3.10/site-packages/aiorwlock/__init__.py", line 149, in _release
    raise RuntimeError('Cannot release an un-acquired lock')
RuntimeError: Cannot release an un-acquired lock

This same script using python built-in async lock works

lock = asyncio.Lock()
try:
    await asyncio.wait_for(lock.acquire(), timeout=5)
    lock.release()
except asyncio.TimeoutError:
    pass

marcmonfort avatar Apr 30 '24 16:04 marcmonfort