diff --git a/src/asgi/mod.rs b/src/asgi/mod.rs index 7b0e3f0..3298fe7 100644 --- a/src/asgi/mod.rs +++ b/src/asgi/mod.rs @@ -93,12 +93,12 @@ fn ensure_python_event_loop() -> Result, HandlerError> { let weak_handle = PYTHON_EVENT_LOOP.get_or_init(|| RwLock::new(Weak::new())); // Try to upgrade the weak reference - if let Some(handle) = weak_handle.read().unwrap().upgrade() { + if let Some(handle) = weak_handle.read()?.upgrade() { return Ok(handle); } // Need write lock to create new handle - let mut guard = weak_handle.write().unwrap(); + let mut guard = weak_handle.write()?; // Double-check in case another thread created it if let Some(handle) = guard.upgrade() { diff --git a/src/lib.rs b/src/lib.rs index 7e592a5..648360f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,8 @@ #![warn(clippy::dbg_macro, clippy::print_stdout)] #![warn(missing_docs)] -use std::ffi::c_char; #[cfg(feature = "napi-support")] -use std::sync::Arc; +use std::{ffi::c_char, sync::Arc}; #[cfg(feature = "napi-support")] use http_handler::napi::{Request as NapiRequest, Response as NapiResponse}; @@ -345,6 +344,16 @@ pub enum HandlerError { /// Error when PYTHON_NODE_WORKERS is invalid #[error("Invalid PYTHON_NODE_WORKERS count: {0}")] InvalidWorkerCount(#[from] std::num::ParseIntError), + + /// Error when a lock is poisoned + #[error("Lock poisoned: {0}")] + LockPoisoned(String), +} + +impl From> for HandlerError { + fn from(err: std::sync::PoisonError) -> Self { + HandlerError::LockPoisoned(err.to_string()) + } } #[cfg(feature = "napi-support")]