import threading from deerflow.sandbox.sandbox import Sandbox _FILE_OPERATION_LOCKS: dict[tuple[str, str], threading.Lock] = {} _FILE_OPERATION_LOCKS_GUARD = threading.Lock() def get_file_operation_lock_key(sandbox: Sandbox, path: str) -> tuple[str, str]: sandbox_id = getattr(sandbox, "id", None) if not sandbox_id: sandbox_id = f"instance:{id(sandbox)}" return sandbox_id, path def get_file_operation_lock(sandbox: Sandbox, path: str) -> threading.Lock: lock_key = get_file_operation_lock_key(sandbox, path) with _FILE_OPERATION_LOCKS_GUARD: lock = _FILE_OPERATION_LOCKS.get(lock_key) if lock is None: lock = threading.Lock() _FILE_OPERATION_LOCKS[lock_key] = lock return lock