summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Devaev <[email protected]>2022-08-05 18:19:31 +0300
committerMaxim Devaev <[email protected]>2022-08-05 18:19:31 +0300
commitd21e74700a5b236ea19981ff74724790a8bc4efc (patch)
tree9bda87884ece795b05a81bfc4863b4562e84d9ca
parenteeaeebf7c76385536eb8f5daaea5f21bda18b735 (diff)
shielded some tasks
-rw-r--r--kvmd/apps/janus/stun.py1
-rw-r--r--kvmd/apps/kvmd/info/extras.py2
-rw-r--r--kvmd/apps/kvmd/snapshoter.py2
-rw-r--r--kvmd/apps/vnc/rfb/__init__.py16
-rw-r--r--kvmd/apps/vnc/server.py16
-rw-r--r--kvmd/clients/kvmd.py5
-rw-r--r--kvmd/htserver.py2
-rw-r--r--kvmd/plugins/ugpio/otgconf.py6
8 files changed, 30 insertions, 20 deletions
diff --git a/kvmd/apps/janus/stun.py b/kvmd/apps/janus/stun.py
index a71cbd43..dc41b961 100644
--- a/kvmd/apps/janus/stun.py
+++ b/kvmd/apps/janus/stun.py
@@ -63,7 +63,6 @@ class Stun:
self.__sock: Optional[socket.socket] = None
async def get_info(self, src_ip: str, src_port: int) -> Tuple[str, str]:
-
(family, _, _, _, addr) = socket.getaddrinfo(src_ip, src_port, type=socket.SOCK_DGRAM)[0]
try:
with socket.socket(family, socket.SOCK_DGRAM) as self.__sock:
diff --git a/kvmd/apps/kvmd/info/extras.py b/kvmd/apps/kvmd/info/extras.py
index 37bf3ff7..3a1721c8 100644
--- a/kvmd/apps/kvmd/info/extras.py
+++ b/kvmd/apps/kvmd/info/extras.py
@@ -66,7 +66,7 @@ class ExtrasInfoSubmanager(BaseInfoSubmanager):
return None
finally:
if sui is not None:
- await sui.close()
+ await asyncio.shield(sui.close())
def __get_extras_path(self, *parts: str) -> str:
return os.path.join(self.__global_config.kvmd.info.extras, *parts)
diff --git a/kvmd/apps/kvmd/snapshoter.py b/kvmd/apps/kvmd/snapshoter.py
index 65c59eaa..0a8874a1 100644
--- a/kvmd/apps/kvmd/snapshoter.py
+++ b/kvmd/apps/kvmd/snapshoter.py
@@ -116,7 +116,7 @@ class Snapshoter: # pylint: disable=too-many-instance-attributes
logger.exception("Unhandled exception while taking snapshot")
finally:
self.__snapshoting = False
- await notifier.notify()
+ await asyncio.shield(notifier.notify())
async def __wakeup(self) -> None:
logger = get_logger(0)
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
diff --git a/kvmd/clients/kvmd.py b/kvmd/clients/kvmd.py
index 4a996be2..3f258a3b 100644
--- a/kvmd/clients/kvmd.py
+++ b/kvmd/clients/kvmd.py
@@ -170,10 +170,11 @@ class KvmdClientWs:
if writer_task:
writer_task.cancel()
try:
- await self.__ws.close()
+ await asyncio.shield(self.__ws.close())
except Exception:
pass
- self.__communicated = False
+ finally:
+ self.__communicated = False
async def send_key_event(self, key: str, state: bool) -> None:
await self.__writer_queue.put(("key", {"key": key, "state": state}))
diff --git a/kvmd/htserver.py b/kvmd/htserver.py
index 35f4b388..16d152b3 100644
--- a/kvmd/htserver.py
+++ b/kvmd/htserver.py
@@ -352,7 +352,7 @@ class HttpServer:
await self._on_ws_opened()
yield ws
finally:
- await self.__close_ws(ws)
+ await asyncio.shield(self.__close_ws(ws))
async def _ws_loop(self, ws: WsSession) -> WebSocketResponse:
logger = get_logger()
diff --git a/kvmd/plugins/ugpio/otgconf.py b/kvmd/plugins/ugpio/otgconf.py
index 8b888c8e..efe1b5c4 100644
--- a/kvmd/plugins/ugpio/otgconf.py
+++ b/kvmd/plugins/ugpio/otgconf.py
@@ -123,8 +123,10 @@ class Plugin(BaseUserGpioDriver):
else:
os.unlink(os.path.join(self.__profile_path, pin))
finally:
- await asyncio.sleep(self.__init_delay)
- self.__set_udc_enabled(True)
+ try:
+ await asyncio.sleep(self.__init_delay)
+ finally:
+ self.__set_udc_enabled(True)
def __set_udc_enabled(self, enabled: bool) -> None:
with open(self.__udc_path, "w") as udc_file: