gh-109795: _thread.start_new_thread: allocate thread bootstate using raw memory allocator#109808
Merged
vstinner merged 8 commits intopython:mainfrom Sep 25, 2023
chgnrdv:_thread-start-new-thread-use-raw-malloc-for-bootstate
Merged
gh-109795: _thread.start_new_thread: allocate thread bootstate using raw memory allocator#109808vstinner merged 8 commits intopython:mainfrom chgnrdv:_thread-start-new-thread-use-raw-malloc-for-bootstate
_thread.start_new_thread: allocate thread bootstate using raw memory allocator#109808vstinner merged 8 commits intopython:mainfrom
chgnrdv:_thread-start-new-thread-use-raw-malloc-for-bootstate
Conversation
…mory allocator When new thread is starting, but Python is finalizing, PyMem_Free can't be called on new thread bootstate, because this created thread doesn't own the GIL. We should use raw memory allocator to allocate/free bootstate to avoid crashes/other unexpected behaviour.
_thread.start_new_thread: allocate/free thread bootstate using raw memory allocator_thread.start_new_thread: allocate thread bootstate using raw memory allocator
Contributor
Author
|
Windows (x86) failed due to #109739. |
vstinner
reviewed
Sep 25, 2023
…of github.com:chgnrdv/cpython into _thread-start-new-thread-use-raw-malloc-for-bootstate
vstinner
reviewed
Sep 25, 2023
Co-authored-by: Victor Stinner <[email protected]>
vstinner
reviewed
Sep 25, 2023
| @@ -0,0 +1 @@ | |||
| :func:`!_thread.start_new_thread` now uses raw memory allocator to allocate new thread bootstate. This helps to avoid crashes in case when new thread gets started at interpreter finalization. | |||
Member
There was a problem hiding this comment.
Oh. Please remove this Changelog entry. I don't think that it's worth it. It doesn't impact directly users and it's a recent regression, no?
…e-109795.gI7KW6.rst
Contributor
Contributor
|
Sorry, @chgnrdv and @vstinner, I could not cleanly backport this to |
vstinner
pushed a commit
to vstinner/cpython
that referenced
this pull request
Sep 25, 2023
…e using raw memory allocator (python#109808) (cherry picked from commit 1b8f236)
|
GH-109852 is a backport of this pull request to the 3.11 branch. |
Member
Member
|
Merged, thanks! |
|
vstinner
added a commit
that referenced
this pull request
Sep 25, 2023
…te usin… (#109852) gh-109795: `_thread.start_new_thread`: allocate thread bootstate using raw memory allocator (#109808) (cherry picked from commit 1b8f236) Co-authored-by: Radislav Chugunov <[email protected]>
csm10495
pushed a commit
to csm10495/cpython
that referenced
this pull request
Sep 28, 2023
…e using raw memory allocator (python#109808)
vstinner
pushed a commit
to vstinner/cpython
that referenced
this pull request
Oct 4, 2023
…e using raw memory allocator (python#109808) (cherry picked from commit 1b8f236)
vstinner
added a commit
that referenced
this pull request
Oct 4, 2023
) (#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 517cd82) * gh-109795: `_thread.start_new_thread`: allocate thread bootstate using raw memory allocator (#109808) (cherry picked from commit 1b8f236) --------- Co-authored-by: Radislav Chugunov <[email protected]>
Glyphack
pushed a commit
to Glyphack/cpython
that referenced
this pull request
Sep 2, 2024
…e using raw memory allocator (python#109808)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #109795
When new thread is starting, but Python is finalizing,
PyMem_Freecan't be called on new threadbootstate, because this thread doesn't own the GIL. We should use raw memory allocator to allocate/freebootstateto avoid crashes/other unexpected behaviour.PyMem_Freewithout holding the GIL #109795