Add locking for model download files

This commit is contained in:
Abinila Siva 2025-09-09 13:32:22 -04:00
parent fd6e7afea9
commit 2cacaf91e4

View File

@ -177,6 +177,29 @@ class MemryXDetector(DetectionApi):
logger.error(f"Failed to initialize MemryX model: {e}") logger.error(f"Failed to initialize MemryX model: {e}")
raise raise
def _acquire_file_lock(self, lock_path: str, timeout: int = 60, poll: float = 0.2):
"""
Create an exclusive lock file. Blocks (with polling) until it can acquire,
or raises TimeoutError. Uses only stdlib (os.O_EXCL).
"""
start = time.time()
while True:
try:
fd = os.open(lock_path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
os.close(fd)
return
except FileExistsError:
if time.time() - start > timeout:
raise TimeoutError(f"Timeout waiting for lock: {lock_path}")
time.sleep(poll)
def _release_file_lock(self, lock_path: str):
"""Best-effort removal of the lock file."""
try:
os.remove(lock_path)
except FileNotFoundError:
pass
def load_yolo_constants(self): def load_yolo_constants(self):
base = f"{self.cache_dir}/{self.model_folder}" base = f"{self.cache_dir}/{self.model_folder}"
# constants for yolov9 post-processing # constants for yolov9 post-processing
@ -188,6 +211,11 @@ class MemryXDetector(DetectionApi):
if not os.path.exists(self.cache_dir): if not os.path.exists(self.cache_dir):
os.makedirs(self.cache_dir, exist_ok=True) os.makedirs(self.cache_dir, exist_ok=True)
lock_path = os.path.join(self.cache_dir, f".{self.model_folder}.lock")
self._acquire_file_lock(lock_path)
try:
# ---------- CASE 1: user provided a custom model path ---------- # ---------- CASE 1: user provided a custom model path ----------
if self.memx_model_path: if self.memx_model_path:
if not self.memx_model_path.endswith(".zip"): if not self.memx_model_path.endswith(".zip"):
@ -305,6 +333,9 @@ class MemryXDetector(DetectionApi):
except Exception as e: except Exception as e:
logger.warning(f"Failed to remove downloaded zip {zip_path}: {e}") logger.warning(f"Failed to remove downloaded zip {zip_path}: {e}")
finally:
self._release_file_lock(lock_path)
def send_input(self, connection_id, tensor_input: np.ndarray): def send_input(self, connection_id, tensor_input: np.ndarray):
"""Pre-process (if needed) and send frame to MemryX input queue""" """Pre-process (if needed) and send frame to MemryX input queue"""
if tensor_input is None: if tensor_input is None: