Skip to content

Commit

Permalink
Remove destroy from FixedPool (#7602)
Browse files Browse the repository at this point in the history
turns out we don't need it
  • Loading branch information
alex authored Sep 11, 2022
1 parent d480268 commit 30114c6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/cryptography/hazmat/bindings/_rust/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class FixedPool(typing.Generic[T]):
def __init__(
self,
create: typing.Callable[[], T],
destroy: typing.Callable[[T], None],
) -> None: ...
def acquire(self) -> PoolAcquisition[T]: ...

Expand Down
24 changes: 2 additions & 22 deletions src/rust/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::cell::Cell;
#[pyo3::prelude::pyclass]
pub(crate) struct FixedPool {
create_fn: pyo3::PyObject,
destroy_fn: pyo3::PyObject,

value: Cell<Option<pyo3::PyObject>>,
}
Expand All @@ -26,16 +25,11 @@ struct PoolAcquisition {
#[pyo3::pymethods]
impl FixedPool {
#[new]
fn new(
py: pyo3::Python<'_>,
create: pyo3::PyObject,
destroy: pyo3::PyObject,
) -> pyo3::PyResult<Self> {
fn new(py: pyo3::Python<'_>, create: pyo3::PyObject) -> pyo3::PyResult<Self> {
let value = create.call0(py)?;

Ok(FixedPool {
create_fn: create,
destroy_fn: destroy,

value: Cell::new(Some(value)),
})
Expand All @@ -60,18 +54,6 @@ impl FixedPool {
}
}

impl Drop for FixedPool {
fn drop(&mut self) {
if let Some(value) = self.value.replace(None) {
let gil = pyo3::Python::acquire_gil();
let py = gil.python();
self.destroy_fn
.call1(py, (value,))
.expect("FixedPool destroy function failed in destructor");
}
}
}

#[pyo3::pymethods]
impl PoolAcquisition {
fn __enter__(&self, py: pyo3::Python<'_>) -> pyo3::PyObject {
Expand All @@ -86,9 +68,7 @@ impl PoolAcquisition {
_exc_tb: &pyo3::PyAny,
) -> pyo3::PyResult<()> {
let pool = self.pool.as_ref(py).borrow();
if self.fresh {
pool.destroy_fn.call1(py, (self.value.clone_ref(py),))?;
} else {
if !self.fresh {
pool.value.replace(Some(self.value.clone_ref(py)));
}
Ok(())
Expand Down
16 changes: 4 additions & 12 deletions tests/test_rust_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ def create():
events.append(("create", c))
return c

def destroy(c):
events.append(("destroy", c))

pool = FixedPool(create, destroy)
pool = FixedPool(create)
assert events == [("create", 1)]
with pool.acquire() as c:
assert c == 1
Expand All @@ -32,9 +29,9 @@ def destroy(c):
assert c == 2
assert events == [("create", 1), ("create", 2)]

assert events == [("create", 1), ("create", 2), ("destroy", 2)]
assert events == [("create", 1), ("create", 2)]

assert events == [("create", 1), ("create", 2), ("destroy", 2)]
assert events == [("create", 1), ("create", 2)]

del pool
gc.collect()
Expand All @@ -44,18 +41,13 @@ def destroy(c):
assert events == [
("create", 1),
("create", 2),
("destroy", 2),
("destroy", 1),
]

def test_thread_stress(self):
def create():
return None

def destroy(c):
pass

pool = FixedPool(create, destroy)
pool = FixedPool(create)

def thread_fn():
with pool.acquire():
Expand Down

0 comments on commit 30114c6

Please sign in to comment.