Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

test(serve): make the released-serve-lock test robust under parallel load (#745)

AkitaOnRails5a86c57e61a6
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rs
index 1ca78327..25aa3746 100644
--- a/crates/ai-memory-cli/src/commands/serve.rs
+++ b/crates/ai-memory-cli/src/commands/serve.rs
@@ -2492,6 +2492,30 @@ mod tests {
}
}
+ /// Acquire the serve lock after a prior holder was released, tolerating the
+ /// brief window in which a just-released `flock` can still report busy when
+ /// the release and the re-acquire race in the *same* process under heavy
+ /// parallel test load. This asserts the guarantee that actually matters — a
+ /// released lock is not *permanently* held — rather than instant
+ /// availability; a real server releases on process exit, so production never
+ /// hits this same-process window (and `acquire_serve_lock` rightly never
+ /// retries a genuine `WouldBlock`). Still requires the lock to be genuinely
+ /// acquired within the window, and panics with the concrete cause otherwise.
+ fn acquire_released_serve_lock(dir: &Path) -> ServeLock {
+ let deadline = std::time::Instant::now() + Duration::from_secs(2);
+ loop {
+ match acquire_serve_lock(dir, false) {
+ Ok(Some(lock)) => return lock,
+ other => {
+ if std::time::Instant::now() >= deadline {
+ return assert_serve_lock_held(other);
+ }
+ std::thread::sleep(Duration::from_millis(25));
+ }
+ }
+ }
+ }
+
#[test]
fn transient_errors_are_retriable_but_a_busy_lock_is_not() {
use std::io::{Error, ErrorKind};
@@ -2546,7 +2570,11 @@ mod tests {
// Dropping the holder is what process exit does to the flock: the
// leftover .serve.lock file must not outlive the lock it named.
}
- assert_serve_lock_held(acquire_serve_lock(dir.path(), false));
+ // Under heavy parallel `cargo test --workspace` load the just-released
+ // flock can momentarily still report busy in this same process; retry
+ // briefly so the assertion checks "not permanently locked out" rather
+ // than instant availability.
+ let _ = acquire_released_serve_lock(dir.path());
}
#[test]