Skip to content

bpo-37878: Make PyThreadState_DeleteCurrent() Internal #15315

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 10 commits into from
Sep 5, 2019

Conversation

nanjekyejoannah
Copy link
Contributor

@nanjekyejoannah nanjekyejoannah commented Aug 16, 2019

Make PyThreadState_DeleteCurrent() Internal.

https://bugs.python.org/issue37878

@nanjekyejoannah
Copy link
Contributor Author

cc @ericsnowcurrently

Copy link
Contributor

@willingc willingc left a comment

Choose a reason for hiding this comment

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

Thanks @nanjekyejoannah for the PR. I've made some suggested wording changes for reader clarity.

nanjekyejoannah and others added 2 commits August 17, 2019 20:07
Co-Authored-By: Carol Willing <carolcode@willingconsulting.com>
Co-Authored-By: Carol Willing <carolcode@willingconsulting.com>
@nanjekyejoannah
Copy link
Contributor Author

@willingc Changes committed.

@willingc
Copy link
Contributor

Let's give @ericsnowcurrently a chance to review too. Thanks. Please ping me if this is still open later this week and I will merge.

willingc
willingc previously approved these changes Aug 17, 2019
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.

I'm not sure that it's a good idea to document this function: https://bugs.python.org/issue37878#msg349954

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@nanjekyejoannah nanjekyejoannah changed the title bpo-37878: Document PyThreadState_DeleteCurrent() bpo-37878: Make PyThreadState_DeleteCurrent() Internal Aug 28, 2019
@nanjekyejoannah
Copy link
Contributor Author

@vstinner, I made the function internal instead. PTAL

@@ -1025,7 +1025,7 @@ t_bootstrap(void *boot_raw)
PyMem_DEL(boot_raw);
tstate->interp->num_threads--;
PyThreadState_Clear(tstate);
PyThreadState_DeleteCurrent();
_PyThreadState_DeleteCurrent(&_PyRuntime);
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 "_PyRuntimeState *runtime = &_PyRuntime;" at the beginning of the function, and replace &_PyRuntime with runtime.

Or maybe even better: add the field to "struct bootstate" and fill the field in thread_PyThread_start_new_thread(). It would make t_bootstrap() more "state-less" (don't access directly global variables).

@vstinner vstinner dismissed willingc’s stale review August 29, 2019 13:02

I dismiss Carol's review since Joannah rewrote her PR, and Carol approved the previous version of the PR.

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.

Since it is a public function, even if it's not documented, please document the removal at:
https://docs.python.org/dev/whatsnew/3.9.html#removed

You can mention that the removed function was not documented.

For example, in Python 3.8, I wrote:

"The PyByteArray_Init() and PyByteArray_Fini() functions have been removed. They did nothing since Python 2.7.4 and Python 3.2.0, were excluded from the limited API (stable ABI), and were not documented. (Contributed by Victor Stinner in bpo-35713.)"

@nanjekyejoannah
Copy link
Contributor Author

I have made the requested changes; please review again .

@bedevere-bot
Copy link

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

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 Sep 5, 2019

Thanks @nanjekyejoannah! It now looks better: remove the function rather than documenting it :-) I prefer to have a smaller C API!

lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
)

* Rename PyThreadState_DeleteCurrent()
  to _PyThreadState_DeleteCurrent()
* Move it to the internal C API

Co-Authored-By: Carol Willing <carolcode@willingconsulting.com>
@tacaswell
Copy link
Contributor

This breaks pybind 11. Opened an issue with them (pybind/pybind11#1932). Where this should be fixed is a bit over my head at the moment.

@vstinner
Copy link
Member

This breaks pybind 11. Opened an issue with them (pybind/pybind11#1932). Where this should be fixed is a bit over my head at the moment.

Your gil_scoped_acquire class implementation is scary! Would you mind to open a new issue at bugs.python.org to describe your use case and ask to revert the change which removed PyThreadState_DeleteCurrent?

@wjakob
Copy link
Contributor

wjakob commented Sep 24, 2019

Pybind11 supports some advanced GIL-related tricks that require access this function. For instance, pybind11 can intercept a Python function call on the main() thread and delete the associated thread state, launching a new thread and continuing Python execution there (with a newly created thread state).

Kind of crazy, but why is this useful? UI libraries. On some platforms, it is not legal to poll UI events on any thread other than the main thread. This means that it's impossible to implement a proper UI event loop because Python is hogging the main thread. With the functionality in pybind11's gil_scoped_acquire, I can launch an event polling loop on the main thread, continue running an interactive Python session on another thread, and even swap them back e.g. when the user interface is no longer needed.

@wjakob
Copy link
Contributor

wjakob commented Sep 24, 2019

@vstinner: I provided some feedback on the Python bugtracker and here.

@wjakob
Copy link
Contributor

wjakob commented Sep 24, 2019

DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
)

* Rename PyThreadState_DeleteCurrent()
  to _PyThreadState_DeleteCurrent()
* Move it to the internal C API

Co-Authored-By: Carol Willing <carolcode@willingconsulting.com>
@henryiii
Copy link
Contributor

I'm confused by the attached issue; it looks like the decision was to simply revert the change with the function moved, and not add the underscore; and maybe even make it slightly easier to use. Now that 3.9b1 is out, PyBind11-based libraries cannot build, and the only fix would be to start using a now internal function in CPython. SciPy is an example of a PyBind11-using library.

@vstinner
Copy link
Member

vstinner commented May 26, 2020

PyThreadState_DeleteCurrent() is declared by the public C API, but it's now excluded from the limited C API. Does pybind11 uses the limited C API? What is your issue? Please comment https://bugs.python.org/issue37878 instead of this merged PR (if you have a bugs.python.org account).

@henryiii
Copy link
Contributor

I'm sorry, I'll add a bugs.python.org account or see if I had one in the past if I need to comment there. I realize, from looking through the code, that an underscore was not added, the function was moved intact (which was not clear from the PR), exactly as it was supposed to be in the issue. The only remaining problem is that the release notes here are wrong: https://docs.python.org/3.9/whatsnew/3.9.html#removed (which is a much better problem).

I'd love it if PyBind11 could use the limited API, but it can't.

Thank you, @vstinner !

@vstinner
Copy link
Member

The only remaining problem is that the release notes here are wrong: https://docs.python.org/3.9/whatsnew/3.9.html#removed (which is a much better problem).

I created #20489 to update the What's New in Python 3.9 document. Would you mind to review my PR?

websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
)

* Rename PyThreadState_DeleteCurrent()
  to _PyThreadState_DeleteCurrent()
* Move it to the internal C API

Co-Authored-By: Carol Willing <carolcode@willingconsulting.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants