-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-109795: _thread.start_new_thread
: allocate thread bootstate using raw memory allocator
#109808
gh-109795: _thread.start_new_thread
: allocate thread bootstate using raw memory allocator
#109808
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
Windows (x86) failed due to #109739. |
…of github.com:chgnrdv/cpython into _thread-start-new-thread-use-raw-malloc-for-bootstate
Co-authored-by: Victor Stinner <[email protected]>
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Sorry, @chgnrdv and @vstinner, I could not cleanly backport this to |
…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. |
Merged, thanks! |
|
…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]>
…e using raw memory allocator (python#109808)
…e using raw memory allocator (python#109808) (cherry picked from commit 1b8f236)
) (#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]>
…e using raw memory allocator (python#109808)
Fixes #109795
When new thread is starting, but Python is finalizing,
PyMem_Free
can't be called on new threadbootstate
, because this thread doesn't own the GIL. We should use raw memory allocator to allocate/freebootstate
to avoid crashes/other unexpected behaviour.PyMem_Free
without holding the GIL #109795