diff --git a/src/dashboard_defs/surprise.rs b/src/dashboard_defs/surprise.rs index de4ea76..f0d5267 100644 --- a/src/dashboard_defs/surprise.rs +++ b/src/dashboard_defs/surprise.rs @@ -191,18 +191,16 @@ pub fn make_surprise_window( const SURPRISE_STREAM_PATH_BUFFER_INITIAL_SIZE: usize = 64; - let options = ListenerOptions::new().name(artificial_triggering_socket_path.to_fs_name::()?); + let socket_path_fs_name = artificial_triggering_socket_path.to_fs_name::()?; + let make_listener = || ListenerOptions::new().name(socket_path_fs_name.clone()).create_sync(); - let surprise_stream_listener = match options.create_sync() { + let surprise_stream_listener = match make_listener() { Ok(listener) => listener, Err(err) => { - return error_msg!( - "Could not create a surprise stream listener. \ - Perhaps the socket at '{artificial_triggering_socket_path}' is already in use, or \ - maybe it was still around from a crash? \ - Official error: '{err}'." - ); + log::warn!("A previous surprise stream socket path was still around after a previous crash; removing it and making a new one."); + std::fs::remove_file(artificial_triggering_socket_path)?; + make_listener().unwrap_or_else(|_| panic!("Could not create a surprise stream listener: '{err}'.")) } }; diff --git a/src/dashboard_defs/twilio.rs b/src/dashboard_defs/twilio.rs index 7dea14d..6a95fe2 100644 --- a/src/dashboard_defs/twilio.rs +++ b/src/dashboard_defs/twilio.rs @@ -9,7 +9,7 @@ use crate::{ generic_result::*, update_rate::UpdateRate, dynamic_optional::DynamicOptional, - thread_task::{ContinuallyUpdated, Updatable} + continually_updated::{ContinuallyUpdated, Updatable} }, dashboard_defs::shared_window_state::SharedWindowState, diff --git a/src/dashboard_defs/weather.rs b/src/dashboard_defs/weather.rs index b7c9e1c..8517aa9 100644 --- a/src/dashboard_defs/weather.rs +++ b/src/dashboard_defs/weather.rs @@ -10,7 +10,7 @@ use crate::{ generic_result::*, dynamic_optional::DynamicOptional, update_rate::{UpdateRateCreator, Seconds}, - thread_task::{ContinuallyUpdated, Updatable} + continually_updated::{ContinuallyUpdated, Updatable} }, window_tree::{ diff --git a/src/spinitron/state.rs b/src/spinitron/state.rs index 03f71a6..0b24581 100644 --- a/src/spinitron/state.rs +++ b/src/spinitron/state.rs @@ -8,7 +8,7 @@ use crate::{ utility_types::{ generic_result::*, - thread_task::{Updatable, ContinuallyUpdated} + continually_updated::{Updatable, ContinuallyUpdated} }, spinitron::model::{ diff --git a/src/utility_types/thread_task.rs b/src/utility_types/continually_updated.rs similarity index 95% rename from src/utility_types/thread_task.rs rename to src/utility_types/continually_updated.rs index d70673e..5e303e3 100644 --- a/src/utility_types/thread_task.rs +++ b/src/utility_types/continually_updated.rs @@ -1,8 +1,9 @@ use std::thread; use std::sync::mpsc; - use crate::utility_types::generic_result::*; +const STACK_SIZE: usize = 65536; + ////////// /* TODO: @@ -11,8 +12,6 @@ in short bursts when you call `update` (or work with coroutines somehow)? - I can maybe use the `park_timeout`, `sleep`, `yield_now`, or `sleep_until` functions for that... - Allow for thread joining (or just finishing one iteration, rather;) (would be useful for Twilio) (but this is assuming that I don't do the thread-always-alive idea) -- Give the thread the name string, rather than keeping the string around -- Rename this file to `continually_updated.rs` */ pub trait Updatable: Clone + Send { @@ -27,6 +26,8 @@ pub struct ContinuallyUpdated { name: &'static str } +////////// + impl ContinuallyUpdated { pub fn new(data: &T, initial_param: &T::Param, name: &'static str) -> Self { let (data_sender, data_receiver) = mpsc::sync_channel(1); // This can be async if needed @@ -34,7 +35,7 @@ impl ContinuallyUpdated { let mut cloned_data = data.clone(); - thread::spawn(move || { + thread::Builder::new().name(name.to_string()).stack_size(STACK_SIZE).spawn(move || { loop { fn handle_channel_error(err: Error, name: &str, transfer_description: &str) { log::warn!("Problem from {name} with {transfer_description} main thread (probably harmless, at program shutdown): {err}"); @@ -61,7 +62,7 @@ impl ContinuallyUpdated { return; } } - }); + }).expect("Failed to spawn thread"); let continually_updated = Self { curr_data: data.clone(), param_sender, diff --git a/src/utility_types/generic_result.rs b/src/utility_types/generic_result.rs index d565ac3..661a94d 100644 --- a/src/utility_types/generic_result.rs +++ b/src/utility_types/generic_result.rs @@ -18,7 +18,7 @@ pub trait ToGenericError { impl ToGenericError for Result where E: std::fmt::Debug + std::fmt::Display + Send + Sync + 'static { - fn to_generic(self) -> anyhow::Result { + fn to_generic(self) -> GenericResult { self.map_err(anyhow::Error::msg) } } diff --git a/src/utility_types/mod.rs b/src/utility_types/mod.rs index ea4053e..3501355 100644 --- a/src/utility_types/mod.rs +++ b/src/utility_types/mod.rs @@ -1,6 +1,6 @@ pub mod vec2f; pub mod json_utils; pub mod update_rate; -pub mod thread_task; pub mod generic_result; pub mod dynamic_optional; +pub mod continually_updated;