From 0a8eb11c3db20aa62c78641b3aea16d3c2150c6c Mon Sep 17 00:00:00 2001 From: Mohan <158349177+mohansinghi@users.noreply.github.com> Date: Sun, 18 Jan 2026 17:48:10 -0800 Subject: [PATCH] fix: Add proper error handling for database reconnection attempts (#12650) ## Problem When database connection is lost, the reconnection logic had a bug: if the first reconnect attempt failed, the second attempt was not wrapped in error handling, causing unhandled exceptions. ## Solution Added proper try-except blocks around the second reconnect attempt in both MySQL and PostgreSQL database classes to ensure errors are properly logged and handled. ## Changes - Fixed `_handle_connection_loss()` in `RetryingPooledMySQLDatabase` - Fixed `_handle_connection_loss()` in `RetryingPooledPostgresqlDatabase` Fixes #12294 --- Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=158349177 Co-authored-by: SID <158349177+0xsid0703@users.noreply.github.com> --- api/db/db_models.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/api/db/db_models.py b/api/db/db_models.py index 080613b84..cdd986c48 100644 --- a/api/db/db_models.py +++ b/api/db/db_models.py @@ -281,7 +281,11 @@ class RetryingPooledMySQLDatabase(PooledMySQLDatabase): except Exception as e: logging.error(f"Failed to reconnect: {e}") time.sleep(0.1) - self.connect() + try: + self.connect() + except Exception as e2: + logging.error(f"Failed to reconnect on second attempt: {e2}") + raise def begin(self): for attempt in range(self.max_retries + 1): @@ -352,7 +356,11 @@ class RetryingPooledPostgresqlDatabase(PooledPostgresqlDatabase): except Exception as e: logging.error(f"Failed to reconnect to PostgreSQL: {e}") time.sleep(0.1) - self.connect() + try: + self.connect() + except Exception as e2: + logging.error(f"Failed to reconnect to PostgreSQL on second attempt: {e2}") + raise def begin(self): for attempt in range(self.max_retries + 1):