The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(modules): close web3 and x402 services on shutdown
Steven Enamakelc6dcfd95da10
diff --git a/crates/openhuman-core/src/modules/web3_service.rs b/crates/openhuman-core/src/modules/web3_service.rsindex 0b0f010275..3ba27aaf6d 100644--- a/crates/openhuman-core/src/modules/web3_service.rs+++ b/crates/openhuman-core/src/modules/web3_service.rs@@ -2,6 +2,7 @@use std::collections::HashMap;use std::path::PathBuf;+use std::sync::atomic::{AtomicBool, Ordering};use std::sync::OnceLock;use serde::Deserialize;@@ -24,6 +25,7 @@ struct Web3Service {}static WEB3_SERVICE: OnceLock<tokio::sync::Mutex<Option<Web3Service>>> = OnceLock::new();+static WEB3_SHUTDOWN_HOOK_REGISTERED: AtomicBool = AtomicBool::new(false);fn service() -> &'static tokio::sync::Mutex<Option<Web3Service>> {WEB3_SERVICE.get_or_init(|| tokio::sync::Mutex::new(None))@@ -33,6 +35,7 @@ async fn ensure_service(config: &Config,active: &mut Option<Web3Service>,) -> Result<(), wallet::WalletCallError> {+ register_shutdown_hook();let accounts = wallet_host::wallet_accounts_snapshot().await.map_err(wallet::WalletCallError::Failed)?;@@ -104,6 +107,7 @@ fn owner() -> Option<QuoteOwner> {/// Close the active module service on sign-out. The mutex joins any in-flight/// quote, route, or execution call before its owner-scoped handles are cleared.pub(crate) async fn close_service(config: &Config) -> Result<(), wallet::WalletCallError> {+ register_shutdown_hook();let mut active = service().lock().await;let Some(current) = active.as_ref() else {return Ok(());@@ -121,6 +125,39 @@ pub(crate) async fn close_service(config: &Config) -> Result<(), wallet::WalletCOk(())}+fn register_shutdown_hook() {+ if WEB3_SHUTDOWN_HOOK_REGISTERED.swap(true, Ordering::AcqRel) {+ return;+ }+ crate::core::shutdown::register(|| async {+ WEB3_SHUTDOWN_HOOK_REGISTERED.store(false, Ordering::Release);+ let mut active = service().lock().await;+ let Some(current) = active.as_ref() else {+ return;+ };+ let config = crate::config::rpc::load_config_with_timeout().await;+ let Ok(config) = config else {+ tracing::warn!("[modules:web3] could not load configuration to close service at shutdown");+ return;+ };+ match wallet::call::<_, ()>(+ &config,+ methods::WEB3_CLOSE,+ Web3CloseRequest {+ service: current.response.service.clone(),+ },+ )+ .await+ {+ Ok(()) => {+ wallet_host::clear_endpoint_references(¤t.response.service.handle);+ *active = None;+ }+ Err(error) => tracing::warn!(%error, "[modules:web3] failed to close service at shutdown"),+ }+ });+}+#[derive(Deserialize)]#[serde(rename_all = "camelCase")]pub(crate) struct ExecuteParams {diff --git a/crates/openhuman-core/src/modules/x402_service.rs b/crates/openhuman-core/src/modules/x402_service.rsindex f487d4bde4..1209e11ca3 100644--- a/crates/openhuman-core/src/modules/x402_service.rs+++ b/crates/openhuman-core/src/modules/x402_service.rs@@ -1,6 +1,7 @@//! User-scoped lifecycle for the module-owned x402 ledger.use std::path::PathBuf;+use std::sync::atomic::{AtomicBool, Ordering};use std::sync::OnceLock;use tinywallet_bus::names::methods;@@ -98,6 +99,7 @@ struct X402Service {}static X402_SERVICE: OnceLock<tokio::sync::Mutex<Option<X402Service>>> = OnceLock::new();+static X402_SHUTDOWN_HOOK_REGISTERED: AtomicBool = AtomicBool::new(false);fn service() -> &'static tokio::sync::Mutex<Option<X402Service>> {X402_SERVICE.get_or_init(|| tokio::sync::Mutex::new(None))@@ -107,6 +109,7 @@ async fn ensure_service(config: &Config,active: &mut Option<X402Service>,) -> Result<(), WalletCallError> {+ register_shutdown_hook();let workspace = config.workspace_dir.clone();let accounts = super::wallet_host::wallet_accounts_snapshot().await@@ -166,6 +169,7 @@ async fn ensure_service(/// operation. If close fails, the handle stays cached so a later lifecycle/// attempt can retry instead of silently abandoning pending reservations.pub(crate) async fn close_service(config: &Config) -> Result<(), WalletCallError> {+ register_shutdown_hook();let mut active = service().lock().await;let Some(current) = active.as_ref() else {return Ok(());@@ -182,6 +186,35 @@ pub(crate) async fn close_service(config: &Config) -> Result<(), WalletCallErrorOk(())}+fn register_shutdown_hook() {+ if X402_SHUTDOWN_HOOK_REGISTERED.swap(true, Ordering::AcqRel) {+ return;+ }+ crate::core::shutdown::register(|| async {+ X402_SHUTDOWN_HOOK_REGISTERED.store(false, Ordering::Release);+ let mut active = service().lock().await;+ let Some(current) = active.as_ref() else {+ return;+ };+ let Ok(config) = crate::config::rpc::load_config_with_timeout().await else {+ tracing::warn!("[modules:x402] could not load configuration to close service at shutdown");+ return;+ };+ match wallet::call::<_, ()>(+ &config,+ methods::X402_CLOSE,+ X402CloseRequest {+ service: current.handle.clone(),+ },+ )+ .await+ {+ Ok(()) => *active = None,+ Err(error) => tracing::warn!(%error, "[modules:x402] failed to close service at shutdown"),+ }+ });+}+async fn with_service<R, F, Fut>(config: &Config, operation: F) -> Result<R, WalletCallError>whereF: FnOnce(X402ServiceHandle) -> Fut,