You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello there, I have an issue on program exit where the Drop trait of a PyClass object doesn't get called when python is exitting. I first encountered it in an extension module I am working on, but it seems to happen also when it is rust initializing python.
Here is minimal example
use pyo3::prelude::*;fnmain(){println!("starting");let a = Python::with_gil(|py| {Py::new(py,A::new(1))});let b = A::new(2);drop(b);drop(a);// commenting this changes nothingprintln!("ending");// Python::with_gil(|_| {}); // uncomment it to have a being freed}#[pyclass]structA{a:u32,}implA{fnnew(a:u32) -> Self{println!("creating");Self{a}}}implDropforA{fndrop(&mutself){println!("dropping {}", self.a);self.a = 0;}}
Using pyo3==0.21.2 or lower, and python==3.11.2 I get the following output
starting
creating 1
creating 2
dropping 2
ending
I would have expected to see drop 1 somewhere, likely after ending and main exit, when the started python interpreter stops and reacquire its GIL to drop all its objects. Instead it seems the object is never dropped.
If I reacquire the GIL before leaving main (uncommenting the last line) I finally get what's expected.
In this example, reacquire the GIL at the end of the main seems to be a workaround, but it is not in the case of an extension module where you do not know when the program will stop and thus cannot insert this GIL acquisition.
In my usecase, I need to properly stop rust threads in drop
Do you have an idea of what to do in order to ensure drop will be called ?
The text was updated successfully, but these errors were encountered:
Hello there, I have an issue on program exit where the
Drop
trait of aPyClass
object doesn't get called when python is exitting. I first encountered it in an extension module I am working on, but it seems to happen also when it is rust initializing python.Here is minimal example
Using
pyo3==0.21.2
or lower, andpython==3.11.2
I get the following outputI would have expected to see
drop 1
somewhere, likely afterending
andmain
exit, when the started python interpreter stops and reacquire its GIL to drop all its objects. Instead it seems the object is never dropped.If I reacquire the GIL before leaving
main
(uncommenting the last line) I finally get what's expected.In this example, reacquire the GIL at the end of the main seems to be a workaround, but it is not in the case of an extension module where you do not know when the program will stop and thus cannot insert this GIL acquisition.
In my usecase, I need to properly stop rust threads in
drop
Do you have an idea of what to do in order to ensure
drop
will be called ?The text was updated successfully, but these errors were encountered: