[New-bugs-announce] [issue47246] Race condition in `threadig.Thread._wait_for_tstate_lock`

Dieter Maurer report at bugs.python.org
Thu Apr 7 04:44:47 EDT 2022


New submission from Dieter Maurer <dieter at handshake.de>:

I have observed an `AssertionError (assert self._is_stopped)` in `threading.Thread._wait_for_tstate_lock`. This indicates that Python's internal state has been corrupted.

The analysis revealed the following race condition:
`_wait_for_tstate:lock` contains the code:
```
                lock.release()
                self._stop()
```
The `lock.release()` allows a conflicting call to execute. If this happens before `self._stop()` has executed `self._is_stopped = True`, the `AssertionError` is raised.

I suggest to give `_stop` an additional parameter `locked` with default `False`. In indicates whether the caller holds the `tstate_lock`. If this is the case, `_stop` releases the lock after it has ensured a consistent state (esspecially set `_is_stopped` to `True`). With this modification to `_stop` the two lines above can be replaced by `self._stop(locked=True)`.

----------
components: Library (Lib)
messages: 416919
nosy: dmaurer
priority: normal
severity: normal
status: open
title: Race condition in `threadig.Thread._wait_for_tstate_lock`
type: behavior
versions: Python 3.10, Python 3.11, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue47246>
_______________________________________


More information about the New-bugs-announce mailing list