watchfiles icon indicating copy to clipboard operation
watchfiles copied to clipboard

Resource warning when RustNotify is deleted without calling `close()`

Open samuelcolvin opened this issue 2 years ago • 0 comments

I agree with @graingert in #164 that we should raise a resource warning just to be safe if RustNotify isn't explicitly "closed" even though it mostly wouldn't actually be necessary since __del__ would clean up the thread.

However at the moment it seems pyo3 doesn't support the __del__ method, see https://github.com/PyO3/pyo3/issues/2479.

Once (if) pyo3 support __del__, here is the code to add so I don't need to remember it:

    pub fn __del__(&mut self, py: Python) -> PyResult<()> {
        if matches!(self.watcher, WatcherEnum::None) {
            Ok(())
        } else {
            self.close();
            let resource_warning = py.import("warnings")?.getattr("ResourceWarning")?;
            PyErr::warn(py, resource_warning, "RustNotify was not closed correctly", 1)
        }
    }

samuelcolvin avatar Jun 25 '22 11:06 samuelcolvin