Skip to content

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.

@wjakob
Copy link
Owner

wjakob commented Apr 8, 2025

Hi @smurfix,

sorry for the long delay in getting to this. The information in this message is not quite accurate anymore: the crashing behavior was changed in recent Python versions. The issue is also not in threads being alive in general -- it's threads that work with nanobind objects, and specifically in such a way that the GIL is dropped and reacquired (which is the step that crashes on old python versions, and pauses those threads on newer versions). Would you consider updating the text to make this more clear, or shall I close the PR?

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