Semaphore race condition
See original GitHub issueI unexpectedly got a ‘WouldBlock’ error while not using any *_nowait() calls. I’m using the latest version of the master branch. The problem goes away if I replace https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_synchronization.py#L305 with await self.acquire()
, but I’m not sure if that’s actually the best solution. I think the problem is that another task increments the semaphore (by calling acquire()) before the Event.wait()-handling coroutine is scheduled to run.
Relevant part of the traceback:
async with self._pool.read_transaction() as tx:
/usr/lib/python3.8/contextlib.py:171: in __aenter__
return await self.gen.__anext__()
tests/test_replicate.py:39: in read_transaction
async with self.read_semaphore, self._transaction() as conn:
venv/lib/python3.8/site-packages/anyio/_core/_synchronization.py:282: in __aenter__
await self.acquire()
venv/lib/python3.8/site-packages/anyio/_core/_synchronization.py:306: in acquire
self.acquire_nowait()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <anyio.Semaphore object at 0x7f434698fb50>
def acquire_nowait(self) -> None:
"""
Acquire the underlying lock, without blocking.
:raises ~WouldBlock: if the operation would block
"""
if self._value == 0:
> raise WouldBlock
E anyio.WouldBlock
venv/lib/python3.8/site-packages/anyio/_core/_synchronization.py:316: WouldBlock
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
Race Condition, Critical Section and Semaphore - Tutorialspoint
A race condition is a situation that may occur inside a critical section. This happens when the result of multiple thread execution in...
Read more >Race Conditions, Critical Sections and Semaphores
Prohibits more than one process from accessing shared memory at same time. If no two processes enter their critical sections at same time,...
Read more >Race Conditions, Locks, Semaphores, and Deadlocks - Medium
A semaphore is a variable or abstract data type used to control access to a common resource by multiple processes in a concurrent...
Read more >What prevents a race condition when checking a semaphore ...
Suppose two threads call the P function at the same time, they both reach the if(lock > 0) check at the same time...
Read more >A Brief Introduction to Race Conditions - Linux Device Drivers ...
A semaphore is a general mechanism for controlling access to resources. In its simplest form, a semaphore may be used for mutual exclusion;...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, it does!
I have a test that reliably hits this issue, and after installing your branch it passes, but sadly it’s an integration test that isn’t easy to reduce to something that could be integrated in anyio’s test suite.
I can confirm the final fix also works. Thanks @smurfix and @agronholm!