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

Emitting a log message during interpreter finalization will cause a panic #30

Open
ritikmishra opened this issue Aug 11, 2023 · 1 comment

Comments

@ritikmishra
Copy link

In my project, I have a Rust struct that will print a log message when it is dropped. In my Python code, this Rust struct lives in global scope and is not deallocated by the interpreter until the Python process exits.

When the Python process exits and drops my Rust struct, pyo3_log::Logger panics when trying to log the message. This is because it tries to acquire the GIL, but since the interpreter is in the process of finalization, the GIL is not available, so pyo3::Python::with_gil panics

Due to PyO3/pyo3#2102, this panic manifests as a SIGABRT being sent to the thread.

--

I think one potential solution is to check that the interpreter is in an initialized state before trying to acquire the GIL using pyo3::ffi::Py_IsInitialized -- however the function is unsafe and I'm not familiar enough with Python internals to know when it is safe to call.

@vorner
Copy link
Owner

vorner commented Aug 14, 2023

Hello

That Py_IsInitialized doesn't seem like a proper solution. The state could change between checking it and then calling the with_gil. I think we want to have a solution, but this one doesn't seem to be the correct one.

But I wonder what the expected behavior would even be, in such case? Just drop the message? For that we would probably need some kind of with_gil_fallible that can refuse to run instead of panicking. I don't see one at a first glance, would you mind asking the pyo3 folks about it or if there's other, preferred, method?

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

No branches or pull requests

2 participants