Skip to content

Commit 94092af

Browse files
authored
Eliminate RwLock::unwrap() call (#15)
1 parent 986206a commit 94092af

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/asgi/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ fn ensure_python_event_loop() -> Result<Arc<EventLoopHandle>, HandlerError> {
9393
let weak_handle = PYTHON_EVENT_LOOP.get_or_init(|| RwLock::new(Weak::new()));
9494

9595
// Try to upgrade the weak reference
96-
if let Some(handle) = weak_handle.read().unwrap().upgrade() {
96+
if let Some(handle) = weak_handle.read()?.upgrade() {
9797
return Ok(handle);
9898
}
9999

100100
// Need write lock to create new handle
101-
let mut guard = weak_handle.write().unwrap();
101+
let mut guard = weak_handle.write()?;
102102

103103
// Double-check in case another thread created it
104104
if let Some(handle) = guard.upgrade() {

src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
#![warn(clippy::dbg_macro, clippy::print_stdout)]
99
#![warn(missing_docs)]
1010

11-
use std::ffi::c_char;
1211
#[cfg(feature = "napi-support")]
13-
use std::sync::Arc;
12+
use std::{ffi::c_char, sync::Arc};
1413

1514
#[cfg(feature = "napi-support")]
1615
use http_handler::napi::{Request as NapiRequest, Response as NapiResponse};
@@ -345,6 +344,16 @@ pub enum HandlerError {
345344
/// Error when PYTHON_NODE_WORKERS is invalid
346345
#[error("Invalid PYTHON_NODE_WORKERS count: {0}")]
347346
InvalidWorkerCount(#[from] std::num::ParseIntError),
347+
348+
/// Error when a lock is poisoned
349+
#[error("Lock poisoned: {0}")]
350+
LockPoisoned(String),
351+
}
352+
353+
impl<T> From<std::sync::PoisonError<T>> for HandlerError {
354+
fn from(err: std::sync::PoisonError<T>) -> Self {
355+
HandlerError::LockPoisoned(err.to_string())
356+
}
348357
}
349358

350359
#[cfg(feature = "napi-support")]

0 commit comments

Comments
 (0)