From ab9c6530cd297012759b5720382df35a41310e66 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 13 Aug 2024 18:05:46 +0200 Subject: [PATCH] fix: better try_recv mutex logic for cursor ctrl --- src/cursor/controller.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/cursor/controller.rs b/src/cursor/controller.rs index 67ba81c..3b1253f 100644 --- a/src/cursor/controller.rs +++ b/src/cursor/controller.rs @@ -67,14 +67,16 @@ impl Controller for CursorController { /// try to receive without blocking, but will still block on stream mutex fn try_recv(&self) -> crate::Result> { - let mut stream = self.0.stream.blocking_lock(); - match stream.try_recv() { - Ok(x) => Ok(Some(x.into())), - Err(TryRecvError::Empty) => Ok(None), - Err(TryRecvError::Closed) => Err(crate::Error::Channel { send: false }), - Err(TryRecvError::Lagged(n)) => { - tracing::warn!("cursor channel lagged, skipping {} events", n); - Ok(stream.try_recv().map(|x| x.into()).ok()) + match self.0.stream.try_lock() { + Err(_) => Ok(None), + Ok(mut stream) => match stream.try_recv() { + Ok(x) => Ok(Some(x.into())), + Err(TryRecvError::Empty) => Ok(None), + Err(TryRecvError::Closed) => Err(crate::Error::Channel { send: false }), + Err(TryRecvError::Lagged(n)) => { + tracing::warn!("cursor channel lagged, skipping {} events", n); + Ok(stream.try_recv().map(|x| x.into()).ok()) + } } } }