From 9a39ea00d6f3ec08e59dc0a23799277fdab8fd1d Mon Sep 17 00:00:00 2001 From: "R. Tyler Croy" Date: Sat, 28 Nov 2020 14:46:43 -0800 Subject: [PATCH] Relocate the agent control socket to outside the current working directory There was a race condition where sometimes the agent would set up the control port so fast that a git into `.` would fail because the directory wasn't empty. The better solution is to put the control socket outside the workspace in a temp directory --- cli/agent/src/main.rs | 7 +++++-- crates/agent/src/control.rs | 16 ++++++++++------ crates/agent/src/lib.rs | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cli/agent/src/main.rs b/cli/agent/src/main.rs index f2ec8c1..816cf3c 100644 --- a/cli/agent/src/main.rs +++ b/cli/agent/src/main.rs @@ -78,9 +78,12 @@ async fn main() -> std::io::Result<()> { panic!("Failed to parse parameters file: {:#?}", e); } Ok(invoke) => { - async_std::task::spawn(async { + let pipeline_uuid = invoke.pipeline.clone(); + async_std::task::spawn(async move { // TODO better error handling and behavior - control::run(sender).await.expect("Failed to bind control?"); + control::run(pipeline_uuid, sender) + .await + .expect("Failed to bind control?"); }); /* diff --git a/crates/agent/src/control.rs b/crates/agent/src/control.rs index 28583d8..c8cc2ed 100644 --- a/crates/agent/src/control.rs +++ b/crates/agent/src/control.rs @@ -5,6 +5,7 @@ use async_std::sync::Sender; use log::*; use serde::{Deserialize, Serialize}; +use uuid::Uuid; #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(tag = "type")] @@ -27,9 +28,9 @@ async fn handle_request(mut req: tide::Request) -> tide::Result { Ok("{}".into()) } -pub async fn run(sender: Sender) -> tide::Result<()> { +pub async fn run(pipeline: Uuid, sender: Sender) -> tide::Result<()> { info!("Starting the agent control server"); - let sock = agent_socket(); + let sock = agent_socket(&pipeline); let state = State { sender }; let mut app = tide::with_state(state); @@ -53,9 +54,11 @@ pub async fn run(sender: Sender) -> tide::Result<()> { /** * Return a string representing the absolute path of this agent's control socket */ -pub fn agent_socket() -> std::path::PathBuf { - let path = std::env::current_dir().expect("Failed to get current directory"); - path.join("agent.sock").to_path_buf() +pub fn agent_socket(pipeline: &Uuid) -> std::path::PathBuf { + let uuid = pipeline.to_hyphenated().to_string(); + std::env::temp_dir() + .join(format!("{}-agent.sock", uuid)) + .to_path_buf() } #[cfg(test)] @@ -64,7 +67,8 @@ mod tests { #[test] fn test_agent_sock() { - let buf = agent_socket(); + let uuid = uuid::Uuid::new_v4(); + let buf = agent_socket(&uuid); assert!(buf.to_string_lossy().ends_with("agent.sock")); } } diff --git a/crates/agent/src/lib.rs b/crates/agent/src/lib.rs index a7689db..a05c0b2 100644 --- a/crates/agent/src/lib.rs +++ b/crates/agent/src/lib.rs @@ -188,7 +188,7 @@ pub fn run( }; // TODO: This is going to be wrong on nested steps - let sock = control::agent_socket(); + let sock = control::agent_socket(&pipeline); let configuration = step::Configuration { pipeline: pipeline, uuid: step.uuid,