From 551e1ce691a2d32cafe3fc72ab6e9d0a2e9bbd43 Mon Sep 17 00:00:00 2001 From: quietvoid Date: Sun, 16 Feb 2025 18:01:21 -0500 Subject: [PATCH] fix(keyboard): panic when layout update channel lags behind (#869) Fixes #866 --- src/clients/compositor/hyprland.rs | 2 +- src/clients/compositor/sway.rs | 2 +- src/modules/keyboard.rs | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/clients/compositor/hyprland.rs b/src/clients/compositor/hyprland.rs index d843ced..2d8990e 100644 --- a/src/clients/compositor/hyprland.rs +++ b/src/clients/compositor/hyprland.rs @@ -25,7 +25,7 @@ pub struct Client { impl Client { pub(crate) fn new() -> Self { let (workspace_tx, workspace_rx) = channel(16); - let (keyboard_layout_tx, keyboard_layout_rx) = channel(4); + let (keyboard_layout_tx, keyboard_layout_rx) = channel(16); let instance = Self { workspace_tx, diff --git a/src/clients/compositor/sway.rs b/src/clients/compositor/sway.rs index f16aaf2..411eadd 100644 --- a/src/clients/compositor/sway.rs +++ b/src/clients/compositor/sway.rs @@ -166,7 +166,7 @@ impl KeyboardLayoutClient for Client { } fn subscribe(&self) -> Receiver { - let (tx, rx) = channel(4); + let (tx, rx) = channel(16); let client = self.connection().clone(); diff --git a/src/modules/keyboard.rs b/src/modules/keyboard.rs index 19efa4e..8900e67 100644 --- a/src/modules/keyboard.rs +++ b/src/modules/keyboard.rs @@ -210,9 +210,20 @@ impl Module for KeyboardModule { trace!("Set up keyboard_layout subscription"); - while let Ok(payload) = srx.recv().await { - debug!("Received update: {payload:?}"); - module_update!(tx, KeyboardUpdate::Layout(payload)); + loop { + match srx.recv().await { + Ok(payload) => { + debug!("Received update: {payload:?}"); + module_update!(tx, KeyboardUpdate::Layout(payload)); + } + Err(tokio::sync::broadcast::error::RecvError::Lagged(count)) => { + tracing::warn!("Channel lagged behind by {count}, this may result in unexpected or broken behaviour"); + } + Err(err) => { + tracing::error!("{err:?}"); + break; + } + }; } }); }