Skip to content

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

Merged
merged 6 commits into from
Oct 4, 2019

Conversation

nanjekyejoannah
Copy link
Contributor

@nanjekyejoannah nanjekyejoannah commented Oct 3, 2019

Reverting the removal of PyThreadState_DeleteCurrent() with documentation.

https://bugs.python.org/issue38266

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`.
Copy link
Member

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.

Copy link
Member

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);
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

@@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prototype must be:

Suggested change
PyAPI_FUNC(void) PyThreadState_DeleteCurrent();
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);

@@ -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(). */
Copy link
Member

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.

@@ -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(). */
Copy link
Member

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()
Copy link
Member

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:

Suggested change
PyThreadState_DeleteCurrent()
PyThreadState_DeleteCurrent(void)

@vstinner
Copy link
Member

vstinner commented Oct 3, 2019

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.

@nanjekyejoannah
Copy link
Contributor Author

@vstinner , I have addressed the changes. PTAL

Copy link
Member

@vstinner vstinner left a 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`.
Copy link
Member

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);
Copy link
Member

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);
Copy link
Member

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.

@vstinner
Copy link
Member

vstinner commented Oct 4, 2019

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.)

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@vstinner
Copy link
Member

vstinner commented Oct 4, 2019

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.

@vstinner vstinner merged commit 8855e47 into python:master Oct 4, 2019
@miss-islington
Copy link
Contributor

Thanks @nanjekyejoannah for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

I'm having trouble backporting to 3.8. Reason: 'Error 110 while writing to socket. Connection timed out.'. Please retry by removing and re-adding the needs backport to 3.8 label.

@miss-islington
Copy link
Contributor

Thanks @nanjekyejoannah for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @nanjekyejoannah and @vstinner, I could not cleanly backport this to 3.8 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 8855e47d09d4db1206c65b24efc8ad0df585ac46 3.8

@miss-islington
Copy link
Contributor

Thanks @nanjekyejoannah for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @nanjekyejoannah and @vstinner, I could not cleanly backport this to 3.8 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 8855e47d09d4db1206c65b24efc8ad0df585ac46 3.8

jacobneiltaylor pushed a commit to jacobneiltaylor/cpython that referenced this pull request Dec 5, 2019
…nal (pythonGH-16558)

Revert the removal of PyThreadState_DeleteCurrent() with documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants