using shield_fg()

This commit is contained in:
Maxim Devaev 2022-08-07 19:18:23 +03:00
parent 1d4b39ef1b
commit fdc3edfa79
7 changed files with 16 additions and 30 deletions

View File

@ -66,7 +66,7 @@ class ExtrasInfoSubmanager(BaseInfoSubmanager):
return None
finally:
if sui is not None:
await asyncio.shield(sui.close())
await aiotools.shield_fg(sui.close())
def __get_extras_path(self, *parts: str) -> str:
return os.path.join(self.__global_config.kvmd.info.extras, *parts)

View File

@ -101,7 +101,7 @@ class RfbClient(RfbClientStream): # pylint: disable=too-many-instance-attribute
try:
await aiotools.wait_first(*tasks)
finally:
await asyncio.shield(self.__cleanup(tasks))
await aiotools.shield_fg(self.__cleanup(tasks))
async def __cleanup(self, tasks: List[asyncio.Task]) -> None:
for task in tasks:

View File

@ -147,7 +147,7 @@ class _Client(RfbClient): # pylint: disable=too-many-instance-attributes
fb_sender=self.__fb_sender_task_loop(),
)
finally:
await asyncio.shield(self.__cleanup())
await aiotools.shield_fg(self.__cleanup())
async def __cleanup(self) -> None:
if self.__kvmd_session:
@ -498,7 +498,7 @@ class VncServer: # pylint: disable=too-many-instance-attributes
except Exception:
logger.exception("%s [entry]: Unhandled exception in client task", remote)
finally:
await asyncio.shield(cleanup_client(writer))
await aiotools.shield_fg(cleanup_client(writer))
self.__handle_client = handle_client

View File

@ -170,7 +170,7 @@ class KvmdClientWs:
if writer_task:
writer_task.cancel()
try:
await asyncio.shield(self.__ws.close())
await aiotools.shield_fg(self.__ws.close())
except Exception:
pass
finally:

View File

@ -61,6 +61,8 @@ from .errors import IsBusyError
from .validators import ValidatorError
from . import aiotools
# =====
class HttpError(Exception):
@ -352,7 +354,7 @@ class HttpServer:
await self._on_ws_opened()
yield ws
finally:
await asyncio.shield(self.__close_ws(ws))
await aiotools.shield_fg(self.__close_ws(ws))
async def _ws_loop(self, ws: WsSession) -> WebSocketResponse:
logger = get_logger()

View File

@ -354,12 +354,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
yield self.__reader
finally:
# FIXME: Перехват важен потому что по какой-то причине await
# во вложенных finally путаются и выполняются не по порядку
try:
await asyncio.shield(self.__close_reader())
except asyncio.CancelledError:
pass
await aiotools.shield_fg(self.__close_reader())
finally:
self.__notifier.notify()
@ -404,20 +399,13 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
except Exception:
pass
try:
await asyncio.shield(self.__close_writer())
except asyncio.CancelledError:
pass
try:
await asyncio.shield(self.__remount_rw(False, fatal=False))
except asyncio.CancelledError:
pass
await aiotools.shield_fg(self.__close_writer())
finally:
await aiotools.shield_fg(self.__remount_rw(False, fatal=False))
finally:
# Между закрытием файла и эвентом айнотифи состояние может быть не обновлено,
# так что форсим обновление вручную, чтобы получить актуальное состояние.
try:
await asyncio.shield(self.__reload_state())
except asyncio.CancelledError:
pass
await aiotools.shield_fg(self.__reload_state())
@aiotools.atomic
async def remove(self, name: str) -> None:

View File

@ -250,13 +250,9 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
await self.__write_image_info(True)
finally:
try:
await asyncio.shield(self.__close_device_writer())
except asyncio.CancelledError:
pass
try:
await asyncio.shield(self.__load_device_info())
except asyncio.CancelledError:
pass
await aiotools.shield_fg(self.__close_device_writer())
finally:
await aiotools.shield_fg(self.__load_device_info())
@aiotools.atomic
async def remove(self, name: str) -> None: