From c4b4746e501517bfc7b72026c898f06753c5b5b5 Mon Sep 17 00:00:00 2001
From: Ayush Singh <ayush@beagleboard.org>
Date: Fri, 17 Jan 2025 17:13:08 +0530
Subject: [PATCH] gui: Make destination subscription efficient

Only poll for destination when on the selection page, and throttle the
polling with a delay of 1 sec.

The previous implementation can crash the running udisk2 server.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
 Cargo.lock                     | 12 ++++++++++++
 bb-imager-gui/Cargo.toml       |  1 +
 bb-imager-gui/src/main.rs      |  9 ++++++---
 bb-imager-gui/src/pages/mod.rs |  2 +-
 4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 7834772..c33bcb7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -543,6 +543,7 @@ dependencies = [
  "notify-rust",
  "rfd",
  "tokio",
+ "tokio-stream",
  "tracing",
  "tracing-subscriber",
  "url",
@@ -5528,6 +5529,17 @@ dependencies = [
  "tokio",
 ]
 
+[[package]]
+name = "tokio-stream"
+version = "0.1.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047"
+dependencies = [
+ "futures-core",
+ "pin-project-lite",
+ "tokio",
+]
+
 [[package]]
 name = "tokio-util"
 version = "0.7.11"
diff --git a/bb-imager-gui/Cargo.toml b/bb-imager-gui/Cargo.toml
index 4eaa34d..26f19dd 100644
--- a/bb-imager-gui/Cargo.toml
+++ b/bb-imager-gui/Cargo.toml
@@ -23,6 +23,7 @@ chrono = { version = "0.4.38" }
 futures-util = "0.3.31"
 whoami = "1.5.2"
 localzone = "0.3.1"
+tokio-stream = "0.1.17"
 
 [build-dependencies]
 embed-resource = "2.5.0"
diff --git a/bb-imager-gui/src/main.rs b/bb-imager-gui/src/main.rs
index bb45324..80fe787 100644
--- a/bb-imager-gui/src/main.rs
+++ b/bb-imager-gui/src/main.rs
@@ -1,10 +1,11 @@
 #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
 
-use std::{borrow::Cow, collections::HashSet};
+use std::{borrow::Cow, collections::HashSet, time::Duration};
 
 use helpers::{ProgressBarState, Tainted};
 use iced::{futures::SinkExt, widget, Element, Subscription, Task};
 use pages::{configuration::FlashingCustomization, Screen};
+use tokio_stream::StreamExt;
 use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
 
 mod constants;
@@ -568,14 +569,16 @@ impl BBImager {
     fn subscription(&self) -> Subscription<BBImagerMessage> {
         if let Some(board) = self.selected_board.as_ref() {
             // Do not use subscription for static destinations
-            if self.destination_selectable {
+            // Also do not use subscription when on other screens. Can cause Udisk2 to crash.
+            if self.destination_selectable && self.screen == Screen::DestinationSelection {
                 let flasher = self.boards.device(board).flasher;
 
                 let stream = futures_util::stream::unfold(flasher, |f| async move {
                     let dsts = f.destinations().await;
                     let dsts = BBImagerMessage::Destinations(dsts);
                     Some((dsts, f))
-                });
+                })
+                .throttle(Duration::from_secs(1));
 
                 return Subscription::run_with_id(board.to_string(), stream);
             }
diff --git a/bb-imager-gui/src/pages/mod.rs b/bb-imager-gui/src/pages/mod.rs
index 35aacb4..cf52173 100644
--- a/bb-imager-gui/src/pages/mod.rs
+++ b/bb-imager-gui/src/pages/mod.rs
@@ -5,7 +5,7 @@ pub mod flash;
 pub mod home;
 pub mod image_selection;
 
-#[derive(Default, Debug, Clone, Copy)]
+#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
 pub enum Screen {
     #[default]
     Home,
-- 
GitLab