From 09e3a7d79b08204db266025b9e435c183a3c0ca8 Mon Sep 17 00:00:00 2001 From: David Venhoek Date: Wed, 17 Jan 2024 12:09:10 +0100 Subject: [PATCH] Fixed race condition during startup. (#375) Co-authored-by: Marlon Peeters --- statime-linux/src/main.rs | 19 ++++++++++++++----- statime-linux/src/metrics/exporter.rs | 6 ++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/statime-linux/src/main.rs b/statime-linux/src/main.rs index 8a0be6710..ec8ee47f8 100644 --- a/statime-linux/src/main.rs +++ b/statime-linux/src/main.rs @@ -267,6 +267,8 @@ async fn actual_main() { let mut clock_name_map = HashMap::new(); let mut clock_port_map = Vec::with_capacity(config.ports.len()); + let mut ports = Vec::with_capacity(config.ports.len()); + for port_config in config.ports { let interface = port_config.interface; let network_mode = port_config.network_mode; @@ -299,11 +301,10 @@ async fn actual_main() { let (main_task_sender, port_task_receiver) = tokio::sync::mpsc::channel(1); let (port_task_sender, main_task_receiver) = tokio::sync::mpsc::channel(1); - main_task_sender - .send(port) - .await - .expect("space in channel buffer"); - + // We can't send the port yet, since that may start running on the port, + // inhibiting write access to the instance and making it impossible to + // create more ports. + ports.push(port); main_task_senders.push(main_task_sender); main_task_receivers.push(main_task_receiver); @@ -356,6 +357,14 @@ async fn actual_main() { } } + // All ports created, so we can start running them. + for (i, port) in ports.into_iter().enumerate() { + main_task_senders[i] + .send(port) + .await + .expect("space in channel buffer"); + } + // The observer for the metrics exporter statime_linux::observer::spawn(&config.observability).await; diff --git a/statime-linux/src/metrics/exporter.rs b/statime-linux/src/metrics/exporter.rs index 44e7692c8..8f2169f95 100644 --- a/statime-linux/src/metrics/exporter.rs +++ b/statime-linux/src/metrics/exporter.rs @@ -257,10 +257,12 @@ fn format_metric( #[cfg(test)] mod tests { - use crate::metrics::exporter::Args; - use clap::Parser; use std::path::Path; + use clap::Parser; + + use crate::metrics::exporter::Args; + const BINARY: &str = "/usr/bin/statime-metrics-exporter"; #[test]