Skip to content

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

chore(runtime): track dispatch context clones for leak diagnostics

Steven Enamakel9ab91ab5654f
diff --git a/crates/openhuman-core/src/core/runtime/context_agent.rs b/crates/openhuman-core/src/core/runtime/context_agent.rs
index be5d62128b..711b0387d9 100644
--- a/crates/openhuman-core/src/core/runtime/context_agent.rs
+++ b/crates/openhuman-core/src/core/runtime/context_agent.rs
@@ -90,7 +90,7 @@ impl CoreContext {
///
/// A host must not close or evict a tenant while this is `true`: its
/// in-flight tables (turns, queues, caches) live in those slots.
- pub fn debug_counts(self: &Arc<Self>) -> usize { let n = Arc::strong_count(&self.agent.state); if n > 1 { for (w, bt) in super::DBG_DERIVED.lock().unwrap().iter() { if let Some(c) = w.upgrade() { if Arc::ptr_eq(&c.agent.state, &self.agent.state) && !Arc::ptr_eq(&c, self) { for l in bt.lines().filter(|l| l.contains("openhuman") && !l.trim_start().starts_with("at ")).take(12) { log::warn!("DBGLEAK {}", l.trim()); } } } } } n }
+ pub fn debug_counts(self: &Arc<Self>) -> usize { let n = Arc::strong_count(&self.agent.state); if n > 1 { for (t, w, bt) in super::dispatch_ctx::DBG_TRACK.lock().unwrap().iter() { if t.strong_count() > 0 { if let Some(c) = w.upgrade() { if Arc::ptr_eq(&c.agent.state, &self.agent.state) { for l in bt.lines().filter(|l| l.contains("openhuman") && !l.trim_start().starts_with("at ")).take(9) { log::warn!("DBGHOLD {}", l.trim()); } log::warn!("DBGHOLD ----"); } } } } for (w, bt) in super::DBG_DERIVED.lock().unwrap().iter() { if let Some(c) = w.upgrade() { if Arc::ptr_eq(&c.agent.state, &self.agent.state) && !Arc::ptr_eq(&c, self) { for l in bt.lines().filter(|l| l.contains("openhuman") && !l.trim_start().starts_with("at ")).take(12) { log::warn!("DBGLEAK {}", l.trim()); } } } } } n }
pub fn tenant_in_use(self: &Arc<Self>) -> bool {
Arc::strong_count(self) > 1 || Arc::strong_count(&self.agent.state) > 1
diff --git a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
index 1ffb7bc8c5..27045fff84 100644
--- a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
+++ b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
@@ -21,10 +21,18 @@ use super::CoreContext;
/// Cheap to clone (one `Arc`). It is `unbound` only when no [`CoreContext`]
/// exists at all: a unit test that never booted the core, or a call made
/// before boot. A booted host always dispatches with a bound context.
-#[derive(Clone, Default)]
+#[derive(Default)]
pub struct DispatchCtx {
core: Option<Arc<CoreContext>>,
headless_agent_chat: bool,
+ track: Option<Arc<()>>,
+}
+pub static DBG_TRACK: std::sync::Mutex<Vec<(std::sync::Weak<()>, std::sync::Weak<CoreContext>, String)>> = std::sync::Mutex::new(Vec::new());
+impl Clone for DispatchCtx {
+ fn clone(&self) -> Self {
+ let track = self.core.as_ref().map(|c| { let t = Arc::new(()); DBG_TRACK.lock().unwrap().push((Arc::downgrade(&t), Arc::downgrade(c), std::backtrace::Backtrace::force_capture().to_string())); t });
+ Self { core: self.core.clone(), headless_agent_chat: self.headless_agent_chat, track }
+ }
}
impl DispatchCtx {
@@ -34,6 +42,7 @@ impl DispatchCtx {
Self {
core: Some(core),
headless_agent_chat: false,
+ track: None,
}
}
@@ -51,6 +60,7 @@ impl DispatchCtx {
Self {
core: None,
headless_agent_chat: false,
+ track: None,
}
}
@@ -61,6 +71,7 @@ impl DispatchCtx {
Self {
core: CoreContext::default_context(),
headless_agent_chat: false,
+ track: None,
}
}
@@ -81,6 +92,7 @@ impl DispatchCtx {
Self {
core,
headless_agent_chat: false,
+ track: None,
}
}
@@ -153,6 +165,7 @@ impl DispatchCtx {
Self {
core: CoreContext::scoped_for_turn(),
headless_agent_chat: false,
+ track: None,
}
}