Skip to content

Commit

Permalink
Fixed race condition during startup. (#375)
Browse files Browse the repository at this point in the history
Co-authored-by: Marlon Peeters <[email protected]>
  • Loading branch information
davidv1992 and cikzh authored Jan 17, 2024
1 parent d5d5a74 commit 09e3a7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
19 changes: 14 additions & 5 deletions statime-linux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions statime-linux/src/metrics/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,12 @@ fn format_metric<T: std::fmt::Display>(

#[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]
Expand Down

0 comments on commit 09e3a7d

Please sign in to comment.