Skip to content

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

fix(profiles): serialize release with lifecycle lock and roll back archive on failure

Steven Enamakel12e163ba44cf
diff --git a/crates/openhuman-core/src/profiles/host.rs b/crates/openhuman-core/src/profiles/host.rs
index fafa95d6b6..b824275e8c 100644
--- a/crates/openhuman-core/src/profiles/host.rs
+++ b/crates/openhuman-core/src/profiles/host.rs
@@ -385,6 +385,8 @@ impl ProfileHost {
/// host it at once. Returns whether it was open here. A profile in use
/// is not released from under its work.
pub async fn release(&self, id: &ProfileId) -> Result<bool, String> {
+ // Lifecycle lock before the gate, as every lifecycle operation does.
+ let _profile = self.lifecycle.lock(id).await;
let _gate = self.gate.lock().await;
let Some(grant) = self.close_if_idle(id)? else {
return Ok(false);
diff --git a/crates/openhuman-core/src/profiles/lifecycle.rs b/crates/openhuman-core/src/profiles/lifecycle.rs
index d0c2cd582e..1c4589a831 100644
--- a/crates/openhuman-core/src/profiles/lifecycle.rs
+++ b/crates/openhuman-core/src/profiles/lifecycle.rs
@@ -176,6 +176,7 @@ impl ProfileHost {
// leave the secret for a re-provisioned profile to inherit.
return Err(format!("clearing credentials before archiving: {e}"));
}
+ let mut moved: Option<std::path::PathBuf> = None;
if layout.dir.exists() {
let archive = layout::archive_dir(&self.saas.root);
std::fs::create_dir_all(&archive)
@@ -188,8 +189,21 @@ impl ProfileHost {
));
std::fs::rename(&layout.dir, &dest)
.map_err(|e| format!("archiving {}: {e}", layout.dir.display()))?;
+ moved = Some(dest);
+ }
+ if let Err(error) = self.registry.remove(id).await {
+ // The record still advertises the profile: put its directory back
+ // so an open does not recreate an empty one over archived state.
+ if let Some(dest) = moved {
+ if let Err(e) = std::fs::rename(&dest, &layout.dir) {
+ log::error!(
+ "[profiles] restoring {} after a failed record removal failed: {e}",
+ layout.dir.display()
+ );
+ }
+ }
+ return Err(error);
}
- self.registry.remove(id).await?;
Ok(())
}
diff --git a/tests/saas_mode/hashed_ids.rs b/tests/saas_mode/hashed_ids.rs
index 0b50e4424d..6088702c0e 100644
--- a/tests/saas_mode/hashed_ids.rs
+++ b/tests/saas_mode/hashed_ids.rs
@@ -233,7 +233,9 @@ fn hashed_profile_ids_keep_user_ids_off_disk_out_of_responses_and_logs() {
}
assert_free_of_user_ids("a response", &seen);
for log in &logs {
- let text = std::fs::read_to_string(log).unwrap_or_default();
+ // The CLI server's logging layer writes to stderr (`<name>.err`).
+ let mut text = std::fs::read_to_string(log).unwrap_or_default();
+ text.push_str(&std::fs::read_to_string(log.with_extension("err")).unwrap_or_default());
assert!(
text.contains("[profiles]"),
"the log was captured: {}",