Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): Added locking status check before releasing to avoid releasing timing out lock (#9135)

Zamil Majdya646e60d2fa5
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py b/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py
index bdd0aa79e6..ca44b1ce74 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 @@ class RedisKeyedMutex:
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 @@ class RedisKeyedMutex:
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 4dd2709c89..f39165b715 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 17633bd587..89bdb5bc90 100644
--- a/autogpt_platform/backend/backend/integrations/creds_manager.py
+++ b/autogpt_platform/backend/backend/integrations/creds_manager.py
@@ -92,7 +92,7 @@ class IntegrationCredentialsManager:
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 @@ class IntegrationCredentialsManager:
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"""