aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Modules/_threadmodule.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.com>2023-10-03 23:32:21 +0300
committershadchin <shadchin@yandex-team.com>2023-10-03 23:48:51 +0300
commit01ffd024041ac933854c367fb8d1b5682d19883f (patch)
treeb70aa497ba132a133ccece49f7763427dcd0743f /contrib/tools/python3/src/Modules/_threadmodule.c
parenta33fdb9a34581fd124e92535153b1f1fdeca6aaf (diff)
downloadydb-01ffd024041ac933854c367fb8d1b5682d19883f.tar.gz
Update Python 3 to 3.11.6
Diffstat (limited to 'contrib/tools/python3/src/Modules/_threadmodule.c')
-rw-r--r--contrib/tools/python3/src/Modules/_threadmodule.c71
1 files changed, 53 insertions, 18 deletions
diff --git a/contrib/tools/python3/src/Modules/_threadmodule.c b/contrib/tools/python3/src/Modules/_threadmodule.c
index 199e3b89d9..625e4e6e77 100644
--- a/contrib/tools/python3/src/Modules/_threadmodule.c
+++ b/contrib/tools/python3/src/Modules/_threadmodule.c
@@ -485,6 +485,18 @@ PyDoc_STRVAR(rlock_release_save_doc,
\n\
For internal use by `threading.Condition`.");
+static PyObject *
+rlock_recursion_count(rlockobject *self, PyObject *Py_UNUSED(ignored))
+{
+ unsigned long tid = PyThread_get_thread_ident();
+ return PyLong_FromUnsignedLong(
+ self->rlock_owner == tid ? self->rlock_count : 0UL);
+}
+
+PyDoc_STRVAR(rlock_recursion_count_doc,
+"_recursion_count() -> int\n\
+\n\
+For internal use by reentrancy checks.");
static PyObject *
rlock_is_owned(rlockobject *self, PyObject *Py_UNUSED(ignored))
@@ -560,6 +572,8 @@ static PyMethodDef rlock_methods[] = {
METH_VARARGS, rlock_acquire_restore_doc},
{"_release_save", (PyCFunction)rlock_release_save,
METH_NOARGS, rlock_release_save_doc},
+ {"_recursion_count", (PyCFunction)rlock_recursion_count,
+ METH_NOARGS, rlock_recursion_count_doc},
{"__enter__", _PyCFunction_CAST(rlock_acquire),
METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc},
{"__exit__", (PyCFunction)rlock_release,
@@ -1053,22 +1067,22 @@ _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
/* Module functions */
struct bootstate {
- PyInterpreterState *interp;
+ PyThreadState *tstate;
PyObject *func;
PyObject *args;
PyObject *kwargs;
- PyThreadState *tstate;
- _PyRuntimeState *runtime;
};
static void
-thread_bootstate_free(struct bootstate *boot)
+thread_bootstate_free(struct bootstate *boot, int decref)
{
- Py_DECREF(boot->func);
- Py_DECREF(boot->args);
- Py_XDECREF(boot->kwargs);
- PyMem_Free(boot);
+ if (decref) {
+ Py_DECREF(boot->func);
+ Py_DECREF(boot->args);
+ Py_XDECREF(boot->kwargs);
+ }
+ PyMem_RawFree(boot);
}
@@ -1076,9 +1090,27 @@ static void
thread_run(void *boot_raw)
{
struct bootstate *boot = (struct bootstate *) boot_raw;
- PyThreadState *tstate;
+ PyThreadState *tstate = boot->tstate;
+
+ // gh-108987: If _thread.start_new_thread() is called before or while
+ // Python is being finalized, thread_run() can called *after*.
+ // _PyRuntimeState_SetFinalizing() is called. At this point, all Python
+ // threads must exit, except of the thread calling Py_Finalize() whch holds
+ // the GIL and must not exit.
+ //
+ // At this stage, tstate can be a dangling pointer (point to freed memory),
+ // it's ok to call _PyThreadState_MustExit() with a dangling pointer.
+ if (_PyThreadState_MustExit(tstate)) {
+ // Don't call PyThreadState_Clear() nor _PyThreadState_DeleteCurrent().
+ // These functions are called on tstate indirectly by Py_Finalize()
+ // which calls _PyInterpreterState_Clear().
+ //
+ // Py_DECREF() cannot be called because the GIL is not held: leak
+ // references on purpose. Python is being finalized anyway.
+ thread_bootstate_free(boot, 0);
+ goto exit;
+ }
- tstate = boot->tstate;
tstate->thread_id = PyThread_get_thread_ident();
#ifdef PY_HAVE_THREAD_NATIVE_ID
tstate->native_thread_id = PyThread_get_thread_native_id();
@@ -1102,20 +1134,22 @@ thread_run(void *boot_raw)
Py_DECREF(res);
}
- thread_bootstate_free(boot);
+ thread_bootstate_free(boot, 1);
+
tstate->interp->threads.count--;
PyThreadState_Clear(tstate);
_PyThreadState_DeleteCurrent(tstate);
+exit:
// bpo-44434: Don't call explicitly PyThread_exit_thread(). On Linux with
// the glibc, pthread_exit() can abort the whole process if dlopen() fails
// to open the libgcc_s.so library (ex: EMFILE error).
+ return;
}
static PyObject *
thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
{
- _PyRuntimeState *runtime = &_PyRuntime;
PyObject *func, *args, *kwargs = NULL;
if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
@@ -1144,17 +1178,18 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
return NULL;
}
- struct bootstate *boot = PyMem_NEW(struct bootstate, 1);
+ // gh-109795: Use PyMem_RawMalloc() instead of PyMem_Malloc(),
+ // because it should be possible to call thread_bootstate_free()
+ // without holding the GIL.
+ struct bootstate *boot = PyMem_RawMalloc(sizeof(struct bootstate));
if (boot == NULL) {
return PyErr_NoMemory();
}
- boot->interp = _PyInterpreterState_GET();
- boot->tstate = _PyThreadState_Prealloc(boot->interp);
+ boot->tstate = _PyThreadState_Prealloc(interp);
if (boot->tstate == NULL) {
- PyMem_Free(boot);
+ PyMem_RawFree(boot);
return PyErr_NoMemory();
}
- boot->runtime = runtime;
boot->func = Py_NewRef(func);
boot->args = Py_NewRef(args);
boot->kwargs = Py_XNewRef(kwargs);
@@ -1163,7 +1198,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
if (ident == PYTHREAD_INVALID_THREAD_ID) {
PyErr_SetString(ThreadError, "can't start new thread");
PyThreadState_Clear(boot->tstate);
- thread_bootstate_free(boot);
+ thread_bootstate_free(boot, 1);
return NULL;
}
return PyLong_FromUnsignedLong(ident);