From ec369f65c5ec9ef27714ddc879dde2457f537a9e Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Tue, 26 Aug 2025 01:31:33 -0700 Subject: [PATCH] use callback queue to make this thread safe and remove locks (which lag ui thread?) --- system/ui/lib/wifi_manager.py | 17 ++++++++++++----- system/ui/widgets/network.py | 3 +++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/system/ui/lib/wifi_manager.py b/system/ui/lib/wifi_manager.py index 96940c2b3b..df3a834296 100644 --- a/system/ui/lib/wifi_manager.py +++ b/system/ui/lib/wifi_manager.py @@ -133,6 +133,7 @@ class WifiManager: # State self._connecting_to_ssid: str = "" self._last_network_update: float = 0.0 + self._callback_queue: list[Callable] = [] # Callbacks # TODO: implement a callback queue to avoid blocking UI thread @@ -163,6 +164,12 @@ class WifiManager: self._networks_updated = networks_updated self._disconnected = disconnected + def process_callbacks(self): + for cb in self._callback_queue: + print('calling wifi cb', cb.__name__) + cb() + self._callback_queue.clear() + def set_active(self, active: bool): self._active = active @@ -204,19 +211,19 @@ class WifiManager: if new_state == NMDeviceState.NEED_AUTH and change_reason == NM_DEVICE_STATE_REASON_SUPPLICANT_DISCONNECT and len(self._connecting_to_ssid): self.forget_connection(self._connecting_to_ssid, block=True) if self._need_auth is not None: - self._need_auth(self._connecting_to_ssid) + self._callback_queue.append(lambda: self._need_auth(self._connecting_to_ssid)) self._connecting_to_ssid = "" elif new_state == NMDeviceState.ACTIVATED: if self._activated is not None: self._update_networks() - self._activated() + self._callback_queue.append(self._activated) self._connecting_to_ssid = "" elif new_state == NMDeviceState.DISCONNECTED and change_reason != NM_DEVICE_STATE_REASON_NEW_ACTIVATION: self._connecting_to_ssid = "" if self._disconnected is not None: - self._disconnected() + self._callback_queue.append(self._disconnected) def _network_scanner(self): self._wait_for_wifi_device() @@ -324,7 +331,7 @@ class WifiManager: if self._forgotten is not None: self._update_networks() - self._forgotten(ssid) + self._callback_queue.append(lambda: self._forgotten(ssid)) if block: worker() @@ -400,7 +407,7 @@ class WifiManager: self._networks = networks if self._networks_updated is not None: - self._networks_updated(self._networks) + self._callback_queue.append(lambda: self._networks_updated(self._networks)) def __del__(self): self.stop() diff --git a/system/ui/widgets/network.py b/system/ui/widgets/network.py index 6f5a00015b..136db8e60c 100644 --- a/system/ui/widgets/network.py +++ b/system/ui/widgets/network.py @@ -71,6 +71,8 @@ class WifiManagerUI(Widget): gui_app.texture(icon, ICON_SIZE, ICON_SIZE) def _render(self, rect: rl.Rectangle): + self.wifi_manager.process_callbacks() + with self._lock: if not self._networks: gui_label(rect, "Scanning Wi-Fi networks...", 72, alignment=rl.GuiTextAlignment.TEXT_ALIGN_CENTER) @@ -220,6 +222,7 @@ class WifiManagerUI(Widget): font_size=45) def _on_need_auth(self, ssid): + print('need auth for', ssid) with self._lock: network = next((n for n in self._networks if n.ssid == ssid), None) if network: