From 782d93bf996413f15328fdaef54ad8f84a7b8efe Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 23:10:32 +0200 Subject: [PATCH] Release the session lock before closing it, as DirectoryLock.close does A world's session.lock is an flock held by the open file. Closing the file releases it only once every copy of the descriptor is gone, and a process spawned on another thread holds a copy until it execs. Under a parallel cargo test (crash reports spawn uname/sw_vers), deleting a world right after dropping its lock could then fail with "already open". Unlock explicitly in Drop, as vanilla releases the FileLock before closing the channel. Fixes #749 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/save.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index f46be9ccc..2e1eb8455 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -154,7 +154,15 @@ pub fn dimension_dir(world: &Path, dimension: &str) -> PathBuf { } /// `DirectoryLock`: `session.lock` holds "\u{2603}" (UTF-16BE) under an exclusive /// file lock for as long as the world is open, so two instances cannot share it. -pub struct SessionLock(#[allow(dead_code)] fs::File); +pub struct SessionLock(fs::File); +impl Drop for SessionLock { + /// `DirectoryLock.close`: release the lock, then close the file. Closing + /// alone keeps the lock while any copy of the descriptor lives on, such as + /// the one a process spawned on another thread holds until its `exec`. + fn drop(&mut self) { + let _ = self.0.unlock(); + } +} impl SessionLock { pub fn acquire(world: &Path) -> Result { use std::io::Write; @@ -3705,6 +3713,19 @@ mod tests { assert!(super::SessionLock::acquire(&dir).is_ok()); let _ = std::fs::remove_dir_all(&dir); } + /// #749: a copy of the lock's descriptor (a child process spawned on + /// another thread holds one until it execs) must not keep a released + /// world locked. + #[test] + fn session_lock_releases_while_a_descriptor_copy_lives() { + let dir = std::env::temp_dir().join(format!("horizon-lock-copy-{}", std::process::id())); + let lock = super::SessionLock::acquire(&dir).expect("first lock"); + let copy = lock.0.try_clone().unwrap(); + drop(lock); + assert!(super::SessionLock::acquire(&dir).is_ok()); + drop(copy); + let _ = std::fs::remove_dir_all(&dir); + } #[test] fn crafter_block_entity_round_trips() { let mut crafter = crate::containers::Crafter::default();