question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Semaphore race condition

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
marten-de-vriescommented, Apr 6, 2021

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.

0reactions
marten-de-vriescommented, Apr 6, 2021

I can confirm the final fix also works. Thanks @smurfix and @agronholm!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found