[New-bugs-announce] [issue19158] BoundedSemaphore.release() subject to races

Tim Peters report at bugs.python.org
Fri Oct 4 02:00:16 CEST 2013

New submission from Tim Peters:

I'm sure this flaw exists on more than just the current default branch, but didn't check.

BoundedSemaphore.release() doesn't quite do what it thinks it's doing.  By eyeball, the code obviously suffers from a small timing hole:  multiple threads releasing at the same time can all see "self._value >= self._initial_value" as false before any of them actually releases the semaphore.  So the value of ._value can become arbitrarily higher than ._initial_value.

This is hard to provoke.  The attached patch adds a TESTWAIT Event to BoundedSemaphore so that the new file (unbounded.py) can demonstrate the race reliably.  The patch doesn't fix anything - it's just a way to demonstrate the problem.  At the end, unbounded.py prints

bs._value is 2

That should be "impossible" for a BoundedSemaphore(1).

components: Library (Lib)
files: sema
messages: 198928
nosy: tim.peters
priority: normal
severity: normal
stage: needs patch
status: open
title: BoundedSemaphore.release() subject to races
type: behavior
versions: Python 3.5
Added file: http://bugs.python.org/file31957/sema

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list