-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
414cfef
Add threads.mutex member to PyInterpreState
rruuaanng 0c815fc
Add NEWS
rruuaanng f5ec949
Delete Misc/NEWS.d/next/Core_and_Builtins/2024-10-16-11-07-50.gh-issu…
rruuaanng b595153
Change to current thread mutex
rruuaanng 2378caa
Remove runtime variable
rruuaanng 9cb2de0
HEAD_LOCK change to INTERP_THREAD_LOCK
rruuaanng afbaf04
Merge branch 'main' into _gh114940
rruuaanng 29c4ccb
HEAD_LOCK change to INTERP_THREAD_LOCK
rruuaanng 462f97d
HEAD_LOCK change to INTERP_THREAD_LOCK
rruuaanng 8146b6f
Remove unused variable
rruuaanng 74cc060
Clear unused code
rruuaanng f30b2b1
Change comment
rruuaanng 8e0d324
INTERP_THREAD_LOCK rename to INTERP_HEAD_LOCK
rruuaanng 1f488c7
INTERP_THREAD_LOCK rename to INTERP_HEAD_LOCK
rruuaanng c99e44e
INTERP_THREAD_LOCK rename to INTERP_HEAD_LOCK
rruuaanng 7ad9c32
Using raw member
rruuaanng 2131d69
Fix possible race conditions
rruuaanng 7bf9a1c
INTERP_THREAD_LOCK rename to INTERP_HEAD_LOCK
rruuaanng b947bc9
Rollback to unmodified
rruuaanng 46c2709
Use variable
rruuaanng 3c208c2
Recover assert
rruuaanng 2bb60d9
Add mutex for get_mimalloc_allocated_blocks
rruuaanng 3798bed
Add mutex for get_reftotal
rruuaanng 5dc1ced
Merge branch 'main' into _gh114940
rruuaanng 18fbaa0
Merge branch 'main' into _gh114940
rruuaanng 6257837
add lock to codeobject
rruuaanng 4b9a569
clear ci warning
rruuaanng 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
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); | ||
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; | ||
} | ||
|
||
|
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.
Uh oh!
There was an error while loading. Please reload this page.