-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
Race in Thread.join() #116372
Copy link
Copy link
Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Bug report
Bug description:
There is a race between when
Thread._tstate_lockis released1 inThread._wait_for_tstate_lock()and whenThread._stop()asserts2 that it is unlocked. Consider the following execution involving threads A, B, and C:_tstate_lock._tstate_lock._tstate_lock._tstate_lockin_wait_for_tstate_lock(), releases it, but is swapped out before calling_stop()._tstate_lockin_wait_for_tstate_lock()but is swapped out before releasing it._stop(), which asserts that A's_tstate_lockis not held. However, C holds it, so the assertion fails.The race can be reproduced3 by inserting sleeps at the appropriate points in the threading code. To do so, run the
repro_join_race.pyfrom the linked repo.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Footnotes
https://github.com/python/cpython/blob/441affc9e7f419ef0b68f734505fa2f79fe653c7/Lib/threading.py#L1201 ↩
https://github.com/python/cpython/blob/441affc9e7f419ef0b68f734505fa2f79fe653c7/Lib/threading.py#L1115 ↩
https://github.com/mpage/cpython/commit/81946532792f938cd6f6ab4c4ff92a4edf61314f ↩