Skip to content

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

fix(profiles): renew leases under the host gate

Steven Enamakel22b742c88e6f
diff --git a/crates/openhuman-core/src/profiles/host.rs b/crates/openhuman-core/src/profiles/host.rs
index e24f80b1bb..8989401409 100644
--- a/crates/openhuman-core/src/profiles/host.rs
+++ b/crates/openhuman-core/src/profiles/host.rs
@@ -538,6 +538,11 @@ impl ProfileHost {
.collect()
}
+ /// The current grant of `id`, when it is open here.
+ pub(crate) fn grant_of(&self, id: &ProfileId) -> Option<LeaseGrant> {
+ self.lock().get(id).map(|slot| slot.grant.clone())
+ }
+
/// Replace `id`'s grant `old` with `renewed`, unless the slot moved on
/// (closed, or re-opened under a newer grant) meanwhile.
pub(crate) fn store_grant(&self, id: &ProfileId, old: &LeaseGrant, renewed: LeaseGrant) {
diff --git a/crates/openhuman-core/src/profiles/lease.rs b/crates/openhuman-core/src/profiles/lease.rs
index a45ccbe0b2..00b0f07bcd 100644
--- a/crates/openhuman-core/src/profiles/lease.rs
+++ b/crates/openhuman-core/src/profiles/lease.rs
@@ -147,11 +147,25 @@ pub struct HeartbeatReport {
/// is gone (see the module docs).
pub async fn renew_once(host: &ProfileHost) -> HeartbeatReport {
let mut report = HeartbeatReport::default();
- for (id, grant) in host.grants() {
+ for (id, _) in host.grants() {
+ // Renew and store the new grant under the host gate, which every
+ // close-and-release holds: a renewal supersedes the slot's grant in
+ // the lease store, so a release that took the slot's grant between
+ // the two would carry the superseded one and fail as lost. Read the
+ // grant again under the gate, since the slot may have moved on.
+ let gate = host.gate.lock().await;
+ let Some(grant) = host.grant_of(&id) else {
+ log::debug!("[profiles][lease] profile={id} closed before its renewal");
+ continue;
+ };
let now = now_ms();
- match host.leases().renew(&grant, now).await {
- Ok(renewed) => {
- host.store_grant(&id, &grant, renewed);
+ let outcome = host.leases().renew(&grant, now).await;
+ if let Ok(renewed) = &outcome {
+ host.store_grant(&id, &grant, renewed.clone());
+ }
+ drop(gate);
+ match outcome {
+ Ok(_) => {
report.renewed += 1;
}
Err(LeaseError::Lost) => {