Skip to content

Commit 82a1806

Browse files
authored
[3.11] gh-108987: Fix _thread.start_new_thread() race condition (#109135) (#109272)
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)
1 parent 9297a72 commit 82a1806

File tree

5 files changed

+66
-38
lines changed

5 files changed

+66
-38
lines changed

Include/internal/pycore_pystate.h

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ _Py_ThreadCanHandlePendingCalls(void)
6565
extern int _PyThreadState_CheckConsistency(PyThreadState *tstate);
6666
#endif
6767

68+
int _PyThreadState_MustExit(PyThreadState *tstate);
69+
6870
/* Variable and macro for in-line access to current thread
6971
and interpreter state */
7072

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :func:`_thread.start_new_thread` race condition. If a thread is created
2+
during Python finalization, the newly spawned thread now exits immediately
3+
instead of trying to access freed memory and lead to a crash. Patch by
4+
Victor Stinner.

Modules/_threadmodule.c

+31-16
Original file line numberDiff line numberDiff line change
@@ -1053,21 +1053,21 @@ _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
10531053
/* Module functions */
10541054

10551055
struct bootstate {
1056-
PyInterpreterState *interp;
1056+
PyThreadState *tstate;
10571057
PyObject *func;
10581058
PyObject *args;
10591059
PyObject *kwargs;
1060-
PyThreadState *tstate;
1061-
_PyRuntimeState *runtime;
10621060
};
10631061

10641062

10651063
static void
1066-
thread_bootstate_free(struct bootstate *boot)
1064+
thread_bootstate_free(struct bootstate *boot, int decref)
10671065
{
1068-
Py_DECREF(boot->func);
1069-
Py_DECREF(boot->args);
1070-
Py_XDECREF(boot->kwargs);
1066+
if (decref) {
1067+
Py_DECREF(boot->func);
1068+
Py_DECREF(boot->args);
1069+
Py_XDECREF(boot->kwargs);
1070+
}
10711071
PyMem_Free(boot);
10721072
}
10731073

@@ -1078,9 +1078,24 @@ thread_run(void *boot_raw)
10781078
struct bootstate *boot = (struct bootstate *) boot_raw;
10791079
PyThreadState *tstate = boot->tstate;
10801080

1081-
// gh-104690: If Python is being finalized and PyInterpreterState_Delete()
1082-
// was called, tstate becomes a dangling pointer.
1083-
assert(_PyThreadState_CheckConsistency(tstate));
1081+
// gh-108987: If _thread.start_new_thread() is called before or while
1082+
// Python is being finalized, thread_run() can called *after*.
1083+
// _PyRuntimeState_SetFinalizing() is called. At this point, all Python
1084+
// threads must exit, except of the thread calling Py_Finalize() whch holds
1085+
// the GIL and must not exit.
1086+
//
1087+
// At this stage, tstate can be a dangling pointer (point to freed memory),
1088+
// it's ok to call _PyThreadState_MustExit() with a dangling pointer.
1089+
if (_PyThreadState_MustExit(tstate)) {
1090+
// Don't call PyThreadState_Clear() nor _PyThreadState_DeleteCurrent().
1091+
// These functions are called on tstate indirectly by Py_Finalize()
1092+
// which calls _PyInterpreterState_Clear().
1093+
//
1094+
// Py_DECREF() cannot be called because the GIL is not held: leak
1095+
// references on purpose. Python is being finalized anyway.
1096+
thread_bootstate_free(boot, 0);
1097+
goto exit;
1098+
}
10841099

10851100
tstate->thread_id = PyThread_get_thread_ident();
10861101
#ifdef PY_HAVE_THREAD_NATIVE_ID
@@ -1105,20 +1120,22 @@ thread_run(void *boot_raw)
11051120
Py_DECREF(res);
11061121
}
11071122

1108-
thread_bootstate_free(boot);
1123+
thread_bootstate_free(boot, 1);
1124+
11091125
tstate->interp->threads.count--;
11101126
PyThreadState_Clear(tstate);
11111127
_PyThreadState_DeleteCurrent(tstate);
11121128

1129+
exit:
11131130
// bpo-44434: Don't call explicitly PyThread_exit_thread(). On Linux with
11141131
// the glibc, pthread_exit() can abort the whole process if dlopen() fails
11151132
// to open the libgcc_s.so library (ex: EMFILE error).
1133+
return;
11161134
}
11171135

