From b0944662fa35045918d6803cbe51ec5316871847 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sun, 13 Oct 2024 14:41:46 -0700 Subject: [PATCH] fix(wm): add cmd thread lock acquisition timeouts After some investigation by @alex-ds13 on Discord it looks like there are times where attempting to gain a lock on the WindowManager inside of read_commands_uds results in the thread becoming blocked when it's not possible to obtain the lock. Instead of waiting indefinitely for a lock, this change ensures that we will wait for at most 1 second before discarding the message so that the command listener loop can continue. Warning logs have been added to inform when a message has been dropped as a result of lock acquisition failure. --- komorebi/src/process_command.rs | 35 ++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 0cfa013e..50593c66 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1619,22 +1619,29 @@ pub fn read_commands_uds(wm: &Arc>, mut stream: UnixStream) for line in reader.lines() { let message = SocketMessage::from_str(&line?)?; - let mut wm = wm.lock(); - - if wm.is_paused { - return match message { - SocketMessage::TogglePause - | SocketMessage::State - | SocketMessage::GlobalState - | SocketMessage::Stop => Ok(wm.process_command(message, &mut stream)?), - _ => { - tracing::trace!("ignoring while paused"); - Ok(()) + match wm.try_lock_for(Duration::from_secs(1)) { + None => { + tracing::warn!( + "could not acquire window manager lock, not processing message: {message}" + ); + } + Some(mut wm) => { + if wm.is_paused { + return match message { + SocketMessage::TogglePause + | SocketMessage::State + | SocketMessage::GlobalState + | SocketMessage::Stop => Ok(wm.process_command(message, &mut stream)?), + _ => { + tracing::trace!("ignoring while paused"); + Ok(()) + } + }; } - }; - } - wm.process_command(message.clone(), &mut stream)?; + wm.process_command(message.clone(), &mut stream)?; + } + } } Ok(())