diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py b/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py index bdd0aa79e634..ca44b1ce74b1 100644 --- a/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py +++ b/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py @@ -31,7 +31,8 @@ def locked(self, key: Any): try: yield finally: - lock.release() + if lock.locked(): + lock.release() def acquire(self, key: Any) -> "RedisLock": """Acquires and returns a lock with the given key""" @@ -45,7 +46,7 @@ def acquire(self, key: Any) -> "RedisLock": return lock def release(self, key: Any): - if lock := self.locks.get(key): + if (lock := self.locks.get(key)) and lock.locked() and lock.owned(): lock.release() def release_all_locks(self): diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.py index 4dd2709c89d7..f39165b715f6 100644 --- a/autogpt_platform/backend/backend/executor/manager.py +++ b/autogpt_platform/backend/backend/executor/manager.py @@ -947,7 +947,8 @@ def synchronized(key: str, timeout: int = 60): lock.acquire() yield finally: - lock.release() + if lock.locked(): + lock.release() def llprint(message: str): diff --git a/autogpt_platform/backend/backend/integrations/creds_manager.py b/autogpt_platform/backend/backend/integrations/creds_manager.py index 17633bd58721..89bdb5bc9081 100644 --- a/autogpt_platform/backend/backend/integrations/creds_manager.py +++ b/autogpt_platform/backend/backend/integrations/creds_manager.py @@ -92,7 +92,7 @@ def get( fresh_credentials = oauth_handler.refresh_tokens(credentials) self.store.update_creds(user_id, fresh_credentials) - if _lock: + if _lock and _lock.locked(): _lock.release() credentials = fresh_credentials @@ -144,7 +144,8 @@ def _locked(self, user_id: str, credentials_id: str, *args: str): try: yield finally: - lock.release() + if lock.locked(): + lock.release() def release_all_locks(self): """Call this on process termination to ensure all locks are released"""