Repository: tarkah/tickrs (language: rust)

Issue:
stocks don't show on launch if config file update interval is long
I set my update interval to 600 seconds.  When I launched tickrs before I did this from the cli, stocks started to display right away.  With the interval now set in the config file, no stock data.  I changed the update interval to 20 and started.  I counted off 20 seconds and data began to appear.  This there should be a display on launch and then start using the update interval from when the initial display is completed.


Discussion / hints:
Same issue on my end. I use a shorter update interval though (60s) so it's not a big deal for me.
I was trying to look into this and I'm far from an expert in rust or this program. But I'm confused because the code clearly claims to query the data and ask for a draw before the interval is ever evaluated. Which... you can actually see if you show the volumes from the start, since these show up immediately.

All of that to say that I'll keep looking because this annoys me as well but I have no idea what I'm doing 😅 

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6a4a1bb..a9c0ab6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,10 @@ and `Removed`.
 
 ## [Unreleased]
 
+### Fixed
+
+- Race condition preventing data from loading if a long update interval was defined
+
 ## [0.14.9] - 2023-07-25
 
 ### Fixed
diff --git a/src/main.rs b/src/main.rs
index 030ee2c..3bb4230 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -43,7 +43,7 @@ lazy_static! {
     pub static ref SHOW_VOLUMES: RwLock<bool> = RwLock::new(OPTS.show_volumes);
     pub static ref DEFAULT_TIMESTAMPS: RwLock<HashMap<TimeFrame, Vec<i64>>> = Default::default();
     pub static ref THEME: theme::Theme = OPTS.theme.unwrap_or_default();
-    pub static ref YAHOO_CRUMB: RwLock<Option<CrumbData>> = RwLock::default();
+    pub static ref YAHOO_CRUMB: async_std::sync::RwLock<Option<CrumbData>> = Default::default();
 }
 
 fn main() {
@@ -56,6 +56,7 @@ fn main() {
 
     setup_panic_hook();
     setup_terminal();
+    set_crumb();
 
     let request_redraw = REDRAW_REQUEST.0.clone();
     let data_received = DATA_RECEIVED.1.clone();
@@ -80,8 +81,6 @@ fn main() {
 
     let default_timestamp_service = DefaultTimestampService::new();
 
-    set_crumb();
-
     let app = Arc::new(Mutex::new(app::App {
         mode: starting_mode,
         stocks: starting_stocks,
@@ -236,8 +235,10 @@ fn setup_panic_hook() {
 
 fn set_crumb() {
     async_std::task::spawn(async move {
+        let mut _guard = YAHOO_CRUMB.write().await;
+
         if let Ok(crumb) = CLIENT.get_crumb().await {
-            *YAHOO_CRUMB.write() = Some(crumb);
+            *_guard = Some(crumb);
         }
     });
 }
diff --git a/src/task/company.rs b/src/task/company.rs
index b4af146..8ce64d0 100644
--- a/src/task/company.rs
+++ b/src/task/company.rs
@@ -32,7 +32,7 @@ impl AsyncTask for Company {
         Box::pin(async move {
             let symbol = input.as_ref();
 
-            let crumb = YAHOO_CRUMB.read().clone();
+            let crumb = YAHOO_CRUMB.read().await.clone();
 
             if let Some(crumb) = crumb {
                 crate::CLIENT.get_company_data(symbol, crumb).await.ok()
diff --git a/src/task/current_price.rs b/src/task/current_price.rs
index 747ef2a..b89612a 100644
--- a/src/task/current_price.rs
+++ b/src/task/current_price.rs
@@ -31,7 +31,7 @@ impl AsyncTask for CurrentPrice {
         Box::pin(async move {
             let symbol = input.as_ref();
 
-            let crumb = YAHOO_CRUMB.read().clone();
+            let crumb = YAHOO_CRUMB.read().await.clone();
 
             if let Some(crumb) = crumb {
                 if let Ok(response) = crate::CLIENT.get_company_data(symbol, crumb).await {


Test patch: