From 9e1d9a8ce11d865dd0ed9ee5ad8263882f8e527b Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 24 Sep 2020 09:59:18 -0400 Subject: [PATCH] Add an internal context stack This aids with tearing down resources **after** the crash handling and debugger have completed. Leaving this internal for now but should eventually get a public convenience function like `tractor.context_stack()`. --- tractor/_actor.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tractor/_actor.py b/tractor/_actor.py index 5d2a35d..e5b8ef0 100644 --- a/tractor/_actor.py +++ b/tractor/_actor.py @@ -13,6 +13,7 @@ from typing import Dict, List, Tuple, Any, Optional from types import ModuleType import sys import os +from contextlib import ExitStack import trio # type: ignore from trio_typing import TaskStatus @@ -150,7 +151,7 @@ async def _invoke( # If we're cancelled before the task returns then the # cancel scope will not have been inserted yet log.warn( - f"Task {func} was likely cancelled before it was started") + f"Task {func} likely errored or cancelled before it started") if not actor._rpc_tasks: log.info("All RPC tasks have completed") @@ -175,6 +176,7 @@ class Actor: _root_n: Optional[trio.Nursery] = None _service_n: Optional[trio.Nursery] = None _server_n: Optional[trio.Nursery] = None + _lifetime_stack: ExitStack = ExitStack() # Information about `__main__` from parent _parent_main_data: Dict[str, str] @@ -426,7 +428,6 @@ class Actor: async def _process_messages( self, chan: Channel, - treat_as_gen: bool = False, shield: bool = False, task_status: TaskStatus[trio.CancelScope] = trio.TASK_STATUS_IGNORED, ) -> None: @@ -743,6 +744,11 @@ class Actor: finally: log.info("Root nursery complete") + # tear down all lifetime contexts + # api idea: ``tractor.open_context()`` + log.warn("Closing all actor lifetime contexts") + self._lifetime_stack.close() + # Unregister actor from the arbiter if registered_with_arbiter and ( self._arb_addr is not None