diff --git a/libervia/cli/base.py b/libervia/cli/base.py index 81450b80..d98a32ad 100644 --- a/libervia/cli/base.py +++ b/libervia/cli/base.py @@ -49,7 +49,7 @@ import libervia.cli from libervia.cli.loops import QuitException, get_libervia_cli_loop from libervia.cli.constants import Const as C from libervia.frontends.bridge.bridge_frontend import BridgeException -from libervia.frontends.tools import misc +from libervia.frontends.tools import aio, misc import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI from collections import OrderedDict from rich import console @@ -733,7 +733,9 @@ class LiberviaCli: try: self.loop.run(self, args, namespace) except SystemExit: - self.quit() + # The loop is stopped, but we execute it one more time to call `a_quit` which + # will call any cleaning method including async ones. + asyncio.get_event_loop().run_until_complete(self.a_quit()) @classmethod def run(cls): @@ -800,6 +802,22 @@ class LiberviaCli: if raise_exc: raise QuitException + async def a_quit(self, exit_code=0, raise_exc=True): + """Execute async quit callback before actually quitting + + This method should be prefered to ``quit``, as it executes async quit callbacks + which may be necessary for proper cleaning of session. + """ + try: + callbacks_list = self._on_quit_callbacks + except AttributeError: + pass + else: + for callback, args, kwargs in callbacks_list: + await aio.maybe_async(callback(*args, **kwargs)) + callbacks_list.clear() + self.quit(exit_code, raise_exc) + async def check_jids(self, jids): """Check jids validity, transform roster name to corresponding jids