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()`.
debug_tests
Tyler Goodlet 2020-09-24 09:59:18 -04:00
parent 09daba4c9c
commit 9e1d9a8ce1
1 changed files with 8 additions and 2 deletions

View File

@ -13,6 +13,7 @@ from typing import Dict, List, Tuple, Any, Optional
from types import ModuleType from types import ModuleType
import sys import sys
import os import os
from contextlib import ExitStack
import trio # type: ignore import trio # type: ignore
from trio_typing import TaskStatus from trio_typing import TaskStatus
@ -150,7 +151,7 @@ async def _invoke(
# If we're cancelled before the task returns then the # If we're cancelled before the task returns then the
# cancel scope will not have been inserted yet # cancel scope will not have been inserted yet
log.warn( 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: if not actor._rpc_tasks:
log.info("All RPC tasks have completed") log.info("All RPC tasks have completed")
@ -175,6 +176,7 @@ class Actor:
_root_n: Optional[trio.Nursery] = None _root_n: Optional[trio.Nursery] = None
_service_n: Optional[trio.Nursery] = None _service_n: Optional[trio.Nursery] = None
_server_n: Optional[trio.Nursery] = None _server_n: Optional[trio.Nursery] = None
_lifetime_stack: ExitStack = ExitStack()
# Information about `__main__` from parent # Information about `__main__` from parent
_parent_main_data: Dict[str, str] _parent_main_data: Dict[str, str]
@ -426,7 +428,6 @@ class Actor:
async def _process_messages( async def _process_messages(
self, self,
chan: Channel, chan: Channel,
treat_as_gen: bool = False,
shield: bool = False, shield: bool = False,
task_status: TaskStatus[trio.CancelScope] = trio.TASK_STATUS_IGNORED, task_status: TaskStatus[trio.CancelScope] = trio.TASK_STATUS_IGNORED,
) -> None: ) -> None:
@ -743,6 +744,11 @@ class Actor:
finally: finally:
log.info("Root nursery complete") 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 # Unregister actor from the arbiter
if registered_with_arbiter and ( if registered_with_arbiter and (
self._arb_addr is not None self._arb_addr is not None