Skip to content

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

fix(threads): close temp file handle before rename on Windows

Steven Enamakelbab90bbd99e9
diff --git a/crates/openhuman-core/src/threads/turn_state/store.rs b/crates/openhuman-core/src/threads/turn_state/store.rs
index 87e11310ea..94913112cf 100644
--- a/crates/openhuman-core/src/threads/turn_state/store.rs
+++ b/crates/openhuman-core/src/threads/turn_state/store.rs
@@ -496,11 +496,21 @@ impl TurnStateStore {
#[cfg(windows)]
fn persist_temp_file(tmp: NamedTempFile, path: &Path) -> Result<(), String> {
- let (_file, temp_path) = tmp
+ let (file, temp_path) = tmp
.keep()
.map_err(|e| format!("persist turn-state file {}: {e}", path.display()))?;
- fs::rename(&temp_path, path)
- .map_err(|e| format!("persist turn-state file {}: {e}", path.display()))
+ // `keep` transfers ownership to us, so close the handle before replacing
+ // the destination and explicitly clean it up if the replacement fails.
+ drop(file);
+ fs::rename(&temp_path, path).map_err(|e| {
+ if let Err(cleanup_err) = fs::remove_file(&temp_path) {
+ warn!(
+ "{LOG_PREFIX} failed to remove turn-state tempfile {} after rename failure: {cleanup_err}",
+ temp_path.display()
+ );
+ }
+ format!("persist turn-state file {}: {e}", path.display())
+ })
}
#[cfg(not(windows))]
diff --git a/crates/openhuman-core/src/threads/turn_state/store_tests.rs b/crates/openhuman-core/src/threads/turn_state/store_tests.rs
index 7bf66ade94..b2db25a1d2 100644
--- a/crates/openhuman-core/src/threads/turn_state/store_tests.rs
+++ b/crates/openhuman-core/src/threads/turn_state/store_tests.rs
@@ -7,6 +7,9 @@ use crate::threads::turn_state::types::{
};
use tempfile::tempdir;
+#[cfg(windows)]
+use std::os::windows::ffi::OsStrExt;
+
fn sample_state(thread_id: &str) -> TurnState {
TurnState::started(thread_id.to_string(), "req-1", 25, "2026-05-04T10:00:00Z")
}
@@ -57,14 +60,32 @@ fn put_then_get_roundtrips_state() {
#[test]
fn put_roundtrips_snapshot_with_a_path_longer_than_max_path() {
let dir = tempdir().expect("tempdir");
- let workspace = dir.path().join("w".repeat(50));
+ let mut workspace = dir.path().to_path_buf();
let thread_id = "t".repeat(43);
let request_id = "r".repeat(36);
- let state = turn(&thread_id, &request_id, "2026-05-04T10:00:00Z");
+ while workspace
+ .join("memory")
+ .join("conversations")
+ .join("turn_states")
+ .join(hex::encode(&thread_id))
+ .join(format!("{}.json", hex::encode(&request_id)))
+ .as_os_str()
+ .encode_wide()
+ .count()
+ <= 260
+ {
+ workspace.push("long_workspace_segment");
+ }
+ let mut state = turn(&thread_id, &request_id, "2026-05-04T10:00:00Z");
let store = TurnStateStore::new(workspace);
store.put(&state).expect("persist a snapshot past MAX_PATH");
assert_eq!(store.get(&thread_id).expect("get"), Some(state));
+
+ // A regular progress flush replaces the same snapshot repeatedly.
+ state.iteration = 2;
+ store.put(&state).expect("replace snapshot past MAX_PATH");
+ assert_eq!(store.get(&thread_id).expect("get replacement"), Some(state));
}
#[test]