diff options
author | Maxim Devaev <[email protected]> | 2022-08-05 18:19:31 +0300 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2022-08-05 18:19:31 +0300 |
commit | d21e74700a5b236ea19981ff74724790a8bc4efc (patch) | |
tree | 9bda87884ece795b05a81bfc4863b4562e84d9ca /kvmd/apps/vnc | |
parent | eeaeebf7c76385536eb8f5daaea5f21bda18b735 (diff) |
shielded some tasks
Diffstat (limited to 'kvmd/apps/vnc')
-rw-r--r-- | kvmd/apps/vnc/rfb/__init__.py | 16 | ||||
-rw-r--r-- | kvmd/apps/vnc/server.py | 16 |
2 files changed, 20 insertions, 12 deletions
diff --git a/kvmd/apps/vnc/rfb/__init__.py b/kvmd/apps/vnc/rfb/__init__.py index 5d7847a9..172d52ba 100644 --- a/kvmd/apps/vnc/rfb/__init__.py +++ b/kvmd/apps/vnc/rfb/__init__.py @@ -93,8 +93,7 @@ class RfbClient(RfbClientStream): # pylint: disable=too-many-instance-attribute # ===== async def _run(self, **coros: Coroutine) -> None: - logger = get_logger(0) - logger.info("%s [entry]: Starting client tasks ...", self._remote) + get_logger(0).info("%s [entry]: Starting client tasks ...", self._remote) tasks = list(map(asyncio.create_task, [ # type: ignore # Я хз, почему github action фейлится здесь self.__wrapper(name, coro) for (name, coro) in {"main": self.__main_task_loop(), **coros}.items() @@ -102,11 +101,14 @@ class RfbClient(RfbClientStream): # pylint: disable=too-many-instance-attribute try: await aiotools.wait_first(*tasks) finally: - for task in tasks: - task.cancel() - await asyncio.gather(*tasks, return_exceptions=True) - await self._close() - logger.info("%s [entry]: Connection closed", self._remote) + await asyncio.shield(self.__cleanup(tasks)) + + async def __cleanup(self, tasks: List[asyncio.Task]) -> None: + for task in tasks: + task.cancel() + await asyncio.gather(*tasks, return_exceptions=True) + await self._close() + get_logger(0).info("%s [entry]: Connection closed", self._remote) async def __wrapper(self, name: str, coro: Coroutine) -> None: logger = get_logger(0) diff --git a/kvmd/apps/vnc/server.py b/kvmd/apps/vnc/server.py index 2f822ab9..54974bfb 100644 --- a/kvmd/apps/vnc/server.py +++ b/kvmd/apps/vnc/server.py @@ -147,9 +147,12 @@ class _Client(RfbClient): # pylint: disable=too-many-instance-attributes fb_sender=self.__fb_sender_task_loop(), ) finally: - if self.__kvmd_session: - await self.__kvmd_session.close() - self.__kvmd_session = None + await asyncio.shield(self.__cleanup()) + + async def __cleanup(self) -> None: + if self.__kvmd_session: + await self.__kvmd_session.close() + self.__kvmd_session = None # ===== @@ -446,6 +449,10 @@ class VncServer: # pylint: disable=too-many-instance-attributes shared_params = _SharedParams() + async def cleanup_client(writer: asyncio.StreamWriter) -> None: + if (await aiotools.close_writer(writer)): + get_logger(0).info("%s [entry]: Connection is closed in an emergency", rfb_format_remote(writer)) + async def handle_client(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: logger = get_logger(0) remote = rfb_format_remote(writer) @@ -491,8 +498,7 @@ class VncServer: # pylint: disable=too-many-instance-attributes except Exception: logger.exception("%s [entry]: Unhandled exception in client task", remote) finally: - if (await aiotools.close_writer(writer)): - logger.info("%s [entry]: Connection is closed in an emergency", remote) + await asyncio.shield(cleanup_client(writer)) self.__handle_client = handle_client |