fix(keenetic): reset session after auth probe, retry 503

Probing used short timeouts on the session reused for RCI calls.
Recreate the session after auth and treat 503 as transient.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Андрей Бобырев
2026-05-22 03:22:25 +03:00
parent dc161e151d
commit 306e687988

View File

@@ -177,11 +177,12 @@ class KeeneticClient:
) )
return self._session return self._session
async def _reset_session(self): async def _reset_session(self, *, keep_auth: bool = False):
if self._session and not self._session.closed: if self._session and not self._session.closed:
await self._session.close() await self._session.close()
self._session = None self._session = None
self._authenticated = False if not keep_auth:
self._authenticated = False
def _timeout_error_message(self) -> str: def _timeout_error_message(self) -> str:
if is_public_ip_host(self._host_key): if is_public_ip_host(self._host_key):
@@ -213,13 +214,16 @@ class KeeneticClient:
async with session.get(auth_url, ssl=False) as resp: async with session.get(auth_url, ssl=False) as resp:
if resp.status == 200: if resp.status == 200:
self._authenticated = True self._authenticated = True
await self._reset_session(keep_auth=True)
return True return True
if resp.status != 401: if resp.status != 401:
if resp.status in (400, 403): if resp.status in (400, 403):
self.last_error = "Wrong protocol or port (try http/https)" self.last_error = "Wrong protocol or port (try http/https)"
else: return False
self.last_error = f"Auth HTTP {resp.status}" if resp.status in (502, 503, 504):
continue
self.last_error = f"Auth HTTP {resp.status}"
logger.error( logger.error(
f"Keenetic auth unexpected status: {resp.status} @ {auth_url}" f"Keenetic auth unexpected status: {resp.status} @ {auth_url}"
) )
@@ -246,6 +250,7 @@ class KeeneticClient:
) as resp: ) as resp:
if resp.status == 200: if resp.status == 200:
self._authenticated = True self._authenticated = True
await self._reset_session(keep_auth=True)
return True return True
self.last_error = "Wrong login or password" self.last_error = "Wrong login or password"
logger.error(f"Keenetic auth failed: {resp.status} @ {auth_url}") logger.error(f"Keenetic auth failed: {resp.status} @ {auth_url}")