-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-140746: Fix Thread.start() that can hang indefinitely - v2 #144750
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix :func:`threading.Thread.start` that can hang indefinitely in case of heap memory exhaustion | ||
| during initialization of the new thread. | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -103,7 +103,8 @@ typedef enum { | |||||
| THREAD_HANDLE_NOT_STARTED = 1, | ||||||
| THREAD_HANDLE_STARTING = 2, | ||||||
| THREAD_HANDLE_RUNNING = 3, | ||||||
| THREAD_HANDLE_DONE = 4, | ||||||
| THREAD_HANDLE_FAILED = 4, | ||||||
| THREAD_HANDLE_DONE = 5, | ||||||
| } ThreadHandleState; | ||||||
|
|
||||||
| // A handle to wait for thread completion. | ||||||
|
|
@@ -139,6 +140,7 @@ typedef struct { | |||||
|
|
||||||
| PyMutex mutex; | ||||||
|
|
||||||
| PyEvent thread_is_bootstraped; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "bootstraped" is misspelled everywhere (should be bootstrapped). |
||||||
| // Set immediately before `thread_run` returns to indicate that the OS | ||||||
| // thread is about to exit. This is used to avoid false positives when | ||||||
| // detecting self-join attempts. See the comment in `ThreadHandle_join()` | ||||||
|
|
@@ -231,6 +233,7 @@ ThreadHandle_new(void) | |||||
| self->os_handle = 0; | ||||||
| self->has_os_handle = 0; | ||||||
| self->thread_is_exiting = (PyEvent){0}; | ||||||
| self->thread_is_bootstraped = (PyEvent){0}; | ||||||
| self->mutex = (PyMutex){_Py_UNLOCKED}; | ||||||
| self->once = (_PyOnceFlag){0}; | ||||||
| self->state = THREAD_HANDLE_NOT_STARTED; | ||||||
|
|
@@ -286,7 +289,8 @@ ThreadHandle_decref(ThreadHandle *self) | |||||
| // 1. This is the destructor; nothing else holds a reference. | ||||||
| // 2. The refcount going to zero is a "synchronizes-with" event; all | ||||||
| // changes from other threads are visible. | ||||||
| if (self->state == THREAD_HANDLE_RUNNING && !detach_thread(self)) { | ||||||
| if ((self->state == THREAD_HANDLE_RUNNING || self->state == THREAD_HANDLE_FAILED) | ||||||
| && !detach_thread(self)) { | ||||||
| self->state = THREAD_HANDLE_DONE; | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -322,6 +326,7 @@ _PyThread_AfterFork(struct _pythread_runtime_state *state) | |||||
| handle->once = (_PyOnceFlag){_Py_ONCE_INITIALIZED}; | ||||||
| handle->mutex = (PyMutex){_Py_UNLOCKED}; | ||||||
| _PyEvent_Notify(&handle->thread_is_exiting); | ||||||
| _PyEvent_Notify(&handle->thread_is_bootstraped); | ||||||
| llist_remove(node); | ||||||
| remove_from_shutdown_handles(handle); | ||||||
| } | ||||||
|
|
@@ -393,6 +398,9 @@ thread_run(void *boot_raw) | |||||
| PyErr_FormatUnraisable( | ||||||
| "Exception ignored in thread started by %R", boot->func); | ||||||
| } | ||||||
| // Notify that the bootstraped is done and failed (e.g. Memory error). | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this makes more sense:
Suggested change
|
||||||
| set_thread_handle_state(handle, THREAD_HANDLE_FAILED); | ||||||
| _PyEvent_Notify(&handle->thread_is_bootstraped); | ||||||
| } | ||||||
| else { | ||||||
| Py_DECREF(res); | ||||||
|
|
@@ -502,7 +510,10 @@ static int | |||||
| join_thread(void *arg) | ||||||
| { | ||||||
| ThreadHandle *handle = (ThreadHandle*)arg; | ||||||
| assert(get_thread_handle_state(handle) == THREAD_HANDLE_RUNNING); | ||||||
| assert( | ||||||
| get_thread_handle_state(handle) == THREAD_HANDLE_RUNNING || | ||||||
| get_thread_handle_state(handle) == THREAD_HANDLE_FAILED | ||||||
| ); | ||||||
| PyThread_handle_t os_handle; | ||||||
| if (ThreadHandle_get_os_handle(handle, &os_handle)) { | ||||||
| int err = 0; | ||||||
|
|
@@ -707,6 +718,46 @@ PyThreadHandleObject_join(PyObject *op, PyObject *args) | |||||
| Py_RETURN_NONE; | ||||||
| } | ||||||
|
|
||||||
| static PyObject * | ||||||
| PyThreadHandleObject_is_bootstraped(PyObject *op, PyObject *Py_UNUSED(dummy)) | ||||||
| { | ||||||
| PyThreadHandleObject *self = PyThreadHandleObject_CAST(op); | ||||||
| if (_PyEvent_IsSet(&self->handle->thread_is_bootstraped)) { | ||||||
| Py_RETURN_TRUE; | ||||||
| } | ||||||
| else { | ||||||
| Py_RETURN_FALSE; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| static PyObject * | ||||||
| PyThreadHandleObject_wait_bootstraped(PyObject *op, PyObject *Py_UNUSED(dummy)) | ||||||
| { | ||||||
| PyThreadHandleObject *self = PyThreadHandleObject_CAST(op); | ||||||
| PyEvent_Wait(&self->handle->thread_is_bootstraped); | ||||||
| Py_RETURN_NONE; | ||||||
| } | ||||||
|
|
||||||
| static PyObject * | ||||||
| PyThreadHandleObject_set_bootstraped(PyObject *op, PyObject *Py_UNUSED(dummy)) | ||||||
| { | ||||||
| PyThreadHandleObject *self = PyThreadHandleObject_CAST(op); | ||||||
| _PyEvent_Notify(&self->handle->thread_is_bootstraped); | ||||||
| Py_RETURN_NONE; | ||||||
| } | ||||||
|
|
||||||
| static PyObject * | ||||||
| PyThreadHandleObject_is_failed(PyObject *op, PyObject *Py_UNUSED(dummy)) | ||||||
| { | ||||||
| PyThreadHandleObject *self = PyThreadHandleObject_CAST(op); | ||||||
| if (get_thread_handle_state(self->handle) == THREAD_HANDLE_FAILED) { | ||||||
| Py_RETURN_TRUE; | ||||||
| } | ||||||
| else { | ||||||
| Py_RETURN_FALSE; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| static PyObject * | ||||||
| PyThreadHandleObject_is_done(PyObject *op, PyObject *Py_UNUSED(dummy)) | ||||||
| { | ||||||
|
|
@@ -740,6 +791,10 @@ static PyGetSetDef ThreadHandle_getsetlist[] = { | |||||
| static PyMethodDef ThreadHandle_methods[] = { | ||||||
| {"join", PyThreadHandleObject_join, METH_VARARGS, NULL}, | ||||||
| {"_set_done", PyThreadHandleObject_set_done, METH_NOARGS, NULL}, | ||||||
| {"wait_bootstraped", PyThreadHandleObject_wait_bootstraped, METH_NOARGS, NULL}, | ||||||
| {"set_bootstraped", PyThreadHandleObject_set_bootstraped, METH_NOARGS, NULL}, | ||||||
| {"is_bootstraped", PyThreadHandleObject_is_bootstraped, METH_NOARGS, NULL}, | ||||||
| {"is_failed", PyThreadHandleObject_is_failed, METH_NOARGS, NULL}, | ||||||
| {"is_done", PyThreadHandleObject_is_done, METH_NOARGS, NULL}, | ||||||
| {0, 0} | ||||||
| }; | ||||||
|
|
@@ -2025,8 +2080,8 @@ thread_PyThread_start_joinable_thread(PyObject *module, PyObject *fargs, | |||||
| hobj) < 0) { | ||||||
| return NULL; | ||||||
| } | ||||||
|
|
||||||
| if (hobj == Py_None) { | ||||||
| int no_handle_arg = (hobj == Py_None); | ||||||
| if (no_handle_arg) { | ||||||
| hobj = (PyObject *)PyThreadHandleObject_new(state->thread_handle_type); | ||||||
| if (hobj == NULL) { | ||||||
| return NULL; | ||||||
|
|
@@ -2047,6 +2102,23 @@ thread_PyThread_start_joinable_thread(PyObject *module, PyObject *fargs, | |||||
| Py_DECREF(hobj); | ||||||
| return NULL; | ||||||
| } | ||||||
|
|
||||||
| // gh-140746: catch error before thread really start | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| PyThreadHandleObject *thread_handle = PyThreadHandleObject_CAST(hobj); | ||||||
| if (no_handle_arg) { | ||||||
| // If the handle is created by this function, we can be sure that | ||||||
| // the thread is not started before this point. Here, we simulate | ||||||
| // the thread bootstrap process. | ||||||
| _PyEvent_Notify(&thread_handle->handle->thread_is_bootstraped); | ||||||
| } | ||||||
| PyEvent_Wait(&thread_handle->handle->thread_is_bootstraped); | ||||||
|
|
||||||
| if (get_thread_handle_state(thread_handle->handle) == THREAD_HANDLE_FAILED) { | ||||||
| PyErr_SetString(ThreadError, "call to/in _bootstrap/_bootstrap_inner failed"); | ||||||
| Py_DECREF(hobj); | ||||||
| return NULL; | ||||||
| } | ||||||
|
|
||||||
| return (PyObject *) hobj; | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -2467,6 +2539,7 @@ thread__make_thread_handle(PyObject *module, PyObject *identobj) | |||||
| hobj->handle->ident = ident; | ||||||
| hobj->handle->state = THREAD_HANDLE_RUNNING; | ||||||
| PyMutex_Unlock(&hobj->handle->mutex); | ||||||
| _PyEvent_Notify(&hobj->handle->thread_is_bootstraped); | ||||||
| return (PyObject*) hobj; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
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.
Maybe add something about the new
FAILEDstate that is managed by the C code?