Skip to content

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

feat(mcp): add lock-free fast path for host lookup

Steven Enamakeldb3ec4b5e349
diff --git a/src/openhuman/mcp/host.rs b/src/openhuman/mcp/host.rs
index 4692ec230c..28c8309725 100644
--- a/src/openhuman/mcp/host.rs
+++ b/src/openhuman/mcp/host.rs
@@ -138,9 +138,23 @@ impl McpHost {
///
/// Returns an error when the stores cannot be opened or an HTTP client cannot
/// be built.
+/// Opens the service for `config`, without holding the process-wide lock.
+///
+/// Building a host runs the store's migrations and constructs an HTTP client,
+/// which no request path should pay for and no lock should be held across.
+/// So the host is opened first, and only then is the map locked to see whether
+/// another initializer won the race; if one did, its service is returned and
+/// the freshly-built one is dropped.
pub fn for_config(config: &Config) -> anyhow::Result<Arc<McpHost>> {
let workspace = config.workspace_dir.clone();
+ // Fast path: a host for this workspace already exists.
+ if let Some(existing) = try_get_host(&workspace) {
+ return Ok(existing);
+ }
+
+ let service = Arc::new(McpHost::open(config)?);
+
let mut hosts = HOSTS
.get_or_init(|| Mutex::new(HashMap::new()))
.lock()
@@ -152,12 +166,19 @@ pub fn for_config(config: &Config) -> anyhow::Result<Arc<McpHost>> {
return Ok(Arc::clone(existing));
}
- let service = Arc::new(McpHost::open(config)?);
hosts.insert(workspace, Arc::clone(&service));
Ok(service)
}
+/// The registered host for `workspace`, if any. Lock-free read used to short-
+/// circuit [`for_config`] on the common path.
+fn try_get_host(workspace: &Path) -> Option<Arc<McpHost>> {
+ let hosts = HOSTS.get()?;
+ let hosts = hosts.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
+ hosts.get(workspace).cloned()
+}
+
/// Opens the service for `config` and marks its workspace the default.
///
/// Called once, from the core startup path. A second call opens nothing new and