diff --git a/src/dls_servbase_lib/datafaces/context.py b/src/dls_servbase_lib/datafaces/context.py index 0402e14..0888cb7 100644 --- a/src/dls_servbase_lib/datafaces/context.py +++ b/src/dls_servbase_lib/datafaces/context.py @@ -63,23 +63,23 @@ async def aenter(self) -> None: await self.server.activate() # ---------------------------------------------------------------------------------------- - async def aexit(self, type, value, traceback) -> None: + async def aexit(self, type=None, value=None, traceback=None): """ Asyncio context exit. Stop service if one was started and releases any client resources. """ + logger.debug(f"[DISSHU] {thing_type} aexit") if self.server is not None: start_as = self.context_specification.get("start_as") if start_as == "process": - logger.info( - "[DISSHU] in context exit, sending shutdown to client process" - ) - # Put in request to shutdown the server. - await self.server.client_shutdown() - logger.info("[DISSHU] in context exit, sent shutdown to client process") + # The server associated with this context is running? + if await self.is_process_alive(): + logger.debug(f"[DISSHU] {thing_type} calling client_shutdown") + # Put in request to shutdown the server. + await self.server.client_shutdown() if start_as == "coro": await self.server.direct_shutdown() diff --git a/src/dls_servbase_lib/guis/context.py b/src/dls_servbase_lib/guis/context.py index b0a5753..6ab3818 100644 --- a/src/dls_servbase_lib/guis/context.py +++ b/src/dls_servbase_lib/guis/context.py @@ -37,12 +37,23 @@ async def aenter(self): await self.server.start_process() # ---------------------------------------------------------------------------------------- - async def aexit(self, type, value, traceback): - """ """ + async def aexit(self, type=None, value=None, traceback=None): + """ + Asyncio context exit. + + Stop service if one was started and releases any client resources. + """ + logger.debug(f"[DISSHU] {thing_type} aexit") if self.server is not None: - # Put in request to shutdown the server. - await self.server.client_shutdown() + start_as = self.context_specification.get("start_as") + + if start_as == "process": + # The server associated with this context is running? + if await self.is_process_alive(): + logger.debug(f"[DISSHU] {thing_type} calling client_shutdown") + # Put in request to shutdown the server. + await self.server.client_shutdown() - # Release a client connection if we had one. - await self.server.close_client_session() + if start_as == "coro": + await self.server.direct_shutdown()