Skip to content

Commit

Permalink
Add an internal context stack
Browse files Browse the repository at this point in the history
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()`.
  • Loading branch information
goodboy committed Sep 24, 2020
1 parent 482b623 commit 8de9493
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tractor/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from types import ModuleType
import sys
import os
from contextlib import ExitStack

import trio # type: ignore
from trio_typing import TaskStatus
Expand Down Expand Up @@ -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")
Expand All @@ -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]
Expand Down Expand Up @@ -426,7 +428,6 @@ async def send_cmd(
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:
Expand Down Expand Up @@ -743,6 +744,11 @@ async def _async_main(
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
Expand Down

0 comments on commit 8de9493

Please sign in to comment.