shield some finally ops

This commit is contained in:
Maxim Devaev 2022-08-05 15:07:17 +03:00
parent 9ee63aba3e
commit eeaeebf7c7
2 changed files with 34 additions and 8 deletions

View File

@ -354,9 +354,17 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
yield self.__reader yield self.__reader
finally: finally:
await self.__close_reader() # FIXME: Перехват важен потому что по какой-то причине await
# во вложенных finally путаются и выполняются не по порядку
try:
await asyncio.shield(self.__close_reader())
except asyncio.CancelledError:
pass
finally: finally:
await self.__notifier.notify() try:
await asyncio.shield(self.__notifier.notify())
except asyncio.CancelledError:
pass
@contextlib.asynccontextmanager @contextlib.asynccontextmanager
async def write_image(self, name: str, size: int, remove_incomplete: Optional[bool]) -> AsyncGenerator[MsdFileWriter, None]: async def write_image(self, name: str, size: int, remove_incomplete: Optional[bool]) -> AsyncGenerator[MsdFileWriter, None]:
@ -398,13 +406,25 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
os.remove(path) os.remove(path)
except Exception: except Exception:
pass pass
await self.__close_writer() try:
await self.__remount_rw(False, fatal=False) await asyncio.shield(self.__close_writer())
except asyncio.CancelledError:
pass
try:
await asyncio.shield(self.__remount_rw(False, fatal=False))
except asyncio.CancelledError:
pass
finally: finally:
# Между закрытием файла и эвентом айнотифи состояние может быть не обновлено, # Между закрытием файла и эвентом айнотифи состояние может быть не обновлено,
# так что форсим обновление вручную, чтобы получить актуальное состояние. # так что форсим обновление вручную, чтобы получить актуальное состояние.
await self.__reload_state() try:
await self.__notifier.notify() await asyncio.shield(self.__reload_state())
except asyncio.CancelledError:
pass
try:
await asyncio.shield(self.__notifier.notify())
except asyncio.CancelledError:
pass
@aiotools.atomic @aiotools.atomic
async def remove(self, name: str) -> None: async def remove(self, name: str) -> None:

View File

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