2018-09-01 18:52:48 +00:00
|
|
|
"""
|
|
|
|
Cancellation and error propagation
|
2021-10-15 13:16:51 +00:00
|
|
|
|
2018-09-01 18:52:48 +00:00
|
|
|
"""
|
2020-07-21 04:23:14 +00:00
|
|
|
import os
|
|
|
|
import signal
|
2019-11-23 00:27:54 +00:00
|
|
|
import platform
|
2020-07-29 17:27:15 +00:00
|
|
|
import time
|
2018-09-01 18:52:48 +00:00
|
|
|
from itertools import repeat
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import trio
|
|
|
|
import tractor
|
|
|
|
|
2020-10-13 18:42:02 +00:00
|
|
|
from conftest import tractor_test, no_windows
|
2018-09-01 18:52:48 +00:00
|
|
|
|
|
|
|
|
2022-01-20 13:26:30 +00:00
|
|
|
def is_win():
|
|
|
|
return platform.system() == 'Windows'
|
|
|
|
|
|
|
|
|
2019-10-25 20:43:53 +00:00
|
|
|
async def assert_err(delay=0):
|
|
|
|
await trio.sleep(delay)
|
2018-09-01 18:52:48 +00:00
|
|
|
assert 0
|
|
|
|
|
|
|
|
|
2019-10-25 20:43:53 +00:00
|
|
|
async def sleep_forever():
|
2020-07-21 04:23:14 +00:00
|
|
|
await trio.sleep_forever()
|
2019-10-25 20:43:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def do_nuthin():
|
|
|
|
# just nick the scheduler
|
|
|
|
await trio.sleep(0)
|
|
|
|
|
|
|
|
|
2018-11-19 19:16:42 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'args_err',
|
|
|
|
[
|
|
|
|
# expected to be thrown in assert_err
|
|
|
|
({}, AssertionError),
|
|
|
|
# argument mismatch raised in _invoke()
|
|
|
|
({'unexpected': 10}, TypeError)
|
|
|
|
],
|
|
|
|
ids=['no_args', 'unexpected_args'],
|
|
|
|
)
|
|
|
|
def test_remote_error(arb_addr, args_err):
|
|
|
|
"""Verify an error raised in a subactor that is propagated
|
2018-11-22 16:43:04 +00:00
|
|
|
to the parent nursery, contains the underlying boxed builtin
|
|
|
|
error type info and causes cancellation and reraising all the
|
|
|
|
way up the stack.
|
2018-09-01 18:52:48 +00:00
|
|
|
"""
|
2018-11-19 19:16:42 +00:00
|
|
|
args, errtype = args_err
|
|
|
|
|
2018-09-01 18:52:48 +00:00
|
|
|
async def main():
|
2021-02-24 19:37:55 +00:00
|
|
|
async with tractor.open_nursery(
|
|
|
|
arbiter_addr=arb_addr,
|
|
|
|
) as nursery:
|
2018-09-01 18:52:48 +00:00
|
|
|
|
2021-04-28 15:55:37 +00:00
|
|
|
portal = await nursery.run_in_actor(
|
|
|
|
assert_err, name='errorer', **args
|
|
|
|
)
|
2018-09-01 18:52:48 +00:00
|
|
|
|
|
|
|
# get result(s) from main task
|
|
|
|
try:
|
2018-11-19 19:16:42 +00:00
|
|
|
await portal.result()
|
|
|
|
except tractor.RemoteActorError as err:
|
|
|
|
assert err.type == errtype
|
2018-09-01 18:52:48 +00:00
|
|
|
print("Look Maa that actor failed hard, hehh")
|
|
|
|
raise
|
|
|
|
|
2018-11-22 16:43:04 +00:00
|
|
|
with pytest.raises(tractor.RemoteActorError) as excinfo:
|
2021-02-24 19:37:55 +00:00
|
|
|
trio.run(main)
|
2018-09-01 18:52:48 +00:00
|
|
|
|
2018-11-22 16:43:04 +00:00
|
|
|
# ensure boxed error is correct
|
|
|
|
assert excinfo.value.type == errtype
|
|
|
|
|
2018-09-01 18:52:48 +00:00
|
|
|
|
2018-11-19 19:16:42 +00:00
|
|
|
def test_multierror(arb_addr):
|
|
|
|
"""Verify we raise a ``trio.MultiError`` out of a nursery where
|
|
|
|
more then one actor errors.
|
|
|
|
"""
|
|
|
|
async def main():
|
2021-02-24 19:37:55 +00:00
|
|
|
async with tractor.open_nursery(
|
|
|
|
arbiter_addr=arb_addr,
|
|
|
|
) as nursery:
|
2018-11-19 19:16:42 +00:00
|
|
|
|
2020-12-21 14:09:55 +00:00
|
|
|
await nursery.run_in_actor(assert_err, name='errorer1')
|
|
|
|
portal2 = await nursery.run_in_actor(assert_err, name='errorer2')
|
2018-11-19 19:16:42 +00:00
|
|
|
|
|
|
|
# get result(s) from main task
|
|
|
|
try:
|
|
|
|
await portal2.result()
|
|
|
|
except tractor.RemoteActorError as err:
|
|
|
|
assert err.type == AssertionError
|
|
|
|
print("Look Maa that first actor failed hard, hehh")
|
|
|
|
raise
|
|
|
|
|
|
|
|
# here we should get a `trio.MultiError` containing exceptions
|
|
|
|
# from both subactors
|
|
|
|
|
|
|
|
with pytest.raises(trio.MultiError):
|
2021-02-24 19:37:55 +00:00
|
|
|
trio.run(main)
|
2018-11-19 19:16:42 +00:00
|
|
|
|
|
|
|
|
2019-10-30 04:16:39 +00:00
|
|
|
@pytest.mark.parametrize('delay', (0, 0.5))
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'num_subactors', range(25, 26),
|
|
|
|
)
|
|
|
|
def test_multierror_fast_nursery(arb_addr, start_method, num_subactors, delay):
|
|
|
|
"""Verify we raise a ``trio.MultiError`` out of a nursery where
|
|
|
|
more then one actor errors and also with a delay before failure
|
|
|
|
to test failure during an ongoing spawning.
|
|
|
|
"""
|
|
|
|
async def main():
|
2021-02-24 19:37:55 +00:00
|
|
|
async with tractor.open_nursery(
|
|
|
|
arbiter_addr=arb_addr,
|
|
|
|
) as nursery:
|
|
|
|
|
2019-10-30 04:16:39 +00:00
|
|
|
for i in range(num_subactors):
|
|
|
|
await nursery.run_in_actor(
|
2020-12-21 14:09:55 +00:00
|
|
|
assert_err,
|
|
|
|
name=f'errorer{i}',
|
|
|
|
delay=delay
|
|
|
|
)
|
2019-10-30 04:16:39 +00:00
|
|
|
|
|
|
|
with pytest.raises(trio.MultiError) as exc_info:
|
2021-02-24 19:37:55 +00:00
|
|
|
trio.run(main)
|
2019-10-30 04:16:39 +00:00
|
|
|
|
|
|
|
assert exc_info.type == tractor.MultiError
|
|
|
|
err = exc_info.value
|
2021-07-02 15:55:16 +00:00
|
|
|
exceptions = err.exceptions
|
|
|
|
|
|
|
|
if len(exceptions) == 2:
|
|
|
|
# sometimes oddly now there's an embedded BrokenResourceError ?
|
2021-12-02 13:45:58 +00:00
|
|
|
for exc in exceptions:
|
|
|
|
excs = getattr(exc, 'exceptions', None)
|
|
|
|
if excs:
|
|
|
|
exceptions = excs
|
|
|
|
break
|
2021-07-02 15:55:16 +00:00
|
|
|
|
|
|
|
assert len(exceptions) == num_subactors
|
|
|
|
|
|
|
|
for exc in exceptions:
|
2019-10-30 04:16:39 +00:00
|
|
|
assert isinstance(exc, tractor.RemoteActorError)
|
|
|
|
assert exc.type == AssertionError
|
|
|
|
|
|
|
|
|
2021-04-27 13:14:08 +00:00
|
|
|
async def do_nothing():
|
2018-09-01 18:52:48 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-03-12 23:08:00 +00:00
|
|
|
@pytest.mark.parametrize('mechanism', ['nursery_cancel', KeyboardInterrupt])
|
|
|
|
def test_cancel_single_subactor(arb_addr, mechanism):
|
2018-11-19 19:16:42 +00:00
|
|
|
"""Ensure a ``ActorNursery.start_actor()`` spawned subactor
|
|
|
|
cancels when the nursery is cancelled.
|
|
|
|
"""
|
|
|
|
async def spawn_actor():
|
|
|
|
"""Spawn an actor that blocks indefinitely.
|
|
|
|
"""
|
2021-02-24 19:37:55 +00:00
|
|
|
async with tractor.open_nursery(
|
|
|
|
arbiter_addr=arb_addr,
|
|
|
|
) as nursery:
|
2018-09-01 18:52:48 +00:00
|
|
|
|
|
|
|
portal = await nursery.start_actor(
|
2021-02-24 19:37:55 +00:00
|
|
|
'nothin', enable_modules=[__name__],
|
2018-09-01 18:52:48 +00:00
|
|
|
)
|
2020-12-22 15:35:05 +00:00
|
|
|
assert (await portal.run(do_nothing)) is None
|
2018-09-01 18:52:48 +00:00
|
|
|
|
2020-03-12 23:08:00 +00:00
|
|
|
if mechanism == 'nursery_cancel':
|
|
|
|
# would hang otherwise
|
|
|
|
await nursery.cancel()
|
|
|
|
else:
|
|
|
|
raise mechanism
|
|
|
|
|
|
|
|
if mechanism == 'nursery_cancel':
|
2021-02-24 19:37:55 +00:00
|
|
|
trio.run(spawn_actor)
|
2020-03-12 23:08:00 +00:00
|
|
|
else:
|
|
|
|
with pytest.raises(mechanism):
|
2021-02-24 19:37:55 +00:00
|
|
|
trio.run(spawn_actor)
|
2018-09-01 18:52:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def stream_forever():
|
|
|
|
for i in repeat("I can see these little future bubble things"):
|
|
|
|
# each yielded value is sent over the ``Channel`` to the
|
|
|
|
# parent actor
|
|
|
|
yield i
|
|
|
|
await trio.sleep(0.01)
|
|
|
|
|
|
|
|
|
|
|
|
@tractor_test
|
2019-03-09 01:06:16 +00:00
|
|
|
async def test_cancel_infinite_streamer(start_method):
|
2018-09-01 18:52:48 +00:00
|
|
|
|
|
|
|
# stream for at most 1 seconds
|
|
|
|
with trio.move_on_after(1) as cancel_scope:
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
portal = await n.start_actor(
|
2020-07-21 04:23:14 +00:00
|
|
|
'donny',
|
2021-04-28 15:55:37 +00:00
|
|
|
enable_modules=[__name__],
|
2018-09-01 18:52:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# this async for loop streams values from the above
|
|
|
|
# async generator running in a separate process
|
2021-04-28 15:55:37 +00:00
|
|
|
async with portal.open_stream_from(stream_forever) as stream:
|
|
|
|
async for letter in stream:
|
|
|
|
print(letter)
|
2018-09-01 18:52:48 +00:00
|
|
|
|
|
|
|
# we support trio's cancellation system
|
|
|
|
assert cancel_scope.cancelled_caught
|
|
|
|
assert n.cancelled
|
|
|
|
|
|
|
|
|
2018-11-19 19:16:42 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'num_actors_and_errs',
|
|
|
|
[
|
2019-10-25 20:43:53 +00:00
|
|
|
# daemon actors sit idle while single task actors error out
|
|
|
|
(1, tractor.RemoteActorError, AssertionError, (assert_err, {}), None),
|
|
|
|
(2, tractor.MultiError, AssertionError, (assert_err, {}), None),
|
|
|
|
(3, tractor.MultiError, AssertionError, (assert_err, {}), None),
|
|
|
|
|
|
|
|
# 1 daemon actor errors out while single task actors sleep forever
|
|
|
|
(3, tractor.RemoteActorError, AssertionError, (sleep_forever, {}),
|
|
|
|
(assert_err, {}, True)),
|
|
|
|
# daemon actors error out after brief delay while single task
|
|
|
|
# actors complete quickly
|
|
|
|
(3, tractor.RemoteActorError, AssertionError,
|
|
|
|
(do_nuthin, {}), (assert_err, {'delay': 1}, True)),
|
2019-10-26 19:04:13 +00:00
|
|
|
# daemon complete quickly delay while single task
|
|
|
|
# actors error after brief delay
|
2019-10-25 20:43:53 +00:00
|
|
|
(3, tractor.MultiError, AssertionError,
|
|
|
|
(assert_err, {'delay': 1}), (do_nuthin, {}, False)),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
'1_run_in_actor_fails',
|
|
|
|
'2_run_in_actors_fail',
|
|
|
|
'3_run_in_actors_fail',
|
|
|
|
'1_daemon_actors_fail',
|
|
|
|
'1_daemon_actors_fail_all_run_in_actors_dun_quick',
|
|
|
|
'no_daemon_actors_fail_all_run_in_actors_sleep_then_fail',
|
2018-11-19 19:16:42 +00:00
|
|
|
],
|
|
|
|
)
|
2018-09-01 18:52:48 +00:00
|
|
|
@tractor_test
|
2020-07-20 23:51:07 +00:00
|
|
|
async def test_some_cancels_all(num_actors_and_errs, start_method, loglevel):
|
2018-11-22 16:43:04 +00:00
|
|
|
"""Verify a subset of failed subactors causes all others in
|
|
|
|
the nursery to be cancelled just like the strategy in trio.
|
2018-09-01 18:52:48 +00:00
|
|
|
|
|
|
|
This is the first and only supervisory strategy at the moment.
|
|
|
|
"""
|
2019-10-25 20:43:53 +00:00
|
|
|
num_actors, first_err, err_type, ria_func, da_func = num_actors_and_errs
|
2018-09-01 18:52:48 +00:00
|
|
|
try:
|
|
|
|
async with tractor.open_nursery() as n:
|
2019-10-25 20:43:53 +00:00
|
|
|
|
|
|
|
# spawn the same number of deamon actors which should be cancelled
|
|
|
|
dactor_portals = []
|
|
|
|
for i in range(num_actors):
|
|
|
|
dactor_portals.append(await n.start_actor(
|
|
|
|
f'deamon_{i}',
|
2021-02-24 19:37:55 +00:00
|
|
|
enable_modules=[__name__],
|
2018-09-01 18:52:48 +00:00
|
|
|
))
|
|
|
|
|
2019-10-25 20:43:53 +00:00
|
|
|
func, kwargs = ria_func
|
|
|
|
riactor_portals = []
|
|
|
|
for i in range(num_actors):
|
2018-11-22 16:43:04 +00:00
|
|
|
# start actor(s) that will fail immediately
|
2019-10-25 20:43:53 +00:00
|
|
|
riactor_portals.append(
|
2020-12-21 14:09:55 +00:00
|
|
|
await n.run_in_actor(
|
|
|
|
func,
|
|
|
|
name=f'actor_{i}',
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
)
|
2019-10-25 20:43:53 +00:00
|
|
|
|
|
|
|
if da_func:
|
|
|
|
func, kwargs, expect_error = da_func
|
|
|
|
for portal in dactor_portals:
|
|
|
|
# if this function fails then we should error here
|
|
|
|
# and the nursery should teardown all other actors
|
|
|
|
try:
|
2020-12-22 15:35:05 +00:00
|
|
|
await portal.run(func, **kwargs)
|
|
|
|
|
2019-10-25 20:43:53 +00:00
|
|
|
except tractor.RemoteActorError as err:
|
|
|
|
assert err.type == err_type
|
|
|
|
# we only expect this first error to propogate
|
|
|
|
# (all other daemons are cancelled before they
|
|
|
|
# can be scheduled)
|
|
|
|
num_actors = 1
|
|
|
|
# reraise so nursery teardown is triggered
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if expect_error:
|
|
|
|
pytest.fail(
|
|
|
|
"Deamon call should fail at checkpoint?")
|
2018-11-19 19:16:42 +00:00
|
|
|
|
2018-11-22 16:43:04 +00:00
|
|
|
# should error here with a ``RemoteActorError`` or ``MultiError``
|
2018-09-01 18:52:48 +00:00
|
|
|
|
2018-11-19 19:16:42 +00:00
|
|
|
except first_err as err:
|
2018-11-19 21:53:21 +00:00
|
|
|
if isinstance(err, tractor.MultiError):
|
2019-10-25 20:43:53 +00:00
|
|
|
assert len(err.exceptions) == num_actors
|
2018-11-19 19:16:42 +00:00
|
|
|
for exc in err.exceptions:
|
2018-11-19 21:53:21 +00:00
|
|
|
if isinstance(exc, tractor.RemoteActorError):
|
|
|
|
assert exc.type == err_type
|
|
|
|
else:
|
|
|
|
assert isinstance(exc, trio.Cancelled)
|
|
|
|
elif isinstance(err, tractor.RemoteActorError):
|
2018-11-19 19:16:42 +00:00
|
|
|
assert err.type == err_type
|
2018-09-01 18:52:48 +00:00
|
|
|
|
|
|
|
assert n.cancelled is True
|
|
|
|
assert not n._children
|
|
|
|
else:
|
|
|
|
pytest.fail("Should have gotten a remote assertion error?")
|
2019-10-26 19:04:13 +00:00
|
|
|
|
|
|
|
|
2019-11-25 00:22:01 +00:00
|
|
|
async def spawn_and_error(breadth, depth) -> None:
|
2019-10-26 19:04:13 +00:00
|
|
|
name = tractor.current_actor().name
|
2019-10-30 04:16:39 +00:00
|
|
|
async with tractor.open_nursery() as nursery:
|
2019-11-25 00:22:01 +00:00
|
|
|
for i in range(breadth):
|
2020-12-21 14:09:55 +00:00
|
|
|
|
2019-11-25 00:22:01 +00:00
|
|
|
if depth > 0:
|
2020-12-21 14:09:55 +00:00
|
|
|
|
2019-11-25 00:22:01 +00:00
|
|
|
args = (
|
|
|
|
spawn_and_error,
|
|
|
|
)
|
|
|
|
kwargs = {
|
2020-12-21 14:09:55 +00:00
|
|
|
'name': f'spawner_{i}_depth_{depth}',
|
2019-11-25 00:22:01 +00:00
|
|
|
'breadth': breadth,
|
|
|
|
'depth': depth - 1,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
args = (
|
|
|
|
assert_err,
|
|
|
|
)
|
2020-12-21 14:09:55 +00:00
|
|
|
kwargs = {
|
|
|
|
'name': f'{name}_errorer_{i}',
|
|
|
|
}
|
2019-11-25 00:22:01 +00:00
|
|
|
await nursery.run_in_actor(*args, **kwargs)
|
2019-10-26 19:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@tractor_test
|
2020-01-27 02:36:08 +00:00
|
|
|
async def test_nested_multierrors(loglevel, start_method):
|
2022-01-20 13:26:30 +00:00
|
|
|
'''
|
|
|
|
Test that failed actor sets are wrapped in `trio.MultiError`s. This
|
|
|
|
test goes only 2 nurseries deep but we should eventually have tests
|
2019-10-30 04:16:39 +00:00
|
|
|
for arbitrary n-depth actor trees.
|
2022-01-20 13:26:30 +00:00
|
|
|
|
|
|
|
'''
|
2020-07-25 22:18:34 +00:00
|
|
|
if start_method == 'trio':
|
2020-01-27 02:36:08 +00:00
|
|
|
depth = 3
|
|
|
|
subactor_breadth = 2
|
|
|
|
else:
|
|
|
|
# XXX: multiprocessing can't seem to handle any more then 2 depth
|
|
|
|
# process trees for whatever reason.
|
|
|
|
# Any more process levels then this and we see bugs that cause
|
|
|
|
# hangs and broken pipes all over the place...
|
2020-01-27 04:17:06 +00:00
|
|
|
if start_method == 'forkserver':
|
|
|
|
pytest.skip("Forksever sux hard at nested spawning...")
|
2020-07-26 01:20:34 +00:00
|
|
|
depth = 1 # means an additional actor tree of spawning (2 levels deep)
|
2020-01-27 02:36:08 +00:00
|
|
|
subactor_breadth = 2
|
2019-11-25 00:22:01 +00:00
|
|
|
|
|
|
|
with trio.fail_after(120):
|
|
|
|
try:
|
|
|
|
async with tractor.open_nursery() as nursery:
|
|
|
|
for i in range(subactor_breadth):
|
|
|
|
await nursery.run_in_actor(
|
|
|
|
spawn_and_error,
|
2020-12-21 14:09:55 +00:00
|
|
|
name=f'spawner_{i}',
|
2019-11-25 00:22:01 +00:00
|
|
|
breadth=subactor_breadth,
|
|
|
|
depth=depth,
|
|
|
|
)
|
|
|
|
except trio.MultiError as err:
|
|
|
|
assert len(err.exceptions) == subactor_breadth
|
|
|
|
for subexc in err.exceptions:
|
|
|
|
|
2020-07-26 01:20:34 +00:00
|
|
|
# verify first level actor errors are wrapped as remote
|
2022-01-20 13:26:30 +00:00
|
|
|
if is_win():
|
2020-07-26 01:20:34 +00:00
|
|
|
|
|
|
|
# windows is often too slow and cancellation seems
|
|
|
|
# to happen before an actor is spawned
|
2020-07-27 15:14:21 +00:00
|
|
|
if isinstance(subexc, trio.Cancelled):
|
2020-07-26 01:20:34 +00:00
|
|
|
continue
|
2021-10-14 16:12:13 +00:00
|
|
|
|
|
|
|
elif isinstance(subexc, tractor.RemoteActorError):
|
2020-07-27 15:14:21 +00:00
|
|
|
# on windows it seems we can't exactly be sure wtf
|
|
|
|
# will happen..
|
|
|
|
assert subexc.type in (
|
|
|
|
tractor.RemoteActorError,
|
|
|
|
trio.Cancelled,
|
|
|
|
trio.MultiError
|
|
|
|
)
|
2021-10-14 16:12:13 +00:00
|
|
|
|
|
|
|
elif isinstance(subexc, trio.MultiError):
|
|
|
|
for subsub in subexc.exceptions:
|
2021-10-15 13:16:51 +00:00
|
|
|
|
|
|
|
if subsub in (tractor.RemoteActorError,):
|
|
|
|
subsub = subsub.type
|
|
|
|
|
2021-10-15 14:07:45 +00:00
|
|
|
assert type(subsub) in (
|
2021-10-14 16:12:13 +00:00
|
|
|
trio.Cancelled,
|
2021-10-15 13:16:51 +00:00
|
|
|
trio.MultiError,
|
2021-10-14 16:12:13 +00:00
|
|
|
)
|
2020-07-26 01:20:34 +00:00
|
|
|
else:
|
|
|
|
assert isinstance(subexc, tractor.RemoteActorError)
|
|
|
|
|
|
|
|
if depth > 0 and subactor_breadth > 1:
|
2019-11-25 00:22:01 +00:00
|
|
|
# XXX not sure what's up with this..
|
2020-07-26 01:20:34 +00:00
|
|
|
# on windows sometimes spawning is just too slow and
|
|
|
|
# we get back the (sent) cancel signal instead
|
2022-01-20 13:26:30 +00:00
|
|
|
if is_win():
|
2021-10-15 14:07:45 +00:00
|
|
|
if isinstance(subexc, tractor.RemoteActorError):
|
2022-01-20 13:26:30 +00:00
|
|
|
assert subexc.type in (
|
|
|
|
trio.MultiError,
|
|
|
|
tractor.RemoteActorError
|
|
|
|
)
|
2021-10-15 14:07:45 +00:00
|
|
|
else:
|
|
|
|
assert isinstance(subexc, trio.MultiError)
|
2019-11-25 00:22:01 +00:00
|
|
|
else:
|
|
|
|
assert subexc.type is trio.MultiError
|
|
|
|
else:
|
2022-01-20 13:26:30 +00:00
|
|
|
assert subexc.type in (
|
|
|
|
tractor.RemoteActorError,
|
|
|
|
trio.Cancelled
|
|
|
|
)
|
2020-07-21 04:23:14 +00:00
|
|
|
|
|
|
|
|
2020-07-25 16:00:04 +00:00
|
|
|
@no_windows
|
2020-07-29 17:27:15 +00:00
|
|
|
def test_cancel_via_SIGINT(
|
|
|
|
loglevel,
|
|
|
|
start_method,
|
|
|
|
spawn_backend,
|
|
|
|
):
|
2020-07-21 04:23:14 +00:00
|
|
|
"""Ensure that a control-C (SIGINT) signal cancels both the parent and
|
|
|
|
child processes in trionic fashion
|
|
|
|
"""
|
|
|
|
pid = os.getpid()
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
with trio.fail_after(2):
|
|
|
|
async with tractor.open_nursery() as tn:
|
|
|
|
await tn.start_actor('sucka')
|
2022-10-09 21:54:55 +00:00
|
|
|
if 'mp' in spawn_backend:
|
2020-07-29 17:27:15 +00:00
|
|
|
time.sleep(0.1)
|
2020-07-21 04:23:14 +00:00
|
|
|
os.kill(pid, signal.SIGINT)
|
|
|
|
await trio.sleep_forever()
|
|
|
|
|
|
|
|
with pytest.raises(KeyboardInterrupt):
|
2021-02-24 19:37:55 +00:00
|
|
|
trio.run(main)
|
2020-07-21 04:23:14 +00:00
|
|
|
|
|
|
|
|
2020-07-25 16:00:04 +00:00
|
|
|
@no_windows
|
2020-07-27 03:37:44 +00:00
|
|
|
def test_cancel_via_SIGINT_other_task(
|
2020-07-21 04:23:14 +00:00
|
|
|
loglevel,
|
2020-07-29 17:27:15 +00:00
|
|
|
start_method,
|
|
|
|
spawn_backend,
|
2020-07-21 04:23:14 +00:00
|
|
|
):
|
|
|
|
"""Ensure that a control-C (SIGINT) signal cancels both the parent
|
|
|
|
and child processes in trionic fashion even a subprocess is started
|
|
|
|
from a seperate ``trio`` child task.
|
|
|
|
"""
|
|
|
|
pid = os.getpid()
|
2022-01-20 13:26:30 +00:00
|
|
|
timeout: float = 2
|
|
|
|
if is_win(): # smh
|
|
|
|
timeout += 1
|
2020-07-21 04:23:14 +00:00
|
|
|
|
|
|
|
async def spawn_and_sleep_forever(task_status=trio.TASK_STATUS_IGNORED):
|
|
|
|
async with tractor.open_nursery() as tn:
|
|
|
|
for i in range(3):
|
2020-12-21 14:09:55 +00:00
|
|
|
await tn.run_in_actor(
|
|
|
|
sleep_forever,
|
|
|
|
name='namesucka',
|
|
|
|
)
|
2020-07-21 04:23:14 +00:00
|
|
|
task_status.started()
|
|
|
|
await trio.sleep_forever()
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
# should never timeout since SIGINT should cancel the current program
|
2022-01-20 13:26:30 +00:00
|
|
|
with trio.fail_after(timeout):
|
2020-07-21 04:23:14 +00:00
|
|
|
async with trio.open_nursery() as n:
|
|
|
|
await n.start(spawn_and_sleep_forever)
|
2022-10-09 21:54:55 +00:00
|
|
|
if 'mp' in spawn_backend:
|
2020-07-29 17:27:15 +00:00
|
|
|
time.sleep(0.1)
|
2020-07-21 04:23:14 +00:00
|
|
|
os.kill(pid, signal.SIGINT)
|
|
|
|
|
|
|
|
with pytest.raises(KeyboardInterrupt):
|
2021-02-24 19:37:55 +00:00
|
|
|
trio.run(main)
|
2020-10-12 12:56:49 +00:00
|
|
|
|
2021-10-14 16:12:13 +00:00
|
|
|
|
2020-10-12 12:56:49 +00:00
|
|
|
async def spin_for(period=3):
|
|
|
|
"Sync sleep."
|
|
|
|
time.sleep(period)
|
|
|
|
|
|
|
|
|
|
|
|
async def spawn():
|
|
|
|
async with tractor.open_nursery() as tn:
|
2021-04-28 15:55:37 +00:00
|
|
|
await tn.run_in_actor(
|
2020-12-21 14:09:55 +00:00
|
|
|
spin_for,
|
|
|
|
name='sleeper',
|
|
|
|
)
|
2020-10-12 12:56:49 +00:00
|
|
|
|
|
|
|
|
2020-10-13 19:26:14 +00:00
|
|
|
@no_windows
|
2020-10-12 12:56:49 +00:00
|
|
|
def test_cancel_while_childs_child_in_sync_sleep(
|
|
|
|
loglevel,
|
|
|
|
start_method,
|
|
|
|
spawn_backend,
|
|
|
|
):
|
|
|
|
"""Verify that a child cancelled while executing sync code is torn
|
|
|
|
down even when that cancellation is triggered by the parent
|
|
|
|
2 nurseries "up".
|
|
|
|
"""
|
2020-10-13 18:16:20 +00:00
|
|
|
if start_method == 'forkserver':
|
|
|
|
pytest.skip("Forksever sux hard at resuming from sync sleep...")
|
|
|
|
|
2020-10-12 12:56:49 +00:00
|
|
|
async def main():
|
|
|
|
with trio.fail_after(2):
|
|
|
|
async with tractor.open_nursery() as tn:
|
2021-04-28 15:55:37 +00:00
|
|
|
await tn.run_in_actor(
|
2020-12-21 14:09:55 +00:00
|
|
|
spawn,
|
|
|
|
name='spawn',
|
|
|
|
)
|
2020-10-12 12:56:49 +00:00
|
|
|
await trio.sleep(1)
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError):
|
2021-02-24 19:37:55 +00:00
|
|
|
trio.run(main)
|
2021-11-29 17:41:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_fast_graceful_cancel_when_spawn_task_in_soft_proc_wait_for_daemon(
|
|
|
|
start_method,
|
|
|
|
):
|
|
|
|
'''
|
|
|
|
This is a very subtle test which demonstrates how cancellation
|
|
|
|
during process collection can result in non-optimal teardown
|
|
|
|
performance on daemon actors. The fix for this test was to handle
|
|
|
|
``trio.Cancelled`` specially in the spawn task waiting in
|
|
|
|
`proc.wait()` such that ``Portal.cancel_actor()`` is called before
|
|
|
|
executing the "hard reap" sequence (which has an up to 3 second
|
|
|
|
delay currently).
|
|
|
|
|
|
|
|
In other words, if we can cancel the actor using a graceful remote
|
|
|
|
cancellation, and it's faster, we might as well do it.
|
|
|
|
|
|
|
|
'''
|
2021-12-02 13:12:46 +00:00
|
|
|
kbi_delay = 0.5
|
2022-01-20 13:26:30 +00:00
|
|
|
timeout: float = 2.9
|
|
|
|
|
|
|
|
if is_win(): # smh
|
|
|
|
timeout += 1
|
2021-11-29 17:41:40 +00:00
|
|
|
|
|
|
|
async def main():
|
|
|
|
start = time.time()
|
|
|
|
try:
|
|
|
|
async with trio.open_nursery() as nurse:
|
|
|
|
async with tractor.open_nursery() as tn:
|
|
|
|
p = await tn.start_actor(
|
|
|
|
'fast_boi',
|
|
|
|
enable_modules=[__name__],
|
|
|
|
)
|
|
|
|
|
|
|
|
async def delayed_kbi():
|
|
|
|
await trio.sleep(kbi_delay)
|
|
|
|
print(f'RAISING KBI after {kbi_delay} s')
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
# start task which raises a kbi **after**
|
|
|
|
# the actor nursery ``__aexit__()`` has
|
|
|
|
# been run.
|
|
|
|
nurse.start_soon(delayed_kbi)
|
|
|
|
|
|
|
|
await p.run(do_nuthin)
|
|
|
|
finally:
|
|
|
|
duration = time.time() - start
|
2022-01-20 13:26:30 +00:00
|
|
|
if duration > timeout:
|
2021-11-29 17:41:40 +00:00
|
|
|
raise trio.TooSlowError(
|
|
|
|
'daemon cancel was slower then necessary..'
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(KeyboardInterrupt):
|
|
|
|
trio.run(main)
|