From c1138f976f4ba3649e2d2f40fd24b9d7e5127669 Mon Sep 17 00:00:00 2001 From: John Pals Date: Fri, 6 Nov 2026 10:52:49 +0100 Subject: [PATCH] fix(hub): release web service lock explicitly on drop service_lock relied on closing its fd to release the flock. A flock belongs to the open file description, which a child forked by another test thread shares until exec closes its O_CLOEXEC copy, so the lock could outlive the drop and web_service_lock_is_exclusive_and_released_on_drop failed intermittently. Return a guard that unlocks with LOCK_UN on drop. Co-Authored-By: Claude Opus 5.5 --- hub/src/runtime.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/hub/src/runtime.rs b/hub/src/runtime.rs index 4e7580c..20af65b 100644 --- a/hub/src/runtime.rs +++ b/hub/src/runtime.rs @@ -63,13 +63,25 @@ fn private_file(path: &Path) -> Result { .open(path)?) } +/// Exclusive ownership of a runtime directory by one web process. +pub struct ServiceLock(File); + +impl Drop for ServiceLock { + fn drop(&mut self) { + // A flock belongs to the open file description, which a child forked by + // another thread shares until its exec closes the O_CLOEXEC copy. + // Unlock explicitly so dropping the guard releases the lock at once. + unsafe { libc::flock(self.0.as_raw_fd(), libc::LOCK_UN) }; + } +} + /// Held by the web process, and automatically released on crash or termination. -pub fn service_lock(state_dir: &Path) -> Result { +pub fn service_lock(state_dir: &Path) -> Result { let file = private_file(&ensure(state_dir)?.join("web.lock"))?; if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } != 0 { bail!("another Portal Hub web process owns this runtime directory"); } - Ok(file) + Ok(ServiceLock(file)) } #[derive(Serialize, Deserialize)]