Skip to content

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

fix(modules): do not wait on a live import claim from the module import path

M3gA-Mindb7fe9ea9eaf4
diff --git a/crates/openhuman-core/src/modules/storage_import_support.rs b/crates/openhuman-core/src/modules/storage_import_support.rs
index cfe11bcf32..c348305b4e 100644
--- a/crates/openhuman-core/src/modules/storage_import_support.rs
+++ b/crates/openhuman-core/src/modules/storage_import_support.rs
@@ -128,6 +128,11 @@ pub(super) fn module_scope(
/// storage handle bound to the same claim condition and must use it for both
/// payload writes and the completion marker. A lost claim therefore prevents
/// late payload or completion writes.
+///
+/// Returns `Ok(None)` at once when another holder's claim is live, so an
+/// import on the compaction path never blocks a runtime worker (or the
+/// once-per-process import lock) for the claim TTL; it is retried at the next
+/// call.
pub(super) fn with_module_claim<T>(
ctx: &crate::core::runtime::DispatchCtx,
module_id: &str,
@@ -154,7 +159,7 @@ where
)?;
let claim_backend = Arc::clone(&backend);
let claim_scope = scope.clone();
- let claimed = crate::storage::import_state::with_claim(
+ let claimed = crate::storage::import_state::try_claim(
&repo,
crate::storage::import_state::HOST_OWNER,
&crate::storage::import_state::step_id(marker_kind, marker_id),
diff --git a/crates/openhuman-core/src/storage/import_state.rs b/crates/openhuman-core/src/storage/import_state.rs
index cc9d978452..e264c83dc4 100644
--- a/crates/openhuman-core/src/storage/import_state.rs
+++ b/crates/openhuman-core/src/storage/import_state.rs
@@ -250,12 +250,36 @@ pub fn with_claim<T>(
with_claim_ttl(repo, owner, step, CLAIM_TTL_MS, body)
}
+/// Runs `body` under a claim like [`with_claim`], but returns `Ok(None)` at
+/// once when another holder's claim is live instead of polling for up to the
+/// claim TTL. For callers on an async path that retry at their next call.
+pub fn try_claim<T>(
+ repo: &Repo,
+ owner: &str,
+ step: &str,
+ body: impl FnOnce(&WriteCondition) -> Result<T>,
+) -> Result<Option<T>> {
+ claim_waiting(repo, owner, step, CLAIM_TTL_MS, Duration::ZERO, body)
+}
+
fn with_claim_ttl<T>(
repo: &Repo,
owner: &str,
step: &str,
ttl_ms: i64,
body: impl FnOnce(&WriteCondition) -> Result<T>,
+) -> Result<Option<T>> {
+ let max_wait = Duration::from_millis(CLAIM_TTL_MS.max(0) as u64);
+ claim_waiting(repo, owner, step, ttl_ms, max_wait, body)
+}
+
+fn claim_waiting<T>(
+ repo: &Repo,
+ owner: &str,
+ step: &str,
+ ttl_ms: i64,
+ max_wait: Duration,
+ body: impl FnOnce(&WriteCondition) -> Result<T>,
) -> Result<Option<T>> {
let started = Instant::now();
let mut body = Some(body);
@@ -283,7 +307,7 @@ fn with_claim_ttl<T>(
}
return result.map(Some);
}
- if started.elapsed() >= Duration::from_millis(CLAIM_TTL_MS.max(0) as u64) {
+ if started.elapsed() >= max_wait {
return Ok(None);
}
std::thread::sleep(WAIT_POLL);
diff --git a/crates/openhuman-core/src/storage/import_state_tests.rs b/crates/openhuman-core/src/storage/import_state_tests.rs
index bbc3b3124e..b659c3b5ba 100644
--- a/crates/openhuman-core/src/storage/import_state_tests.rs
+++ b/crates/openhuman-core/src/storage/import_state_tests.rs
@@ -217,6 +217,35 @@ fn a_legacy_marker_row_is_honoured_and_removed() {
.unwrap();
}
+#[test]
+fn try_claim_returns_at_once_while_another_holder_is_live() {
+ let repo = repo();
+ let (started_tx, started_rx) = mpsc::channel::<()>();
+ let (release_tx, release_rx) = mpsc::channel::<()>();
+ let holder_repo = repo.clone();
+ let holder = std::thread::spawn(move || {
+ with_claim(&holder_repo, OWNER, "m", |_| {
+ started_tx.send(()).unwrap();
+ release_rx.recv().unwrap();
+ Ok(())
+ })
+ .unwrap()
+ });
+ started_rx.recv().unwrap();
+ let (done_tx, done_rx) = mpsc::channel();
+ let rival_repo = repo.clone();
+ std::thread::spawn(move || {
+ let _ = done_tx.send(try_claim(&rival_repo, OWNER, "m", |_| Ok(())).unwrap());
+ });
+ let attempt = done_rx.recv_timeout(Duration::from_secs(2));
+ release_tx.send(()).unwrap();
+ assert!(
+ matches!(attempt, Ok(None)),
+ "a live rival claim must not make the caller wait: {attempt:?}"
+ );
+ assert_eq!(holder.join().unwrap(), Some(()));
+}
+
#[test]
fn a_waiting_process_runs_once_the_holder_releases() {
let repo = repo();