-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-38266: Revert bpo-37878: Make PyThreadState_DeleteCurrent() Internal #16558
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
Conversation
Destroy the current thread state and release the global interpreter lock. | ||
Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not | ||
be held. The thread state must have been reset with a previous call | ||
to :c:func:`PyThreadState_Clear`. |
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 we start to "expose" this function, would it be possible to be less strict and call PyThreadState_Clear() in PyThreadState_DeleteCurrent()? It sems like calling PyThreadState_Clear() twice is safe.
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.
I marked https://bugs.python.org/issue38266 as a release blocker, so the PyThreadState_Clear() question can definitely wait after 3.8.0: we can even revisit this in Python 3.9. Let's stick to adding back the function unmodified.
@@ -110,8 +110,6 @@ PyAPI_FUNC(void) _PyErr_Print(PyThreadState *tstate); | |||
PyAPI_FUNC(void) _PyErr_Display(PyObject *file, PyObject *exception, | |||
PyObject *value, PyObject *tb); | |||
|
|||
PyAPI_FUNC(void) _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime); |
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.
I don't understand why do you remove this definition: it's still used, no?
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.
I am now exposing PyThreadState_DeleteCurrent() instead of the internal _PyThreadState_DeleteCurrent() I thought. what are you thinking?
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.
_PyThreadState_DeleteCurrent() has to stay, it's part of https://pythoncapi.readthedocs.io/runtime.html effort to avoid global variables.
Include/cpython/pystate.h
Outdated
@@ -183,6 +183,7 @@ PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void); | |||
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *); | |||
PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *); | |||
PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *); | |||
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(); |
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.
The prototype must be:
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(); | |
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void); |
Modules/_tracemalloc.c
Outdated
@@ -34,7 +34,7 @@ static struct { | |||
#if defined(TRACE_RAW_MALLOC) | |||
/* This lock is needed because tracemalloc_free() is called without | |||
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it | |||
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */ | |||
would introduce a deadlock in PyThreadState_DeleteCurrent(). */ |
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.
I prefer to keep _PyThreadState_DeleteCurrent() here. PyThreadState_DeleteCurrent() is only a public helper function, _PyThreadState_DeleteCurrent() is the real implementation.
Modules/_tracemalloc.c
Outdated
@@ -728,7 +728,7 @@ tracemalloc_free(void *ctx, void *ptr) | |||
return; | |||
|
|||
/* GIL cannot be locked in PyMem_RawFree() because it would introduce | |||
a deadlock in _PyThreadState_DeleteCurrent(). */ | |||
a deadlock in PyThreadState_DeleteCurrent(). */ |
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.
ditto: keep _PyThreadState_DeleteCurrent()
Python/pystate.c
Outdated
@@ -874,6 +874,12 @@ _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime) | |||
PyEval_ReleaseLock(); | |||
} | |||
|
|||
void | |||
PyThreadState_DeleteCurrent() |
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.
Please add void to prevent accepting any argument:
PyThreadState_DeleteCurrent() | |
PyThreadState_DeleteCurrent(void) |
Once this change lands, it would be interested to add an unit test, but I'm not sure how to write such test. C code in _testcapi I guess. |
@vstinner , I have addressed the changes. PTAL |
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.
LGTM.
Destroy the current thread state and release the global interpreter lock. | ||
Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not | ||
be held. The thread state must have been reset with a previous call | ||
to :c:func:`PyThreadState_Clear`. |
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.
I marked https://bugs.python.org/issue38266 as a release blocker, so the PyThreadState_Clear() question can definitely wait after 3.8.0: we can even revisit this in Python 3.9. Let's stick to adding back the function unmodified.
@@ -110,8 +110,6 @@ PyAPI_FUNC(void) _PyErr_Print(PyThreadState *tstate); | |||
PyAPI_FUNC(void) _PyErr_Display(PyObject *file, PyObject *exception, | |||
PyObject *value, PyObject *tb); | |||
|
|||
PyAPI_FUNC(void) _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime); |
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.
_PyThreadState_DeleteCurrent() has to stay, it's part of https://pythoncapi.readthedocs.io/runtime.html effort to avoid global variables.
@@ -108,8 +108,7 @@ PyAPI_FUNC(PyObject*) _PyErr_WriteUnraisableDefaultHook(PyObject *unraisable); | |||
|
|||
PyAPI_FUNC(void) _PyErr_Print(PyThreadState *tstate); | |||
PyAPI_FUNC(void) _PyErr_Display(PyObject *file, PyObject *exception, | |||
PyObject *value, PyObject *tb); | |||
|
|||
PyObject *value, PyObject *tb); |
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.
nitpick: it seems like you removed a newline or something else. you may revert that.
According to https://bugs.python.org/issue38266 discussion, it's a Python 3.8 regression and so this change must be backported to 3.8. I suggest to merge this change into 3.8 and master as soon as possible, to attempt to get it into 3.8.0 final. (But it's ok if we miss the final, it can wait for 3.8.1 otherwise.) |
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.
LGTM.
Usually, we let core developers merge their own pull requests. But. Joannah explicitly asked me to merge her PR if I want. Since this change fix a Python 3.8 regression and the next 3.8.0 release (which may be a rc2 or the final) is in 3 days, I prefer to merge it ASAP. |
Thanks @nanjekyejoannah for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8. |
I'm having trouble backporting to |
Thanks @nanjekyejoannah for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8. |
Sorry, @nanjekyejoannah and @vstinner, I could not cleanly backport this to |
Thanks @nanjekyejoannah for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8. |
Sorry, @nanjekyejoannah and @vstinner, I could not cleanly backport this to |
…nal (pythonGH-16558) Revert the removal of PyThreadState_DeleteCurrent() with documentation.
Reverting the removal of
PyThreadState_DeleteCurrent()
with documentation.https://bugs.python.org/issue38266