From ec4db0ee5d630e32f3c8abe10902260c628f4010 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 21 Jul 2023 08:51:10 +1200 Subject: [PATCH] fix the memory leak in fixedpool fixes #9255 --- src/rust/src/pool.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/rust/src/pool.rs b/src/rust/src/pool.rs index b9e6e27cd4af8..ac2fed728d276 100644 --- a/src/rust/src/pool.rs +++ b/src/rust/src/pool.rs @@ -9,7 +9,7 @@ use std::cell::Cell; // use. #[pyo3::prelude::pyclass(module = "cryptography.hazmat.bindings._rust")] pub(crate) struct FixedPool { - create_fn: pyo3::PyObject, + create_fn: Option, value: Cell>, } @@ -29,7 +29,7 @@ impl FixedPool { let value = create.call0(py)?; Ok(FixedPool { - create_fn: create, + create_fn: Some(create), value: Cell::new(Some(value)), }) @@ -44,7 +44,13 @@ impl FixedPool { fresh: false, }) } else { - let value = slf.as_ref(py).borrow().create_fn.call0(py)?; + let value = slf + .as_ref(py) + .borrow() + .create_fn + .as_ref() + .unwrap() + .call0(py)?; Ok(PoolAcquisition { pool: slf, value, @@ -52,6 +58,17 @@ impl FixedPool { }) } } + + fn __traverse__(&self, visit: pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> { + if let Some(create_fn) = &self.create_fn { + visit.call(create_fn)? + } + Ok(()) + } + + fn __clear__(&mut self) { + self.create_fn = None; + } } #[pyo3::pymethods]