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): Avoid releasing lock that is no longer owned by the current thread (#9878)

Zamil Majdyef022720d56d
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py b/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py
index ca44b1ce74..d8221eea0f 100644
--- a/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py
+++ b/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py
@@ -31,7 +31,7 @@ class RedisKeyedMutex:
try:
yield
finally:
- if lock.locked():
+ if lock.locked() and lock.owned():
lock.release()
def acquire(self, key: Any) -> "RedisLock":
diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.py
index a1df240afc..1f0c34a7bd 100644
--- a/autogpt_platform/backend/backend/executor/manager.py
+++ b/autogpt_platform/backend/backend/executor/manager.py
@@ -267,7 +267,7 @@ def execute_node(
raise e
finally:
# Ensure credentials are released even if execution fails
- if creds_lock and creds_lock.locked():
+ if creds_lock and creds_lock.locked() and creds_lock.owned():
try:
creds_lock.release()
except Exception as e:
@@ -1115,7 +1115,7 @@ def synchronized(key: str, timeout: int = 60):
lock.acquire()
yield
finally:
- if lock.locked():
+ if lock.locked() and lock.owned():
lock.release()
diff --git a/autogpt_platform/backend/backend/integrations/creds_manager.py b/autogpt_platform/backend/backend/integrations/creds_manager.py
index b0cab700fb..3abd90d90f 100644
--- a/autogpt_platform/backend/backend/integrations/creds_manager.py
+++ b/autogpt_platform/backend/backend/integrations/creds_manager.py
@@ -93,7 +93,7 @@ class IntegrationCredentialsManager:
fresh_credentials = oauth_handler.refresh_tokens(credentials)
self.store.update_creds(user_id, fresh_credentials)
- if _lock and _lock.locked():
+ if _lock and _lock.locked() and _lock.owned():
_lock.release()
credentials = fresh_credentials
@@ -145,7 +145,7 @@ class IntegrationCredentialsManager:
try:
yield
finally:
- if lock.locked():
+ if lock.locked() and lock.owned():
lock.release()
def release_all_locks(self):