SNIClient: log exceptions and keep task alive (#3911)

* SNIClient: log exceptions and keep task alive

* also log errors in `get_handler`
and disconnect if error in `game_watcher`
This commit is contained in:
Doug Hoskisson 2024-10-30 16:16:02 -07:00 committed by GitHub
parent 0b5c7fe8a9
commit 085b655ad9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 4 deletions

View File

@ -633,7 +633,13 @@ async def game_watcher(ctx: SNIContext) -> None:
if not ctx.client_handler: if not ctx.client_handler:
continue continue
rom_validated = await ctx.client_handler.validate_rom(ctx) try:
rom_validated = await ctx.client_handler.validate_rom(ctx)
except Exception as e:
snes_logger.error(f"An error occurred, see logs for details: {e}")
text_file_logger = logging.getLogger()
text_file_logger.exception(e)
rom_validated = False
if not rom_validated or (ctx.auth and ctx.auth != ctx.rom): if not rom_validated or (ctx.auth and ctx.auth != ctx.rom):
snes_logger.warning("ROM change detected, please reconnect to the multiworld server") snes_logger.warning("ROM change detected, please reconnect to the multiworld server")
@ -649,7 +655,13 @@ async def game_watcher(ctx: SNIContext) -> None:
perf_counter = time.perf_counter() perf_counter = time.perf_counter()
await ctx.client_handler.game_watcher(ctx) try:
await ctx.client_handler.game_watcher(ctx)
except Exception as e:
snes_logger.error(f"An error occurred, see logs for details: {e}")
text_file_logger = logging.getLogger()
text_file_logger.exception(e)
await snes_disconnect(ctx)
async def run_game(romfile: str) -> None: async def run_game(romfile: str) -> None:

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import abc import abc
import logging
from typing import TYPE_CHECKING, ClassVar, Dict, Iterable, Tuple, Any, Optional, Union from typing import TYPE_CHECKING, ClassVar, Dict, Iterable, Tuple, Any, Optional, Union
from typing_extensions import TypeGuard from typing_extensions import TypeGuard
@ -60,8 +61,12 @@ class AutoSNIClientRegister(abc.ABCMeta):
@staticmethod @staticmethod
async def get_handler(ctx: SNIContext) -> Optional[SNIClient]: async def get_handler(ctx: SNIContext) -> Optional[SNIClient]:
for _game, handler in AutoSNIClientRegister.game_handlers.items(): for _game, handler in AutoSNIClientRegister.game_handlers.items():
if await handler.validate_rom(ctx): try:
return handler if await handler.validate_rom(ctx):
return handler
except Exception as e:
text_file_logger = logging.getLogger()
text_file_logger.exception(e)
return None return None