Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(memory): close the exit race with a gate the binding cache checks under its lock

Shanuab7e7568c778
diff --git a/src/core/runtime/builder.rs b/src/core/runtime/builder.rs
index 1dd87ad8c4..ce8341e972 100644
--- a/src/core/runtime/builder.rs
+++ b/src/core/runtime/builder.rs
@@ -841,6 +841,11 @@ impl CoreRuntime {
});
}
+ // Arms memory's exit gate for the eventual exit (and clears one a
+ // previous server in this process may have left): from here on a
+ // memory binding built during exit is refused rather than missed.
+ crate::openhuman::memory::exit::server_starting();
+
// The serve result is held, not propagated, until the exit work below
// has run. A `?` here on a server error would skip the memory teardown
// on exactly the exits where a wedged store is likeliest, and the
diff --git a/src/openhuman/memory/binding.rs b/src/openhuman/memory/binding.rs
index a95cd0ecc1..3aa0f772e5 100644
--- a/src/openhuman/memory/binding.rs
+++ b/src/openhuman/memory/binding.rs
@@ -681,6 +681,16 @@ pub fn for_subtree(
let mut guard = cache
.write()
.map_err(|e| format!("[memory:binding] cache write lock poisoned: {e}"))?;
+ // Under the same lock the exit snapshot is taken under: once memory is on
+ // its way out, a driver bound now would never be asked to shut down, so
+ // it is not bound at all. The check sits inside the lock on purpose — a
+ // builder that passed it inserted before the snapshot, and one that did
+ // not is refused; there is no third case (memory/exit.rs).
+ if crate::openhuman::memory::exit::exiting() {
+ return Err(
+ "[memory:binding] memory is shutting down; not binding a new driver".to_string(),
+ );
+ }
// Re-check under the write lock: a racing caller may have bound the same
// workspace while we were building. Reuse theirs so one workspace never has
// two live drivers (kernel.md §3.1) and `capabilities()` stays asked once.
diff --git a/src/openhuman/memory/exit.rs b/src/openhuman/memory/exit.rs
index 65306c5160..b626fbb56a 100644
--- a/src/openhuman/memory/exit.rs
+++ b/src/openhuman/memory/exit.rs
@@ -13,15 +13,12 @@
//! is bounded, because a quit that hangs on a wedged store is worse than a
//! lease that expires on its own.
-use std::collections::HashSet;
-use std::sync::Arc;
+use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use futures::future::join_all;
use tokio::time::Instant;
-use crate::openhuman::memory::binding::MemoryBinding;
-
/// The whole of memory's exit work — every bound driver's `shutdown`, then
/// the host's hook registry — must fit in this, together.
///
@@ -39,38 +36,63 @@ pub const EXIT_BUDGET: Duration = Duration::from_secs(2);
/// not a driver, so the hooks are never skipped outright.
const HOOKS_FLOOR: Duration = Duration::from_millis(250);
-/// Rounds of "snapshot the cache, shut down what is new". Two is the honest
-/// number: the first round covers everything built before exit began, the
-/// second anything a background task built while the first round ran.
-const SNAPSHOT_ROUNDS: usize = 2;
+/// Whether a real server has started in this process.
+///
+/// The exit gate below engages only then. Unit tests call
+/// [`shutdown_for_exit`] without ever serving, and a process-wide refusal to
+/// bind memory would reach every other test in the same process.
+static SERVING: AtomicBool = AtomicBool::new(false);
+
+/// Set when memory's exit work begins, cleared when a server starts.
+///
+/// Read by the binding cache *inside its write lock* before every insert. That
+/// ordering is what makes the exit snapshot complete: the flag goes up, then
+/// the snapshot is taken under the same lock, so a builder either inserted
+/// before the snapshot (and is in it) or sees the flag and is refused. No
+/// number of re-snapshots could say that.
+static EXITING: AtomicBool = AtomicBool::new(false);
+
+/// Called by the embedded server as it starts serving. Arms the exit gate for
+/// the eventual exit and clears one left by a previous server in this process
+/// (the shell can respawn the core task without restarting the process).
+pub fn server_starting() {
+ EXITING.store(false, Ordering::SeqCst);
+ SERVING.store(true, Ordering::SeqCst);
+}
+
+/// Whether memory is on its way out: new bindings are refused.
+pub(crate) fn exiting() -> bool {
+ EXITING.load(Ordering::SeqCst)
+}
+
+/// Test seam: put both flags back so one test's exit cannot leak into another.
+#[cfg(test)]
+pub(crate) fn reset_gate_for_tests() {
+ EXITING.store(false, Ordering::SeqCst);
+ SERVING.store(false, Ordering::SeqCst);
+}
/// Ask every bound memory driver to shut down, then run the hooks the
/// in-process engine registered with the host.
///
/// Providers first, concurrently and on the shared deadline: a module drains
/// its own banked hook inside `Shutdown`, and the host's registry is where the
-/// in-process engine registers instead. The binding cache is snapshotted, not
-/// locked: a build that overlaps exit inserts after the snapshot, so a second
-/// round picks up whatever appeared. Both halves are idempotent — a provider's
-/// second `shutdown` is a no-op and the registry drains — so a signal landing
+/// in-process engine registers instead. The snapshot of the binding cache is
+/// complete rather than merely recent: the exit gate goes up first, and the
+/// cache checks it under its write lock before every insert, so nothing can
+/// appear after the snapshot. Both halves are idempotent — a provider's second
+/// `shutdown` is a no-op and the registry drains — so a signal landing
/// mid-teardown, or the app-update restart path calling this twice, repeats
/// nothing.
pub async fn shutdown_for_exit() {
+ if SERVING.load(Ordering::SeqCst) {
+ EXITING.store(true, Ordering::SeqCst);
+ }
let deadline = Instant::now() + EXIT_BUDGET;
- // Addresses, not pointers: a raw pointer in the set would make this future
- // `!Send`, and the embedded server task that awaits it is spawned. The
- // `Arc` snapshot keeps every binding alive across the loop, so an address
- // cannot be reused for a different binding while it is in the set.
- let mut seen: HashSet<usize> = HashSet::new();
- for round in 1..=SNAPSHOT_ROUNDS {
- let pending: Vec<Arc<MemoryBinding>> = crate::openhuman::memory::binding::cached_bindings()
- .into_iter()
- .filter(|binding| seen.insert(Arc::as_ptr(binding) as usize))
- .collect();
- if pending.is_empty() {
- break;
- }
- let shutdowns = join_all(pending.iter().map(|binding| async move {
+
+ let bindings = crate::openhuman::memory::binding::cached_bindings();
+ if !bindings.is_empty() {
+ let shutdowns = join_all(bindings.iter().map(|binding| async move {
let driver = binding.driver_id().to_string();
match binding.provider().shutdown().await {
Ok(()) => log::debug!("[memory:exit] driver '{driver}' shut down"),
@@ -81,10 +103,9 @@ pub async fn shutdown_for_exit() {
}));
if tokio::time::timeout_at(deadline, shutdowns).await.is_err() {
log::warn!(
- "[memory:exit] driver shutdown exceeded the {EXIT_BUDGET:?} exit budget in \
- round {round}; proceeding with exit"
+ "[memory:exit] driver shutdown exceeded the {EXIT_BUDGET:?} exit budget; \
+ proceeding with exit"
);
- break;
}
}
diff --git a/src/openhuman/memory/exit_tests.rs b/src/openhuman/memory/exit_tests.rs
index c552587601..96dec35d50 100644
--- a/src/openhuman/memory/exit_tests.rs
+++ b/src/openhuman/memory/exit_tests.rs
@@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
-use super::exit::{shutdown_for_exit, EXIT_BUDGET};
+use super::exit::{exiting, reset_gate_for_tests, server_starting, shutdown_for_exit, EXIT_BUDGET};
/// The hook registry is process-global, so the two tests below must not see
/// each other's hooks: each registers and drains under this lock.
@@ -35,6 +35,30 @@ async fn exit_runs_registered_hooks_once() {
);
}
+/// The gate that refuses new bindings goes up only once a real server has
+/// started — a bare exit (every unit test in this process) must never leave
+/// memory refusing to bind — and a server starting again takes it down.
+#[tokio::test]
+async fn the_exit_gate_engages_only_behind_a_server() {
+ let _serial = REGISTRY.lock().await;
+ reset_gate_for_tests();
+
+ shutdown_for_exit().await;
+ assert!(
+ !exiting(),
+ "no server ever started, so exit must not gate bindings"
+ );
+
+ server_starting();
+ shutdown_for_exit().await;
+ assert!(exiting(), "behind a server, exit refuses new bindings");
+
+ server_starting();
+ assert!(!exiting(), "a server starting again takes the gate down");
+
+ reset_gate_for_tests();
+}
+
/// A hook that never answers must not hold the quit: exit returns within its
/// budget (plus the hooks' floor) and the process goes on without it.
#[tokio::test]