summaryrefslogtreecommitdiffstats
path: root/Include/internal
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-09-11 15:27:03 (GMT)
committerGitHub <noreply@github.com>2023-09-11 15:27:03 (GMT)
commit517cd82ea7d01b344804413ef05610934a43a241 (patch)
tree733959eb7944699c65275e5108be00b8dbc7a061 /Include/internal
parentc0f488b88f2a54d76256818e2841d868fecfd396 (diff)
downloadcpython-517cd82ea7d01b344804413ef05610934a43a241.zip
cpython-517cd82ea7d01b344804413ef05610934a43a241.tar.gz
cpython-517cd82ea7d01b344804413ef05610934a43a241.tar.bz2
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().
Diffstat (limited to 'Include/internal')
-rw-r--r--Include/internal/pycore_pystate.h2
1 files changed, 2 insertions, 0 deletions
diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h
index 9c0e42e..9fc8ae9 100644
--- a/Include/internal/pycore_pystate.h
+++ b/Include/internal/pycore_pystate.h
@@ -71,6 +71,8 @@ extern _Py_thread_local PyThreadState *_Py_tss_tstate;
extern int _PyThreadState_CheckConsistency(PyThreadState *tstate);
#endif
+int _PyThreadState_MustExit(PyThreadState *tstate);
+
// Export for most shared extensions, used via _PyThreadState_GET() static
// inline function.
PyAPI_FUNC(PyThreadState *) _PyThreadState_GetCurrent(void);