Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-114940: Use fine-grained mutex protection for PyInterpreterState.threads#125561
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
414cfef0c815fcf5ec949b5951532378caa9cb2de0afbaf0429c4ccb462f97d8146b6f74cc060f30b2b18e0d3241f488c7c99e44e7ad9c322131d697bf9a1cb947bc946c27093c208c22bb60d93798bed5dc1ced18fbaa062578374b9a569File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -773,7 +773,6 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) | ||
| { | ||
| assert(interp != NULL); | ||
| assert(tstate != NULL); | ||
| _PyRuntimeState *runtime = interp->runtime; | ||
| /* XXX Conditions we need to enforce: | ||
| @@ -790,18 +789,16 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) | ||
| } | ||
| // Clear the current/main thread state last. | ||
| HEAD_LOCK(runtime); | ||
| INTERP_HEAD_LOCK(interp); | ||
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 @ericsnowcurrently pointed this out in a different comment, but we should not change where the lock/unlocks happen in this PR. The existing code has a race condition, but holding the lock across the | ||
| PyThreadState *p = interp->threads.head; | ||
| HEAD_UNLOCK(runtime); | ||
| while (p != NULL){ | ||
| // See https://github.com/python/cpython/issues/102126 | ||
| // Must be called without HEAD_LOCK held as it can deadlock | ||
| // if any finalizer tries to acquire that lock. | ||
| PyThreadState_Clear(p); | ||
| HEAD_LOCK(runtime); | ||
| p = p->next; | ||
| HEAD_UNLOCK(runtime); | ||
| } | ||
| INTERP_HEAD_UNLOCK(interp); | ||
| if (tstate->interp == interp){ | ||
| /* We fix tstate->_status below when we for sure aren't using it | ||
| (e.g. no longer need the GIL). */ | ||
| @@ -1851,13 +1848,13 @@ _PyThreadState_RemoveExcept(PyThreadState *tstate) | ||
| { | ||
| assert(tstate != NULL); | ||
| PyInterpreterState *interp = tstate->interp; | ||
| _PyRuntimeState *runtime = interp->runtime; | ||
| #ifdef Py_GIL_DISABLED | ||
| _PyRuntimeState *runtime = interp->runtime; | ||
| assert(runtime->stoptheworld.world_stopped); | ||
| #endif | ||
| HEAD_LOCK(runtime); | ||
| INTERP_HEAD_LOCK(interp); | ||
| /* Remove all thread states, except tstate, from the linked list of | ||
| thread states. */ | ||
| PyThreadState *list = interp->threads.head; | ||
| @@ -1872,7 +1869,7 @@ _PyThreadState_RemoveExcept(PyThreadState *tstate) | ||
| } | ||
| tstate->prev = tstate->next = NULL; | ||
| interp->threads.head = tstate; | ||
| HEAD_UNLOCK(runtime); | ||
| INTERP_HEAD_UNLOCK(interp); | ||
| return list; | ||
| } | ||
| @@ -2339,7 +2336,6 @@ _PyEval_StartTheWorld(PyInterpreterState *interp) | ||
| int | ||
| PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) | ||
| { | ||
| _PyRuntimeState *runtime = &_PyRuntime; | ||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| /* Although the GIL is held, a few C API functions can be called | ||
| @@ -2348,7 +2344,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) | ||
| * list of thread states we're traversing, so to prevent that we lock | ||
| * head_mutex for the duration. | ||
| */ | ||
| HEAD_LOCK(runtime); | ||
| INTERP_HEAD_LOCK(interp); | ||
| for (PyThreadState *tstate = interp->threads.head; tstate != NULL; tstate = tstate->next){ | ||
| if (tstate->thread_id != id){ | ||
| continue; | ||
| @@ -2363,13 +2359,13 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) | ||
| */ | ||
| Py_XINCREF(exc); | ||
| PyObject *old_exc = _Py_atomic_exchange_ptr(&tstate->async_exc, exc); | ||
| HEAD_UNLOCK(runtime); | ||
| INTERP_HEAD_UNLOCK(interp); | ||
| Py_XDECREF(old_exc); | ||
| _Py_set_eval_breaker_bit(tstate, _PY_ASYNC_EXCEPTION_BIT); | ||
| return 1; | ||
| } | ||
| HEAD_UNLOCK(runtime); | ||
| INTERP_HEAD_UNLOCK(interp); | ||
| return 0; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.