-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Add a Per-Interpreter "HEAD" Lock #114940
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
Comments
Can this issue be closed? It seems to have been merged. |
What change do you mean? AFAIK, we still only have the global |
Oh, I'm sorry, I didn't seem to check the content of PR carefully. Please forgive my negligence. Edit: |
Any help would be very welcome! 😄 I'd be glad to review your PR. |
Maybe you can wait for my good news. I'll launch it today :). |
I'm not sure if this‘s what you want, please review! Edit:
|
@ericsnowcurrently I wonder whether it's time to change this lock to recursive lock, we have had issues about deadlocks where re-entrant calls can deadlock, ex Py_DECREF. |
If we consider recursive locks, we can try to add a count field to threads and maintain it through atomic operations. |
Yeah, a recursive lock could simplify a few things and/or provide better thread-safety. We do have a number of places where we cut some corners in that regard. That said, there still may be a risk of deadlock, e.g. finalizers that create threads, which we'd have to resolve. |
FWIW, having looked at the PR, I think we should take a little time to make sure we have a clear picture of what should change to the per-interpreter lock. There are also several cases that have possible races that we need to sort out. |
This is a precursor to the actual fix for gh-114940, where we will change these macros to use the new lock. This change is almost entirely mechanical; the exceptions are the loops in codeobject.c and ceval.c, which now hold the "head" lock. Note that almost all of the uses of _Py_FOR_EACH_TSTATE_UNLOCKED() here will change to _Py_FOR_EACH_TSTATE_BEGIN() once we add the new per-interpreter lock.
…hongh-127077) This is a precursor to the actual fix for pythongh-114940, where we will change these macros to use the new lock. This change is almost entirely mechanical; the exceptions are the loops in codeobject.c and ceval.c, which now hold the "head" lock. Note that almost all of the uses of _Py_FOR_EACH_TSTATE_UNLOCKED() here will change to _Py_FOR_EACH_TSTATE_BEGIN() once we add the new per-interpreter lock.
Feature or enhancement
We have a lock that we use for thread-safety when working with the set of interpreters:
_PyRuntimeState.interpreters.mutex
. We use two macros in several core files to manage that lock:HEAD_LOCK()
andHEAD_UNLOCK()
(from pycore_pystate.h).Recently we've begun using that runtime-global lock for thread-safety when working with a single interpreter's set of thread states (
PyInterpreterState.threads
). The problem is that this means we are potentially blocking interpreters unnecessarily.Thus it makes sense to introduce an interpreter-specific "head" lock:
PyInterpreterState.threads.mutex
(plus corresponding macros).Linked PRs
PyInterpreterState.threads
#125561The text was updated successfully, but these errors were encountered: