Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+policy-temp.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A generated stack policy is a new file for each solve. The MCP server no longer reuses `eb-stack-policy-<toolchain>-<pid>.json`, and it removes the file when the solve returns.
14 changes: 4 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 16 additions & 4 deletions src/mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2874,6 +2874,7 @@ fn stack_solve(arguments: &Value) -> Result<Value, String> {
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 {
Expand All @@ -2884,10 +2885,10 @@ fn stack_solve(arguments: &Value) -> Result<Value, String> {
.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,
Expand All @@ -2900,6 +2901,17 @@ fn stack_solve(arguments: &Value) -> Result<Value, String> {
.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<PathBuf>);
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)
Expand Down
49 changes: 49 additions & 0 deletions src/workspace_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,13 +990,62 @@ 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<PathBuf> {
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::*;
use std::sync::Mutex;

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<T>(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");
Expand Down
Loading