11181136
static PyObject *
11191137
thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
11201138
{
1121-
_PyRuntimeState *runtime = &_PyRuntime;
11221139
PyObject *func, *args, *kwargs = NULL;
11231140

11241141
if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
@@ -1151,13 +1168,11 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
11511168
if (boot == NULL) {
11521169
return PyErr_NoMemory();
11531170
}
1154-
boot->interp = _PyInterpreterState_GET();
1155-
boot->tstate = _PyThreadState_Prealloc(boot->interp);
1171+
boot->tstate = _PyThreadState_Prealloc(interp);
11561172
if (boot->tstate == NULL) {
11571173
PyMem_Free(boot);
11581174
return PyErr_NoMemory();
11591175
}
1160-
boot->runtime = runtime;
11611176
boot->func = Py_NewRef(func);
11621177
boot->args = Py_NewRef(args);
11631178
boot->kwargs = Py_XNewRef(kwargs);
@@ -1166,7 +1181,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
11661181
if (ident == PYTHREAD_INVALID_THREAD_ID) {
11671182
PyErr_SetString(ThreadError, "can't start new thread");
11681183
PyThreadState_Clear(boot->tstate);
1169-
thread_bootstate_free(boot);
1184+
thread_bootstate_free(boot, 1);
11701185
return NULL;
11711186
}
11721187
return PyLong_FromUnsignedLong(ident);

Python/ceval_gil.h

+3-22
Original file line numberDiff line numberDiff line change
@@ -185,25 +185,6 @@ drop_gil(struct _ceval_runtime_state *ceval, struct _ceval_state *ceval2,
185185
}
186186

187187

188-
/* Check if a Python thread must exit immediately, rather than taking the GIL
189-
if Py_Finalize() has been called.
190-
191-
When this function is called by a daemon thread after Py_Finalize() has been
192-
called, the GIL does no longer exist.
193-
194-
tstate must be non-NULL. */
195-
static inline int
196-
tstate_must_exit(PyThreadState *tstate)
197-
{
198-
/* bpo-39877: Access _PyRuntime directly rather than using
199-
tstate->interp->runtime to support calls from Python daemon threads.
200-
After Py_Finalize() has been called, tstate can be a dangling pointer:
201-
point to PyThreadState freed memory. */
202-
PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
203-
return (finalizing != NULL && finalizing != tstate);
204-
}
205-
206-
207188
/* Take the GIL.
208189
209190
The function saves errno at entry and restores its value at exit.
@@ -216,7 +197,7 @@ take_gil(PyThreadState *tstate)
216197

217198
assert(tstate != NULL);
218199

219-
if (tstate_must_exit(tstate)) {
200+
if (_PyThreadState_MustExit(tstate)) {
220201
/* bpo-39877: If Py_Finalize() has been called and tstate is not the
221202
thread which called Py_Finalize(), exit immediately the thread.
222203
@@ -255,7 +236,7 @@ take_gil(PyThreadState *tstate)
255236
_Py_atomic_load_relaxed(&gil->locked) &&
256237
gil->switch_number == saved_switchnum)
257238
{
258-
if (tstate_must_exit(tstate)) {
239+
if (_PyThreadState_MustExit(tstate)) {
259240
MUTEX_UNLOCK(gil->mutex);
260241
// gh-96387: If the loop requested a drop request in a previous
261242
// iteration, reset the request. Otherwise, drop_gil() can
@@ -295,7 +276,7 @@ take_gil(PyThreadState *tstate)
295276
MUTEX_UNLOCK(gil->switch_mutex);
296277
#endif
297278

298-
if (tstate_must_exit(tstate)) {
279+
if (_PyThreadState_MustExit(tstate)) {
299280
/* bpo-36475: If Py_Finalize() has been called and tstate is not
300281
the thread which called Py_Finalize(), exit immediately the
301282
thread.

Python/pystate.c

+26
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,10 @@ _PyThreadState_Init(PyThreadState *tstate)
882882
void
883883
_PyThreadState_SetCurrent(PyThreadState *tstate)
884884
{
885+
// gh-104690: If Python is being finalized and PyInterpreterState_Delete()
886+
// was called, tstate becomes a dangling pointer.
887+
assert(_PyThreadState_CheckConsistency(tstate));
888+
885889
_PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
886890
}
887891

@@ -2255,6 +2259,28 @@ _PyThreadState_CheckConsistency(PyThreadState *tstate)
22552259
#endif
22562260

22572261

2262+
// Check if a Python thread must exit immediately, rather than taking the GIL
2263+
// if Py_Finalize() has been called.
2264+
//
2265+
// When this function is called by a daemon thread after Py_Finalize() has been
2266+
// called, the GIL does no longer exist.
2267+
//
2268+
// tstate can be a dangling pointer (point to freed memory): only tstate value
2269+
// is used, the pointer is not deferenced.
2270+
//
2271+
// tstate must be non-NULL.
2272+
int
2273+
_PyThreadState_MustExit(PyThreadState *tstate)
2274+
{
2275+
/* bpo-39877: Access _PyRuntime directly rather than using
2276+
tstate->interp->runtime to support calls from Python daemon threads.
2277+
After Py_Finalize() has been called, tstate can be a dangling pointer:
2278+
point to PyThreadState freed memory. */
2279+
PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
2280+
return (finalizing != NULL && finalizing != tstate);
2281+
}
2282+
2283+
22582284
#ifdef __cplusplus
22592285
}
22602286
#endif

0 commit comments

Comments
 (0)