Skip to content

Commit 6cf1dec

Browse files
committed
pythongh-108987: Fix _thread.start_new_thread() race condition
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.
1 parent f63d378 commit 6cf1dec

File tree

5 files changed

+72
-40
lines changed

5 files changed

+72
-40
lines changed

Include/internal/pycore_pystate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ extern _Py_thread_local PyThreadState *_Py_tss_tstate;
7171
extern int _PyThreadState_CheckConsistency(PyThreadState *tstate);
7272
#endif
7373

74+
int _PyThreadState_MustExit(PyThreadState *tstate);
75+
7476
// Export for most shared extensions, used via _PyThreadState_GET() static
7577
// inline function.
7678
PyAPI_FUNC(PyThreadState *) _PyThreadState_GetCurrent(void);
Lines changed: 4 additions & 0 deletions
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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,21 +1051,21 @@ _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
10511051
/* Module functions */
10521052

10531053
struct bootstate {
1054-
PyInterpreterState *interp;
1054+
PyThreadState *tstate;
10551055
PyObject *func;
10561056
PyObject *args;
10571057
PyObject *kwargs;
1058-
PyThreadState *tstate;
1059-
_PyRuntimeState *runtime;
10601058
};
10611059

10621060

10631061
static void
1064-
thread_bootstate_free(struct bootstate *boot)
1062+
thread_bootstate_free(struct bootstate *boot, int decref)
10651063
{
1066-
Py_DECREF(boot->func);
1067-
Py_DECREF(boot->args);
1068-
Py_XDECREF(boot->kwargs);
1064+
if (decref) {
1065+
Py_DECREF(boot->func);
1066+
Py_DECREF(boot->args);
1067+
Py_XDECREF(boot->kwargs);
1068+
}
10691069
PyMem_Free(boot);
10701070
}
10711071

@@ -1076,35 +1076,61 @@ thread_run(void *boot_raw)
10761076
struct bootstate *boot = (struct bootstate *) boot_raw;
10771077
PyThreadState *tstate = boot->tstate;
10781078

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

1102+
PyObject *func = boot->func;
1103+
PyObject *args = boot->args;
1104+
PyObject *kwargs = boot->kwargs;
1105+
10831106
_PyThreadState_Bind(tstate);
10841107
PyEval_AcquireThread(tstate);
10851108
tstate->interp->threads.count++;
10861109

1087-
PyObject *res = PyObject_Call(boot->func, boot->args, boot->kwargs);
1110+
thread_bootstate_free(boot, 1);
1111+
1112+
PyObject *res = PyObject_Call(func, args, kwargs);
10881113
if (res == NULL) {
10891114
if (PyErr_ExceptionMatches(PyExc_SystemExit))
10901115
/* SystemExit is ignored silently */
10911116
PyErr_Clear();
10921117
else {
1093-
_PyErr_WriteUnraisableMsg("in thread started by", boot->func);
1118+
_PyErr_WriteUnraisableMsg("in thread started by", func);
10941119
}
10951120
}
10961121
else {
10971122
Py_DECREF(res);
10981123
}
10991124

1100-
thread_bootstate_free(boot);
11011125
tstate->interp->threads.count--;
11021126
PyThreadState_Clear(tstate);
11031127
_PyThreadState_DeleteCurrent(tstate);
11041128

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

11101136
static PyObject *
@@ -1128,7 +1154,6 @@ and False otherwise.\n");
11281154
static PyObject *
11291155
thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
11301156
{
1131-
_PyRuntimeState *runtime = &_PyRuntime;
11321157
PyObject *func, *args, *kwargs = NULL;
11331158

11341159
if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
@@ -1171,16 +1196,14 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
11711196
if (boot == NULL) {
11721197
return PyErr_NoMemory();
11731198
}
1174-
boot->interp = _PyInterpreterState_GET();
1175-
boot->tstate = _PyThreadState_New(boot->interp);
1199+
boot->tstate = _PyThreadState_New(interp);
11761200
if (boot->tstate == NULL) {
11771201
PyMem_Free(boot);
11781202
if (!PyErr_Occurred()) {
11791203
return PyErr_NoMemory();
11801204
}
11811205
return NULL;
11821206
}
1183-
boot->runtime = runtime;
11841207
boot->func = Py_NewRef(func);
11851208
boot->args = Py_NewRef(args);
11861209
boot->kwargs = Py_XNewRef(kwargs);
@@ -1189,7 +1212,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
11891212
if (ident == PYTHREAD_INVALID_THREAD_ID) {
11901213
PyErr_SetString(ThreadError, "can't start new thread");
11911214
PyThreadState_Clear(boot->tstate);
1192-
thread_bootstate_free(boot);
1215+
thread_bootstate_free(boot, 1);
11931216
return NULL;
11941217
}
11951218
return PyLong_FromUnsignedLong(ident);

Python/ceval_gil.c

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -329,28 +329,6 @@ drop_gil(struct _ceval_state *ceval, PyThreadState *tstate)
329329
}
330330

