]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.12] gh-108987: Fix _thread.start_new_thread() race condition (#109135) (#110342)
authorVictor Stinner <vstinner@python.org>
Wed, 4 Oct 2023 11:20:31 +0000 (13:20 +0200)
committerGitHub <noreply@github.com>
Wed, 4 Oct 2023 11:20:31 +0000 (11:20 +0000)
commit4936fa954125864ae3ae5d36863479094837e88a
tree44d8235ceea9c21e887e17cb2e90a20e526020f3
parent1d87465005e8349323f6dad7e13f48ed5b52f6ad
[3.12] gh-108987: Fix _thread.start_new_thread() race condition (#109135) (#110342)

* gh-108987: Fix _thread.start_new_thread() race condition (#109135)

Fix _thread.start_new_thread() race condition. If a thread is created
during Python finalization, the newly spawned thread now exits
immediately instead of trying to access freed memory and lead to a
crash.

thread_run() calls PyEval_AcquireThread() which checks if the thread
must exit. The problem was that tstate was dereferenced earlier in
_PyThreadState_Bind() which leads to a crash most of the time.

Move _PyThreadState_CheckConsistency() from thread_run() to
_PyThreadState_Bind().

(cherry picked from commit 517cd82ea7d01b344804413ef05610934a43a241)

* gh-109795: `_thread.start_new_thread`: allocate thread bootstate using raw memory allocator (#109808)

(cherry picked from commit 1b8f2366b38c87b0450d9c15bdfdd4c4a2fc3a01)

---------

Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Include/internal/pycore_pystate.h
Misc/NEWS.d/next/Library/2023-09-08-12-09-55.gh-issue-108987.x5AIG8.rst [new file with mode: 0644]
Modules/_threadmodule.c
Python/ceval_gil.c
Python/pystate.c