diff --git a/changelog.d/+policy-temp.bugfix.md b/changelog.d/+policy-temp.bugfix.md new file mode 100644 index 00000000..aa2f1560 --- /dev/null +++ b/changelog.d/+policy-temp.bugfix.md @@ -0,0 +1 @@ +A generated stack policy is a new file for each solve. The MCP server no longer reuses `eb-stack-policy--.json`, and it removes the file when the solve returns. diff --git a/src/main.rs b/src/main.rs index 8223671b..755414da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3318,16 +3318,10 @@ fn run_stack(command: StackCommand) -> Result<()> { // One file per invocation. Two solves for the same // toolchain must not read each other's roots, including a // second solve in this process while the first still reads. - let stamp = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|duration| duration.as_nanos()) - .unwrap_or(0); - written_policy = std::env::temp_dir().join(format!( - "eb-stack-policy-{}-{}-{}-{stamp}.json", - name, - version, - std::process::id() - )); + written_policy = eb_stack::workspace_fs::exclusive_temp_path(&format!( + "eb-stack-policy-{name}-{version}" + )) + .context("create generated stack policy")?; std::fs::write(&written_policy, serde_json::to_vec_pretty(&policy)?)?; written_policy } diff --git a/src/mcp.rs b/src/mcp.rs index c0cfeb1d..58c65e8e 100644 --- a/src/mcp.rs +++ b/src/mcp.rs @@ -2874,6 +2874,7 @@ fn stack_solve(arguments: &Value) -> Result { return Err("toolchain needs at least one root or roots_from_build_list".into()); } let generated; + let created = policy_arg.is_none(); let policy = if let Some(path) = policy_arg { path } else { @@ -2884,10 +2885,10 @@ fn stack_solve(arguments: &Value) -> Result { .map_err(|error| error.to_string())?; let name = parsed.name.as_str(); let version = parsed.version.as_str(); - generated = std::env::temp_dir().join(format!( - "eb-stack-policy-{name}-{version}-{}.json", - std::process::id() - )); + generated = crate::workspace_fs::exclusive_temp_path(&format!( + "eb-stack-policy-{name}-{version}" + )) + .map_err(|error| error.to_string())?; let body = json!({ "toolchain": {"name": name, "version": version}, "roots": roots, @@ -2900,6 +2901,17 @@ fn stack_solve(arguments: &Value) -> Result { .map_err(|error| error.to_string())?; generated }; + // The CLI removes a policy it wrote. Leave this one until the solve + // returns, including the error paths above the solve. + struct GeneratedPolicy(Option); + impl Drop for GeneratedPolicy { + fn drop(&mut self) { + if let Some(path) = self.0.take() { + let _ = std::fs::remove_file(path); + } + } + } + let _remove_generated = GeneratedPolicy(created.then(|| policy.clone())); let stacks = string_array(arguments, "baseline_easystack")? .into_iter() .map(PathBuf::from) diff --git a/src/workspace_fs.rs b/src/workspace_fs.rs index b9b3f61d..9602e0f6 100644 --- a/src/workspace_fs.rs +++ b/src/workspace_fs.rs @@ -990,6 +990,37 @@ fn lexical_normalize(path: &Path) -> PathBuf { out } +/// A new empty file under the temporary directory. +/// +/// The name is not a function of the process id alone, so two calls in one +/// process do not share a file. `create_new` refuses a path that already +/// exists, including a symlink planted in a shared temporary directory. +pub fn exclusive_temp_path(prefix: &str) -> std::io::Result { + use std::sync::atomic::{AtomicU64, Ordering}; + static SEQUENCE: AtomicU64 = AtomicU64::new(0); + let base = std::env::temp_dir(); + for _ in 0..32 { + let sequence = SEQUENCE.fetch_add(1, Ordering::Relaxed); + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|elapsed| elapsed.as_nanos()) + .unwrap_or(0); + let path = base.join(format!( + "{prefix}-{}-{nanos}-{sequence}", + std::process::id() + )); + match OpenOptions::new().write(true).create_new(true).open(&path) { + Ok(_) => return Ok(path), + Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(err) => return Err(err), + } + } + Err(std::io::Error::new( + std::io::ErrorKind::AlreadyExists, + format!("could not create a temporary file under {}", base.display()), + )) +} + #[cfg(test)] mod tests { use super::*; @@ -997,6 +1028,24 @@ mod tests { static ENV_LOCK: Mutex<()> = Mutex::new(()); + #[test] + fn exclusive_temp_paths_are_distinct_files() { + let first = exclusive_temp_path("eb-stack-policy-foss-2026.1").expect("first"); + let second = exclusive_temp_path("eb-stack-policy-foss-2026.1").expect("second"); + assert_ne!(first, second); + std::fs::write(&first, b"roots-a").expect("write first"); + std::fs::write(&second, b"roots-b").expect("write second"); + assert_eq!(std::fs::read(&first).expect("read first"), b"roots-a"); + let predictable = std::env::temp_dir().join(format!( + "eb-stack-policy-foss-2026.1-{}.json", + std::process::id() + )); + assert_ne!(first, predictable); + assert_ne!(second, predictable); + let _ = std::fs::remove_file(&first); + let _ = std::fs::remove_file(&second); + } + fn with_env_root(root: &Path, body: impl FnOnce() -> T) -> T { let _guard = ENV_LOCK.lock().expect("workspace env lock"); let previous = std::env::var_os("EB_STACK_WORKSPACE");