331331

332-
/* Check if a Python thread must exit immediately, rather than taking the GIL
333-
if Py_Finalize() has been called.
334-
335-
When this function is called by a daemon thread after Py_Finalize() has been
336-
called, the GIL does no longer exist.
337-
338-
tstate must be non-NULL. */
339-
static inline int
340-
tstate_must_exit(PyThreadState *tstate)
341-
{
342-
/* bpo-39877: Access _PyRuntime directly rather than using
343-
tstate->interp->runtime to support calls from Python daemon threads.
344-
After Py_Finalize() has been called, tstate can be a dangling pointer:
345-
point to PyThreadState freed memory. */
346-
PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
347-
if (finalizing == NULL) {
348-
finalizing = _PyInterpreterState_GetFinalizing(tstate->interp);
349-
}
350-
return (finalizing != NULL && finalizing != tstate);
351-
}
352-
353-
354332
/* Take the GIL.
355333
356334
The function saves errno at entry and restores its value at exit.
@@ -366,7 +344,7 @@ take_gil(PyThreadState *tstate)
366344
// XXX It may be more correct to check tstate->_status.finalizing.
367345
// XXX assert(!tstate->_status.cleared);
368346

369-
if (tstate_must_exit(tstate)) {
347+
if (_PyThreadState_MustExit(tstate)) {
370348
/* bpo-39877: If Py_Finalize() has been called and tstate is not the
371349
thread which called Py_Finalize(), exit immediately the thread.
372350
@@ -404,7 +382,7 @@ take_gil(PyThreadState *tstate)
404382
_Py_atomic_load_relaxed(&gil->locked) &&
405383
gil->switch_number == saved_switchnum)
406384
{
407-
if (tstate_must_exit(tstate)) {
385+
if (_PyThreadState_MustExit(tstate)) {
408386
MUTEX_UNLOCK(gil->mutex);
409387
// gh-96387: If the loop requested a drop request in a previous
410388
// iteration, reset the request. Otherwise, drop_gil() can
@@ -444,7 +422,7 @@ take_gil(PyThreadState *tstate)
444422
MUTEX_UNLOCK(gil->switch_mutex);
445423
#endif
446424

447-
if (tstate_must_exit(tstate)) {
425+
if (_PyThreadState_MustExit(tstate)) {
448426
/* bpo-36475: If Py_Finalize() has been called and tstate is not
449427
the thread which called Py_Finalize(), exit immediately the
450428
thread.

Python/pystate.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,31 @@ _PyThreadState_CheckConsistency(PyThreadState *tstate)
29082908
#endif
29092909

29102910

2911+
// Check if a Python thread must exit immediately, rather than taking the GIL
2912+
// if Py_Finalize() has been called.
2913+
//
2914+
// When this function is called by a daemon thread after Py_Finalize() has been
2915+
// called, the GIL does no longer exist.
2916+
//
2917+
// tstate can be a dangling pointer (point to freed memory): only tstate value
2918+
// is used, the pointer is not deferenced.
2919+
//
2920+
// tstate must be non-NULL.
2921+
int
2922+
_PyThreadState_MustExit(PyThreadState *tstate)
2923+
{
2924+
/* bpo-39877: Access _PyRuntime directly rather than using
2925+
tstate->interp->runtime to support calls from Python daemon threads.
2926+
After Py_Finalize() has been called, tstate can be a dangling pointer:
2927+
point to PyThreadState freed memory. */
2928+
PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
2929+
if (finalizing == NULL) {
2930+
finalizing = _PyInterpreterState_GetFinalizing(tstate->interp);
2931+
}
2932+
return (finalizing != NULL && finalizing != tstate);
2933+
}
2934+
2935+
29112936
#ifdef __cplusplus
29122937
}
29132938
#endif

0 commit comments

Comments
 (0)