Eliminates the "coroutine was never awaited" warnings

 Properly handles lock acquisition with timeout
 Maintains the same functionality (timeout protection for lock acquisition)
 Ensures proper lock cleanup in the finally block
This commit is contained in:
Vasily.onl 2025-06-03 13:11:51 +08:00
parent c4ec3fac9f
commit 371c0a4591

View File

@ -546,34 +546,34 @@ class OKXWebSocketClient:
# Use lock to prevent concurrent reconnection attempts # Use lock to prevent concurrent reconnection attempts
try: try:
# Use asyncio.wait_for to prevent hanging on lock acquisition # Properly acquire lock with timeout
async with asyncio.wait_for(self._reconnection_lock.acquire(), timeout=5.0): await asyncio.wait_for(self._reconnection_lock.acquire(), timeout=5.0)
try: try:
# Double-check we still need to reconnect # Double-check we still need to reconnect
if (self._connection_state == ConnectionState.DISCONNECTED and if (self._connection_state == ConnectionState.DISCONNECTED and
self._reconnect_attempts < self.max_reconnect_attempts and self._reconnect_attempts < self.max_reconnect_attempts and
not self._tasks_stopping): not self._tasks_stopping):
self._reconnect_attempts += 1 self._reconnect_attempts += 1
if self.logger:
self.logger.info(f"{self.component_name}: Attempting automatic reconnection ({self._reconnect_attempts}/{self.max_reconnect_attempts})")
# Attempt reconnection (this will handle task cleanup)
if await self.reconnect():
if self.logger: if self.logger:
self.logger.info(f"{self.component_name}: Attempting automatic reconnection ({self._reconnect_attempts}/{self.max_reconnect_attempts})") self.logger.info(f"{self.component_name}: Automatic reconnection successful")
# Exit this handler as reconnect will start new tasks
# Attempt reconnection (this will handle task cleanup) break
if await self.reconnect():
if self.logger:
self.logger.info(f"{self.component_name}: Automatic reconnection successful")
# Exit this handler as reconnect will start new tasks
break
else:
if self.logger:
self.logger.error(f"{self.component_name}: Automatic reconnection failed")
break
else: else:
if self.logger: if self.logger:
self.logger.error(f"{self.component_name}: Max reconnection attempts exceeded or shutdown in progress") self.logger.error(f"{self.component_name}: Automatic reconnection failed")
break break
finally: else:
self._reconnection_lock.release() if self.logger:
self.logger.error(f"{self.component_name}: Max reconnection attempts exceeded or shutdown in progress")
break
finally:
self._reconnection_lock.release()
except asyncio.TimeoutError: except asyncio.TimeoutError:
if self.logger: if self.logger:
self.logger.warning(f"{self.component_name}: Timeout acquiring reconnection lock") self.logger.warning(f"{self.component_name}: Timeout acquiring reconnection lock")