[Python-Dev] [Python-checkins] cpython: asyncio: Change bounded semaphore into a subclass, like

Tim Peters tim.peters at gmail.com
Sun Nov 24 00:18:37 CET 2013


[guido]
> http://hg.python.org/cpython/rev/6bee0fdcba39
> changeset:   87468:6bee0fdcba39
> user:        Guido van Rossum <guido at python.org>
> date:        Sat Nov 23 15:09:16 2013 -0800
> summary:
>   asyncio: Change bounded semaphore into a subclass, like threading.[Bounded]Semaphore.
>
> files:
>   Lib/asyncio/locks.py                |  36 ++++++++--------
>   Lib/test/test_asyncio/test_locks.py |   2 +-
>   2 files changed, 20 insertions(+), 18 deletions(-)
>
>
> diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py
> --- a/Lib/asyncio/locks.py
> +++ b/Lib/asyncio/locks.py
> @@ -336,22 +336,15 @@

...
> +class BoundedSemaphore(Semaphore):
...
> +    def release(self):
> +        if self._value >= self._bound_value:
> +            raise ValueError('BoundedSemaphore released too many times')
> +        super().release()

If there's a lock and parallelism involved, this release()
implementation is vulnerable to races:  any number of threads can see
"self._value < self._bound_value" before one of them manages to call
the superclass release(), and so self._value can become arbitrarily
larger than self._bound_value.  I fixed the same bug in threading's
similar bounded semaphore class a few weeks ago.


More information about the Python-Dev mailing list