forked from goodboy/tractor
Unwind process opening and shield hard reap
parent
bb9d9c74b1
commit
46ff558556
|
@ -174,11 +174,30 @@ async def do_hard_kill(
|
||||||
proc.kill()
|
proc.kill()
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
async def new_proc(
|
||||||
async def spawn_subactor(
|
name: str,
|
||||||
subactor: 'Actor',
|
actor_nursery: 'ActorNursery', # type: ignore # noqa
|
||||||
|
subactor: Actor,
|
||||||
|
errors: Dict[Tuple[str, str], Exception],
|
||||||
|
# passed through to actor main
|
||||||
|
bind_addr: Tuple[str, int],
|
||||||
parent_addr: Tuple[str, int],
|
parent_addr: Tuple[str, int],
|
||||||
):
|
_runtime_vars: Dict[str, Any], # serialized and sent to _child
|
||||||
|
*,
|
||||||
|
task_status: TaskStatus[Portal] = trio.TASK_STATUS_IGNORED
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Create a new ``multiprocessing.Process`` using the
|
||||||
|
spawn method as configured using ``try_set_start_method()``.
|
||||||
|
|
||||||
|
"""
|
||||||
|
cancel_scope = None
|
||||||
|
|
||||||
|
# mark the new actor with the global spawn method
|
||||||
|
subactor._spawn_method = _spawn_method
|
||||||
|
|
||||||
|
if _spawn_method == 'trio':
|
||||||
|
|
||||||
spawn_cmd = [
|
spawn_cmd = [
|
||||||
sys.executable,
|
sys.executable,
|
||||||
"-m",
|
"-m",
|
||||||
|
@ -202,46 +221,9 @@ async def spawn_subactor(
|
||||||
subactor.loglevel
|
subactor.loglevel
|
||||||
]
|
]
|
||||||
|
|
||||||
proc = await trio.open_process(spawn_cmd)
|
|
||||||
try:
|
try:
|
||||||
yield proc
|
proc = await trio.open_process(spawn_cmd)
|
||||||
|
|
||||||
finally:
|
|
||||||
log.runtime(f"Attempting to kill {proc}")
|
|
||||||
|
|
||||||
# XXX: do this **after** cancellation/tearfown
|
|
||||||
# to avoid killing the process too early
|
|
||||||
# since trio does this internally on ``__aexit__()``
|
|
||||||
|
|
||||||
await do_hard_kill(proc)
|
|
||||||
|
|
||||||
|
|
||||||
async def new_proc(
|
|
||||||
name: str,
|
|
||||||
actor_nursery: 'ActorNursery', # type: ignore # noqa
|
|
||||||
subactor: Actor,
|
|
||||||
errors: Dict[Tuple[str, str], Exception],
|
|
||||||
# passed through to actor main
|
|
||||||
bind_addr: Tuple[str, int],
|
|
||||||
parent_addr: Tuple[str, int],
|
|
||||||
_runtime_vars: Dict[str, Any], # serialized and sent to _child
|
|
||||||
*,
|
|
||||||
task_status: TaskStatus[Portal] = trio.TASK_STATUS_IGNORED
|
|
||||||
) -> None:
|
|
||||||
"""Create a new ``multiprocessing.Process`` using the
|
|
||||||
spawn method as configured using ``try_set_start_method()``.
|
|
||||||
"""
|
|
||||||
cancel_scope = None
|
|
||||||
|
|
||||||
# mark the new actor with the global spawn method
|
|
||||||
subactor._spawn_method = _spawn_method
|
|
||||||
|
|
||||||
if _spawn_method == 'trio':
|
|
||||||
async with trio.open_nursery() as nursery:
|
|
||||||
async with spawn_subactor(
|
|
||||||
subactor,
|
|
||||||
parent_addr,
|
|
||||||
) as proc:
|
|
||||||
log.runtime(f"Started {proc}")
|
log.runtime(f"Started {proc}")
|
||||||
|
|
||||||
# wait for actor to spawn and connect back to us
|
# wait for actor to spawn and connect back to us
|
||||||
|
@ -274,6 +256,7 @@ async def new_proc(
|
||||||
with trio.CancelScope(shield=True):
|
with trio.CancelScope(shield=True):
|
||||||
await actor_nursery._join_procs.wait()
|
await actor_nursery._join_procs.wait()
|
||||||
|
|
||||||
|
async with trio.open_nursery() as nursery:
|
||||||
if portal in actor_nursery._cancel_after_result_on_exit:
|
if portal in actor_nursery._cancel_after_result_on_exit:
|
||||||
cancel_scope = await nursery.start(
|
cancel_scope = await nursery.start(
|
||||||
cancel_on_completion,
|
cancel_on_completion,
|
||||||
|
@ -285,25 +268,10 @@ async def new_proc(
|
||||||
# Wait for proc termination but **dont' yet** call
|
# Wait for proc termination but **dont' yet** call
|
||||||
# ``trio.Process.__aexit__()`` (it tears down stdio
|
# ``trio.Process.__aexit__()`` (it tears down stdio
|
||||||
# which will kill any waiting remote pdb trace).
|
# which will kill any waiting remote pdb trace).
|
||||||
|
# This is a "soft" (cancellable) join/reap.
|
||||||
# TODO: No idea how we can enforce zombie
|
|
||||||
# reaping more stringently without the shield
|
|
||||||
# we used to have below...
|
|
||||||
|
|
||||||
# with trio.CancelScope(shield=True):
|
|
||||||
# async with proc:
|
|
||||||
|
|
||||||
# Always "hard" join sub procs since no actor zombies
|
|
||||||
# are allowed!
|
|
||||||
|
|
||||||
# this is a "light" (cancellable) join, the hard join is
|
|
||||||
# in the enclosing scope (see above).
|
|
||||||
await proc.wait()
|
await proc.wait()
|
||||||
|
|
||||||
log.debug(f"Joined {proc}")
|
finally:
|
||||||
# pop child entry to indicate we no longer managing this subactor
|
|
||||||
subactor, proc, portal = actor_nursery._children.pop(subactor.uid)
|
|
||||||
|
|
||||||
# cancel result waiter that may have been spawned in
|
# cancel result waiter that may have been spawned in
|
||||||
# tandem if not done already
|
# tandem if not done already
|
||||||
if cancel_scope:
|
if cancel_scope:
|
||||||
|
@ -311,6 +279,19 @@ async def new_proc(
|
||||||
"Cancelling existing result waiter task for "
|
"Cancelling existing result waiter task for "
|
||||||
f"{subactor.uid}")
|
f"{subactor.uid}")
|
||||||
cancel_scope.cancel()
|
cancel_scope.cancel()
|
||||||
|
|
||||||
|
log.runtime(f"Attempting to kill {proc}")
|
||||||
|
|
||||||
|
# The "hard" reap since no actor zombies are allowed!
|
||||||
|
# XXX: do this **after** cancellation/tearfown to avoid
|
||||||
|
# killing the process too early.
|
||||||
|
with trio.CancelScope(shield=True):
|
||||||
|
await do_hard_kill(proc)
|
||||||
|
|
||||||
|
log.debug(f"Joined {proc}")
|
||||||
|
# pop child entry to indicate we no longer managing this subactor
|
||||||
|
subactor, proc, portal = actor_nursery._children.pop(subactor.uid)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# `multiprocessing`
|
# `multiprocessing`
|
||||||
# async with trio.open_nursery() as nursery:
|
# async with trio.open_nursery() as nursery:
|
||||||
|
|
Loading…
Reference in New Issue