Skip to content
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

Added a comment about GIL acquisition vs. multithreading vs. program exit. #891

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

smurfix
Copy link
Contributor

@smurfix smurfix commented Jan 29, 2025

I added a paragraph warning about this problem. Maybe it saves somebody else a few hours of work.

Cf. #890 where one of my problems was exactly this …

@wjakob
Copy link
Owner

wjakob commented Jan 30, 2025

What does "nor ignoring a cancellation" mean?

I will note that nb::is_alive() can be used to skip deallocation/destructor code in some cases. It's not so nice because that causes leaks, but still infinitely preferable to segfaults.

@smurfix
Copy link
Contributor Author

smurfix commented Jan 30, 2025

When you try to acquire the GIL and Python is in shutdown, your thread self-cancels. You can't catch this exception without re-throwing it, the compiler won't let you.

I can't fix the problem by (only) calling is_alive in my own destructors because that's not sufficient if the object contains one of nanobind's. To that effect I created PR #894 which adds aliveness checks to all of nanobind's destructors if they directly acquire the GIL. I didn't yet check whether that's sufficient.

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.

2 participants