-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-114940: Add a Per-Interpreter Lock For the List of Thread States #127037
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
Closed
ericsnowcurrently
wants to merge
4
commits into
python:main
from
ericsnowcurrently:interp-threads-lock
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,17 +789,17 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) | |
} | ||
|
||
// Clear the current/main thread state last. | ||
HEAD_LOCK(runtime); | ||
THREADS_HEAD_LOCK(interp); | ||
PyThreadState *p = interp->threads.head; | ||
HEAD_UNLOCK(runtime); | ||
THREADS_HEAD_UNLOCK(interp); | ||
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); | ||
THREADS_HEAD_LOCK(interp); | ||
p = p->next; | ||
HEAD_UNLOCK(runtime); | ||
THREADS_HEAD_UNLOCK(interp); | ||
} | ||
if (tstate->interp == interp) { | ||
/* We fix tstate->_status below when we for sure aren't using it | ||
|
@@ -1539,7 +1538,7 @@ new_threadstate(PyInterpreterState *interp, int whence) | |
#endif | ||
|
||
/* We serialize concurrent creation to protect global state. */ | ||
HEAD_LOCK(interp->runtime); | ||
THREADS_HEAD_LOCK(interp); | ||
|
||
// Initialize the new thread state. | ||
interp->threads.next_unique_id += 1; | ||
|
@@ -1550,7 +1549,7 @@ new_threadstate(PyInterpreterState *interp, int whence) | |
PyThreadState *old_head = interp->threads.head; | ||
add_threadstate(interp, (PyThreadState *)tstate, old_head); | ||
|
||
HEAD_UNLOCK(interp->runtime); | ||
THREADS_HEAD_UNLOCK(interp); | ||
|
||
#ifdef Py_GIL_DISABLED | ||
// Must be called with lock unlocked to avoid lock ordering deadlocks. | ||
|
@@ -1741,7 +1740,7 @@ tstate_delete_common(PyThreadState *tstate, int release_gil) | |
} | ||
_PyRuntimeState *runtime = interp->runtime; | ||
|
||
HEAD_LOCK(runtime); | ||
THREADS_HEAD_LOCK(interp); | ||
if (tstate->prev) { | ||
tstate->prev->next = tstate->next; | ||
} | ||
|
@@ -1757,9 +1756,11 @@ tstate_delete_common(PyThreadState *tstate, int release_gil) | |
if (interp->stoptheworld.requested) { | ||
decrement_stoptheworld_countdown(&interp->stoptheworld); | ||
} | ||
HEAD_LOCK(runtime); | ||
if (runtime->stoptheworld.requested) { | ||
decrement_stoptheworld_countdown(&runtime->stoptheworld); | ||
} | ||
HEAD_UNLOCK(runtime); | ||
Comment on lines
+1759
to
+1763
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 is correct, but need to double-check. |
||
} | ||
|
||
#if defined(Py_REF_DEBUG) && defined(Py_GIL_DISABLED) | ||
|
@@ -1770,7 +1771,7 @@ tstate_delete_common(PyThreadState *tstate, int release_gil) | |
assert(tstate_impl->refcounts.values == NULL); | ||
#endif | ||
|
||
HEAD_UNLOCK(runtime); | ||
THREADS_HEAD_UNLOCK(interp); | ||
|
||
// XXX Unbind in PyThreadState_Clear(), or earlier | ||
// (and assert not-equal here)? | ||
|
@@ -1851,13 +1852,15 @@ _PyThreadState_RemoveExcept(PyThreadState *tstate) | |
{ | ||
assert(tstate != NULL); | ||
PyInterpreterState *interp = tstate->interp; | ||
_PyRuntimeState *runtime = interp->runtime; | ||
|
||
#ifdef Py_GIL_DISABLED | ||
#ifndef NDEBUG | ||
_PyRuntimeState *runtime = interp->runtime; | ||
#endif | ||
assert(runtime->stoptheworld.world_stopped); | ||
#endif | ||
|
||
HEAD_LOCK(runtime); | ||
THREADS_HEAD_LOCK(interp); | ||
/* Remove all thread states, except tstate, from the linked list of | ||
thread states. */ | ||
PyThreadState *list = interp->threads.head; | ||
|
@@ -1872,7 +1875,7 @@ _PyThreadState_RemoveExcept(PyThreadState *tstate) | |
} | ||
tstate->prev = tstate->next = NULL; | ||
interp->threads.head = tstate; | ||
HEAD_UNLOCK(runtime); | ||
THREADS_HEAD_UNLOCK(interp); | ||
|
||
return list; | ||
} | ||
|
@@ -2339,7 +2342,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 +2350,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); | ||
THREADS_HEAD_LOCK(interp); | ||
for (PyThreadState *tstate = interp->threads.head; tstate != NULL; tstate = tstate->next) { | ||
if (tstate->thread_id != id) { | ||
continue; | ||
|
@@ -2363,13 +2365,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); | ||
THREADS_HEAD_UNLOCK(interp); | ||
|
||
Py_XDECREF(old_exc); | ||
_Py_set_eval_breaker_bit(tstate, _PY_ASYNC_EXCEPTION_BIT); | ||
return 1; | ||
} | ||
HEAD_UNLOCK(runtime); | ||
THREADS_HEAD_UNLOCK(interp); | ||
return 0; | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If I remember correctly, this was an issue that could not be changed because it would cause unexpected issues with CI (it was originally changed in my PR)