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>
This commit is contained in:
Mohan
2026-01-18 17:48:10 -08:00
committed by GitHub
parent 38f0a92da9
commit 0a8eb11c3d

View File

@ -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):