Skip to content

Commit 85707f1

Browse files
committed
fix: silence error messages when sampler is dropped
1 parent 2ebf5d3 commit 85707f1

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

python/nutpie/sample.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,10 @@ def in_marimo_notebook() -> bool:
305305

306306
def _mo_write_internal(cell_id, stream, value: object) -> None:
307307
"""Write to marimo cell given cell_id and stream."""
308-
from marimo._output import formatting
308+
from marimo._messaging.cell_output import CellChannel
309309
from marimo._messaging.ops import CellOp
310310
from marimo._messaging.tracebacks import write_traceback
311-
from marimo._messaging.cell_output import CellChannel
311+
from marimo._output import formatting
312312

313313
output = formatting.try_format(value)
314314
if output.traceback is not None:
@@ -325,9 +325,9 @@ def _mo_write_internal(cell_id, stream, value: object) -> None:
325325

326326
def _mo_create_replace():
327327
"""Create mo.output.replace with current context pinned."""
328+
from marimo._output import formatting
328329
from marimo._runtime.context import get_context
329330
from marimo._runtime.context.types import ContextNotInitializedError
330-
from marimo._output import formatting
331331

332332
try:
333333
ctx = get_context()
@@ -525,9 +525,9 @@ def _extract(self, results):
525525
return results
526526
else:
527527
if results.is_zarr():
528-
from zarr.storage import ObjectStore
529528
import obstore
530529
import xarray as xr
530+
from zarr.storage import ObjectStore
531531

532532
assert self._zarr_store is not None
533533

@@ -604,7 +604,9 @@ def cancel(self):
604604
self._sampler.abort()
605605

606606
def __del__(self):
607-
if not self._sampler.is_empty():
607+
if not hasattr(self, "_sampler"):
608+
return
609+
if not self._sampler.is_empty(ignore_error=True):
608610
self.cancel()
609611

610612
def _repr_html_(self):

src/wrapper.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,11 +1232,16 @@ impl PySampler {
12321232
})
12331233
}
12341234

1235-
fn is_empty(&self) -> bool {
1236-
matches!(
1237-
self.0.lock().expect("Poisoned sampler state lock").deref(),
1238-
(SamplerState::Empty, _)
1239-
)
1235+
#[pyo3(signature = (ignore_error=false))]
1236+
fn is_empty(&self, ignore_error: bool) -> Result<bool> {
1237+
let out = self.0.lock();
1238+
match (ignore_error, out) {
1239+
(false, Err(e)) => return Err(anyhow!("The sampler panicked with error {}", e)),
1240+
(true, Err(_)) => return Ok(true),
1241+
(_, Ok(v)) => {
1242+
return Ok(matches!(v.deref(), (SamplerState::Empty, _)));
1243+
}
1244+
}
12401245
}
12411246

12421247
fn flush<'py>(&mut self, py: Python<'py>) -> PyResult<()> {

0 commit comments

Comments
 (0)