Skip to content

Commit

Permalink
Fix bug in RustNotify causing RuntimeError: Already borrowed on exit/…
Browse files Browse the repository at this point in the history
…signal (#221)

Done as suggested in PyO3/pyo3#2525 (comment)
Fixes #200
  • Loading branch information
vrslev authored Mar 25, 2023
1 parent afb8f34 commit 772a440
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ impl RustNotify {
}

pub fn watch(
&self,
slf: &PyCell<Self>,
py: Python,
debounce_ms: u64,
step_ms: u64,
timeout_ms: u64,
stop_event: PyObject,
) -> PyResult<PyObject> {
if matches!(self.watcher, WatcherEnum::None) {
if matches!(slf.borrow().watcher, WatcherEnum::None) {
return Err(PyRuntimeError::new_err("RustNotify watcher closed"));
}
let stop_event_is_set: Option<&PyAny> = match stop_event.is_none(py) {
Expand All @@ -257,27 +257,27 @@ impl RustNotify {
match py.check_signals() {
Ok(_) => (),
Err(_) => {
self.clear();
slf.borrow().clear();
return Ok("signal".to_object(py));
}
};

if let Some(error) = self.error.lock().unwrap().as_ref() {
self.clear();
if let Some(error) = slf.borrow().error.lock().unwrap().as_ref() {
slf.borrow().clear();
return wf_error!(error.clone());
}

if let Some(is_set) = stop_event_is_set {
if is_set.call0()?.is_true()? {
if self.debug {
if slf.borrow().debug {
eprintln!("stop event set, stopping...");
}
self.clear();
slf.borrow().clear();
return Ok("stop".to_object(py));
}
}

let size = self.changes.lock().unwrap().len();
let size = slf.borrow().changes.lock().unwrap().len();
if size > 0 {
if size == last_size {
break;
Expand All @@ -294,13 +294,13 @@ impl RustNotify {
}
} else if let Some(max_time) = max_timeout_time {
if SystemTime::now() > max_time {
self.clear();
slf.borrow().clear();
return Ok("timeout".to_object(py));
}
}
}
let py_changes = self.changes.lock().unwrap().to_object(py);
self.clear();
let py_changes = slf.borrow().changes.lock().unwrap().to_object(py);
slf.borrow().clear();
Ok(py_changes)
}

Expand Down

0 comments on commit 772a440

Please sign in to comment.