Skip to content

Commit

Permalink
fix the memory leak in fixedpool
Browse files Browse the repository at this point in the history
fixes #9255
  • Loading branch information
reaperhulk committed Jul 20, 2023
1 parent b2abc35 commit ec4db0e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/rust/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<pyo3::PyObject>,

value: Cell<Option<pyo3::PyObject>>,
}
Expand All @@ -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)),
})
Expand All @@ -44,14 +44,31 @@ 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,
fresh: true,
})
}
}

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]
Expand Down

0 comments on commit ec4db0e

Please sign in to comment.