2021-11-05 14:42:43 +00:00
|
|
|
'''
|
2021-11-24 17:13:02 +00:00
|
|
|
The hipster way to force SC onto the stdlib's "async": 'infection mode'.
|
2021-11-05 14:42:43 +00:00
|
|
|
|
|
|
|
'''
|
2020-12-10 18:49:11 +00:00
|
|
|
import asyncio
|
2021-11-07 22:05:40 +00:00
|
|
|
import builtins
|
2024-06-24 17:52:19 +00:00
|
|
|
from contextlib import ExitStack
|
2022-04-12 18:24:30 +00:00
|
|
|
import itertools
|
2021-11-07 22:05:40 +00:00
|
|
|
import importlib
|
2024-06-24 17:52:19 +00:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
import signal
|
|
|
|
from typing import (
|
|
|
|
Callable,
|
|
|
|
Iterable,
|
|
|
|
Union,
|
|
|
|
)
|
2020-12-10 18:49:11 +00:00
|
|
|
|
|
|
|
import pytest
|
2021-11-07 22:05:40 +00:00
|
|
|
import trio
|
2020-12-10 18:49:11 +00:00
|
|
|
import tractor
|
2022-07-14 20:09:05 +00:00
|
|
|
from tractor import (
|
2024-06-24 17:52:19 +00:00
|
|
|
current_actor,
|
2022-07-14 20:09:05 +00:00
|
|
|
to_asyncio,
|
|
|
|
RemoteActorError,
|
2023-04-13 22:06:44 +00:00
|
|
|
ContextCancelled,
|
2022-07-14 20:09:05 +00:00
|
|
|
)
|
2022-04-12 18:24:30 +00:00
|
|
|
from tractor.trionics import BroadcastReceiver
|
2024-03-12 19:48:20 +00:00
|
|
|
from tractor._testing import expect_ctxc
|
2024-03-08 02:33:07 +00:00
|
|
|
|
2021-11-05 14:42:43 +00:00
|
|
|
|
2022-07-14 20:09:05 +00:00
|
|
|
async def sleep_and_err(
|
|
|
|
sleep_for: float = 0.1,
|
|
|
|
|
|
|
|
# just signature placeholders for compat with
|
|
|
|
# ``to_asyncio.open_channel_from()``
|
2024-06-24 17:52:19 +00:00
|
|
|
to_trio: trio.MemorySendChannel|None = None,
|
|
|
|
from_trio: asyncio.Queue|None = None,
|
2022-07-14 20:09:05 +00:00
|
|
|
|
|
|
|
):
|
|
|
|
if to_trio:
|
|
|
|
to_trio.send_nowait('start')
|
|
|
|
|
2022-02-24 18:27:47 +00:00
|
|
|
await asyncio.sleep(sleep_for)
|
2020-12-10 18:49:11 +00:00
|
|
|
assert 0
|
|
|
|
|
|
|
|
|
2024-06-24 17:52:19 +00:00
|
|
|
async def aio_sleep_forever():
|
2021-11-07 22:05:40 +00:00
|
|
|
await asyncio.sleep(float('inf'))
|
|
|
|
|
|
|
|
|
2021-11-18 19:01:13 +00:00
|
|
|
async def trio_cancels_single_aio_task():
|
|
|
|
|
|
|
|
# spawn an ``asyncio`` task to run a func and return result
|
|
|
|
with trio.move_on_after(.2):
|
2024-06-24 17:52:19 +00:00
|
|
|
await tractor.to_asyncio.run_task(aio_sleep_forever)
|
2021-11-18 19:01:13 +00:00
|
|
|
|
|
|
|
|
2023-10-18 19:39:20 +00:00
|
|
|
def test_trio_cancels_aio_on_actor_side(reg_addr):
|
2021-11-18 19:01:13 +00:00
|
|
|
'''
|
|
|
|
Spawn an infected actor that is cancelled by the ``trio`` side
|
|
|
|
task using std cancel scope apis.
|
|
|
|
|
|
|
|
'''
|
|
|
|
async def main():
|
|
|
|
async with tractor.open_nursery(
|
2023-10-18 19:39:20 +00:00
|
|
|
registry_addrs=[reg_addr]
|
2021-11-18 19:01:13 +00:00
|
|
|
) as n:
|
|
|
|
await n.run_in_actor(
|
|
|
|
trio_cancels_single_aio_task,
|
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
trio.run(main)
|
|
|
|
|
|
|
|
|
2021-11-07 22:05:40 +00:00
|
|
|
async def asyncio_actor(
|
|
|
|
target: str,
|
2024-03-11 14:37:34 +00:00
|
|
|
expect_err: Exception|None = None
|
2021-11-07 22:05:40 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
2020-12-10 18:49:11 +00:00
|
|
|
assert tractor.current_actor().is_infected_aio()
|
2024-06-24 17:52:19 +00:00
|
|
|
target: Callable = globals()[target]
|
2020-12-10 18:49:11 +00:00
|
|
|
|
2021-11-07 22:05:40 +00:00
|
|
|
if '.' in expect_err:
|
|
|
|
modpath, _, name = expect_err.rpartition('.')
|
|
|
|
mod = importlib.import_module(modpath)
|
2021-11-17 18:32:42 +00:00
|
|
|
error_type = getattr(mod, name)
|
|
|
|
|
|
|
|
else: # toplevel builtin error type
|
|
|
|
error_type = builtins.__dict__.get(expect_err)
|
2021-11-07 22:05:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# spawn an ``asyncio`` task to run a func and return result
|
|
|
|
await tractor.to_asyncio.run_task(target)
|
2021-11-17 18:32:42 +00:00
|
|
|
|
|
|
|
except BaseException as err:
|
2021-11-07 22:05:40 +00:00
|
|
|
if expect_err:
|
2021-11-17 18:32:42 +00:00
|
|
|
assert isinstance(err, error_type)
|
2021-11-07 22:05:40 +00:00
|
|
|
|
2021-11-24 17:13:02 +00:00
|
|
|
raise
|
2020-12-10 18:49:11 +00:00
|
|
|
|
|
|
|
|
2023-10-18 19:39:20 +00:00
|
|
|
def test_aio_simple_error(reg_addr):
|
2021-11-07 22:05:40 +00:00
|
|
|
'''
|
|
|
|
Verify a simple remote asyncio error propagates back through trio
|
|
|
|
to the parent actor.
|
|
|
|
|
2020-12-10 18:49:11 +00:00
|
|
|
|
2021-11-07 22:05:40 +00:00
|
|
|
'''
|
2020-12-10 18:49:11 +00:00
|
|
|
async def main():
|
2021-11-07 22:05:40 +00:00
|
|
|
async with tractor.open_nursery(
|
2023-10-18 19:39:20 +00:00
|
|
|
registry_addrs=[reg_addr]
|
2021-11-07 22:05:40 +00:00
|
|
|
) as n:
|
|
|
|
await n.run_in_actor(
|
|
|
|
asyncio_actor,
|
|
|
|
target='sleep_and_err',
|
|
|
|
expect_err='AssertionError',
|
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
2020-12-10 18:49:11 +00:00
|
|
|
|
2024-03-11 14:37:34 +00:00
|
|
|
with pytest.raises(
|
|
|
|
expected_exception=(RemoteActorError, ExceptionGroup),
|
|
|
|
) as excinfo:
|
2021-11-07 22:05:40 +00:00
|
|
|
trio.run(main)
|
2021-11-05 14:42:43 +00:00
|
|
|
|
2021-11-07 22:05:40 +00:00
|
|
|
err = excinfo.value
|
2024-03-11 14:37:34 +00:00
|
|
|
|
|
|
|
# might get multiple `trio.Cancelled`s as well inside an inception
|
|
|
|
if isinstance(err, ExceptionGroup):
|
|
|
|
err = next(itertools.dropwhile(
|
|
|
|
lambda exc: not isinstance(exc, tractor.RemoteActorError),
|
|
|
|
err.exceptions
|
|
|
|
))
|
|
|
|
assert err
|
|
|
|
|
2021-11-07 22:05:40 +00:00
|
|
|
assert isinstance(err, RemoteActorError)
|
2024-03-19 22:08:54 +00:00
|
|
|
assert err.boxed_type == AssertionError
|
2021-11-05 14:42:43 +00:00
|
|
|
|
2021-11-07 22:05:40 +00:00
|
|
|
|
2023-10-18 19:39:20 +00:00
|
|
|
def test_tractor_cancels_aio(reg_addr):
|
2021-11-07 22:05:40 +00:00
|
|
|
'''
|
|
|
|
Verify we can cancel a spawned asyncio task gracefully.
|
|
|
|
|
|
|
|
'''
|
|
|
|
async def main():
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
portal = await n.run_in_actor(
|
|
|
|
asyncio_actor,
|
2024-06-24 17:52:19 +00:00
|
|
|
target='aio_sleep_forever',
|
2021-11-17 18:32:42 +00:00
|
|
|
expect_err='trio.Cancelled',
|
2021-11-07 22:05:40 +00:00
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
2021-11-17 18:32:42 +00:00
|
|
|
# cancel the entire remote runtime
|
2021-11-07 22:05:40 +00:00
|
|
|
await portal.cancel_actor()
|
|
|
|
|
|
|
|
trio.run(main)
|
2021-11-05 14:42:43 +00:00
|
|
|
|
|
|
|
|
2023-10-18 19:39:20 +00:00
|
|
|
def test_trio_cancels_aio(reg_addr):
|
2021-11-18 14:35:59 +00:00
|
|
|
'''
|
|
|
|
Much like the above test with ``tractor.Portal.cancel_actor()``
|
|
|
|
except we just use a standard ``trio`` cancellation api.
|
|
|
|
|
|
|
|
'''
|
|
|
|
async def main():
|
|
|
|
|
|
|
|
with trio.move_on_after(1):
|
|
|
|
# cancel the nursery shortly after boot
|
|
|
|
|
|
|
|
async with tractor.open_nursery() as n:
|
2021-11-24 17:13:02 +00:00
|
|
|
await n.run_in_actor(
|
2021-11-18 14:35:59 +00:00
|
|
|
asyncio_actor,
|
2024-06-24 17:52:19 +00:00
|
|
|
target='aio_sleep_forever',
|
2021-11-18 14:35:59 +00:00
|
|
|
expect_err='trio.Cancelled',
|
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
trio.run(main)
|
|
|
|
|
|
|
|
|
2022-07-14 20:09:05 +00:00
|
|
|
@tractor.context
|
|
|
|
async def trio_ctx(
|
|
|
|
ctx: tractor.Context,
|
|
|
|
):
|
|
|
|
|
|
|
|
await ctx.started('start')
|
|
|
|
|
|
|
|
# this will block until the ``asyncio`` task sends a "first"
|
|
|
|
# message.
|
2022-07-15 00:44:10 +00:00
|
|
|
with trio.fail_after(2):
|
2022-07-14 20:09:05 +00:00
|
|
|
async with (
|
2022-10-11 17:48:27 +00:00
|
|
|
trio.open_nursery() as n,
|
|
|
|
|
2022-07-14 20:09:05 +00:00
|
|
|
tractor.to_asyncio.open_channel_from(
|
|
|
|
sleep_and_err,
|
|
|
|
) as (first, chan),
|
|
|
|
):
|
|
|
|
|
|
|
|
assert first == 'start'
|
|
|
|
|
|
|
|
# spawn another asyncio task for the cuck of it.
|
|
|
|
n.start_soon(
|
|
|
|
tractor.to_asyncio.run_task,
|
2024-06-24 17:52:19 +00:00
|
|
|
aio_sleep_forever,
|
2022-07-14 20:09:05 +00:00
|
|
|
)
|
2022-07-14 20:35:41 +00:00
|
|
|
await trio.sleep_forever()
|
2022-07-14 20:09:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2024-03-08 02:33:07 +00:00
|
|
|
'parent_cancels',
|
|
|
|
['context', 'actor', False],
|
2022-07-14 20:09:05 +00:00
|
|
|
ids='parent_actor_cancels_child={}'.format
|
|
|
|
)
|
|
|
|
def test_context_spawns_aio_task_that_errors(
|
2023-10-18 19:39:20 +00:00
|
|
|
reg_addr,
|
2022-07-14 20:09:05 +00:00
|
|
|
parent_cancels: bool,
|
|
|
|
):
|
|
|
|
'''
|
|
|
|
Verify that spawning a task via an intertask channel ctx mngr that
|
|
|
|
errors correctly propagates the error back from the `asyncio`-side
|
2022-07-15 15:18:48 +00:00
|
|
|
task.
|
2022-07-14 20:09:05 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
async def main():
|
|
|
|
|
2022-10-11 17:35:55 +00:00
|
|
|
with trio.fail_after(2):
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
p = await n.start_actor(
|
|
|
|
'aio_daemon',
|
|
|
|
enable_modules=[__name__],
|
|
|
|
infect_asyncio=True,
|
|
|
|
# debug_mode=True,
|
|
|
|
loglevel='cancel',
|
|
|
|
)
|
2024-03-08 02:33:07 +00:00
|
|
|
async with (
|
|
|
|
expect_ctxc(
|
|
|
|
yay=parent_cancels == 'actor',
|
|
|
|
),
|
|
|
|
p.open_context(
|
|
|
|
trio_ctx,
|
|
|
|
) as (ctx, first),
|
|
|
|
):
|
2022-07-14 20:09:05 +00:00
|
|
|
|
2022-10-11 17:35:55 +00:00
|
|
|
assert first == 'start'
|
2022-07-14 20:09:05 +00:00
|
|
|
|
2024-03-08 02:33:07 +00:00
|
|
|
if parent_cancels == 'actor':
|
2022-10-11 17:35:55 +00:00
|
|
|
await p.cancel_actor()
|
2022-07-14 20:09:05 +00:00
|
|
|
|
2024-03-08 02:33:07 +00:00
|
|
|
elif parent_cancels == 'context':
|
|
|
|
await ctx.cancel()
|
|
|
|
|
|
|
|
else:
|
|
|
|
await trio.sleep_forever()
|
|
|
|
|
|
|
|
async with expect_ctxc(
|
|
|
|
yay=parent_cancels == 'actor',
|
|
|
|
):
|
|
|
|
await ctx.result()
|
|
|
|
|
|
|
|
if parent_cancels == 'context':
|
|
|
|
# to tear down sub-acor
|
|
|
|
await p.cancel_actor()
|
2022-07-14 20:09:05 +00:00
|
|
|
|
2024-03-08 02:33:07 +00:00
|
|
|
return ctx.outcome
|
2022-07-14 20:09:05 +00:00
|
|
|
|
|
|
|
if parent_cancels:
|
2023-04-13 22:06:44 +00:00
|
|
|
# bc the parent made the cancel request,
|
|
|
|
# the error is not raised locally but instead
|
|
|
|
# the context is exited silently
|
|
|
|
res = trio.run(main)
|
|
|
|
assert isinstance(res, ContextCancelled)
|
|
|
|
assert 'root' in res.canceller[0]
|
|
|
|
|
2022-07-14 20:09:05 +00:00
|
|
|
else:
|
2023-04-13 22:06:44 +00:00
|
|
|
expect = RemoteActorError
|
|
|
|
with pytest.raises(expect) as excinfo:
|
|
|
|
trio.run(main)
|
|
|
|
|
|
|
|
err = excinfo.value
|
|
|
|
assert isinstance(err, expect)
|
2024-03-19 22:08:54 +00:00
|
|
|
assert err.boxed_type == AssertionError
|
2022-07-14 20:09:05 +00:00
|
|
|
|
|
|
|
|
2021-11-17 18:32:42 +00:00
|
|
|
async def aio_cancel():
|
2021-11-24 17:13:02 +00:00
|
|
|
''''
|
|
|
|
Cancel urself boi.
|
2021-11-17 18:32:42 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
task = asyncio.current_task()
|
|
|
|
|
|
|
|
# cancel and enter sleep
|
|
|
|
task.cancel()
|
2024-06-24 17:52:19 +00:00
|
|
|
await aio_sleep_forever()
|
2021-11-17 18:32:42 +00:00
|
|
|
|
|
|
|
|
2023-10-18 19:39:20 +00:00
|
|
|
def test_aio_cancelled_from_aio_causes_trio_cancelled(reg_addr):
|
2021-11-17 18:32:42 +00:00
|
|
|
|
|
|
|
async def main():
|
|
|
|
async with tractor.open_nursery() as n:
|
2021-11-25 22:10:22 +00:00
|
|
|
await n.run_in_actor(
|
2021-11-17 18:32:42 +00:00
|
|
|
asyncio_actor,
|
|
|
|
target='aio_cancel',
|
2021-11-24 17:13:02 +00:00
|
|
|
expect_err='tractor.to_asyncio.AsyncioCancelled',
|
2021-11-17 18:32:42 +00:00
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
|
|
|
|
2024-03-11 14:37:34 +00:00
|
|
|
with pytest.raises(
|
|
|
|
expected_exception=(RemoteActorError, ExceptionGroup),
|
|
|
|
) as excinfo:
|
2021-11-17 18:32:42 +00:00
|
|
|
trio.run(main)
|
2021-11-05 14:42:43 +00:00
|
|
|
|
2024-03-11 14:37:34 +00:00
|
|
|
# might get multiple `trio.Cancelled`s as well inside an inception
|
|
|
|
err = excinfo.value
|
|
|
|
if isinstance(err, ExceptionGroup):
|
|
|
|
err = next(itertools.dropwhile(
|
|
|
|
lambda exc: not isinstance(exc, tractor.RemoteActorError),
|
|
|
|
err.exceptions
|
|
|
|
))
|
|
|
|
assert err
|
|
|
|
|
2021-11-24 17:13:02 +00:00
|
|
|
# ensure boxed error is correct
|
2024-03-19 22:08:54 +00:00
|
|
|
assert err.boxed_type == to_asyncio.AsyncioCancelled
|
2021-11-24 17:13:02 +00:00
|
|
|
|
2021-11-05 14:42:43 +00:00
|
|
|
|
2021-11-22 18:27:16 +00:00
|
|
|
# TODO: verify open_channel_from will fail on this..
|
2021-11-20 17:47:03 +00:00
|
|
|
async def no_to_trio_in_args():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def push_from_aio_task(
|
|
|
|
|
|
|
|
sequence: Iterable,
|
|
|
|
to_trio: trio.abc.SendChannel,
|
2021-11-22 18:27:16 +00:00
|
|
|
expect_cancel: False,
|
|
|
|
fail_early: bool,
|
2021-11-20 17:47:03 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
2021-11-22 18:27:16 +00:00
|
|
|
try:
|
|
|
|
# sync caller ctx manager
|
|
|
|
to_trio.send_nowait(True)
|
|
|
|
|
|
|
|
for i in sequence:
|
|
|
|
print(f'asyncio sending {i}')
|
|
|
|
to_trio.send_nowait(i)
|
|
|
|
await asyncio.sleep(0.001)
|
|
|
|
|
|
|
|
if i == 50 and fail_early:
|
|
|
|
raise Exception
|
|
|
|
|
2021-11-24 17:13:02 +00:00
|
|
|
print('asyncio streamer complete!')
|
2021-11-22 18:27:16 +00:00
|
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
if not expect_cancel:
|
|
|
|
pytest.fail("aio task was cancelled unexpectedly")
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if expect_cancel:
|
|
|
|
pytest.fail("aio task wasn't cancelled as expected!?")
|
|
|
|
|
2021-11-20 17:47:03 +00:00
|
|
|
|
2021-11-22 18:27:16 +00:00
|
|
|
async def stream_from_aio(
|
|
|
|
exit_early: bool = False,
|
|
|
|
raise_err: bool = False,
|
|
|
|
aio_raise_err: bool = False,
|
2022-04-12 18:24:30 +00:00
|
|
|
fan_out: bool = False,
|
2021-11-22 18:27:16 +00:00
|
|
|
|
|
|
|
) -> None:
|
2021-11-20 17:47:03 +00:00
|
|
|
seq = range(100)
|
|
|
|
expect = list(seq)
|
|
|
|
|
2021-11-22 18:27:16 +00:00
|
|
|
try:
|
|
|
|
pulled = []
|
|
|
|
|
|
|
|
async with to_asyncio.open_channel_from(
|
|
|
|
push_from_aio_task,
|
|
|
|
sequence=seq,
|
|
|
|
expect_cancel=raise_err or exit_early,
|
|
|
|
fail_early=aio_raise_err,
|
|
|
|
) as (first, chan):
|
|
|
|
|
|
|
|
assert first is True
|
|
|
|
|
2022-04-12 18:24:30 +00:00
|
|
|
async def consume(
|
|
|
|
chan: Union[
|
|
|
|
to_asyncio.LinkedTaskChannel,
|
|
|
|
BroadcastReceiver,
|
|
|
|
],
|
|
|
|
):
|
|
|
|
async for value in chan:
|
|
|
|
print(f'trio received {value}')
|
|
|
|
pulled.append(value)
|
|
|
|
|
|
|
|
if value == 50:
|
|
|
|
if raise_err:
|
|
|
|
raise Exception
|
|
|
|
elif exit_early:
|
|
|
|
break
|
|
|
|
|
|
|
|
if fan_out:
|
|
|
|
# start second task that get's the same stream value set.
|
|
|
|
async with (
|
2022-04-12 19:03:44 +00:00
|
|
|
|
|
|
|
# NOTE: this has to come first to avoid
|
|
|
|
# the channel being closed before the nursery
|
|
|
|
# tasks are joined..
|
2022-04-12 18:24:30 +00:00
|
|
|
chan.subscribe() as br,
|
2022-04-12 19:03:44 +00:00
|
|
|
|
|
|
|
trio.open_nursery() as n,
|
2022-04-12 18:24:30 +00:00
|
|
|
):
|
|
|
|
n.start_soon(consume, br)
|
|
|
|
await consume(chan)
|
|
|
|
|
|
|
|
else:
|
|
|
|
await consume(chan)
|
2021-11-22 18:27:16 +00:00
|
|
|
finally:
|
2021-11-20 17:47:03 +00:00
|
|
|
|
2021-11-22 18:27:16 +00:00
|
|
|
if (
|
|
|
|
not raise_err and
|
|
|
|
not exit_early and
|
|
|
|
not aio_raise_err
|
|
|
|
):
|
2022-04-12 18:24:30 +00:00
|
|
|
if fan_out:
|
|
|
|
# we get double the pulled values in the
|
|
|
|
# ``.subscribe()`` fan out case.
|
|
|
|
doubled = list(itertools.chain(*zip(expect, expect)))
|
2022-04-12 20:13:33 +00:00
|
|
|
expect = doubled[:len(pulled)]
|
2022-04-12 20:13:33 +00:00
|
|
|
assert list(sorted(pulled)) == expect
|
2022-04-12 18:24:30 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
assert pulled == expect
|
2021-11-22 18:27:16 +00:00
|
|
|
else:
|
2022-04-12 20:13:33 +00:00
|
|
|
assert not fan_out
|
2021-11-22 18:27:16 +00:00
|
|
|
assert pulled == expect[:51]
|
2021-11-20 17:47:03 +00:00
|
|
|
|
2021-11-22 18:27:16 +00:00
|
|
|
print('trio guest mode task completed!')
|
2021-11-05 14:42:43 +00:00
|
|
|
|
|
|
|
|
2022-04-12 18:24:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'fan_out', [False, True],
|
|
|
|
ids='fan_out_w_chan_subscribe={}'.format
|
|
|
|
)
|
2023-10-18 19:39:20 +00:00
|
|
|
def test_basic_interloop_channel_stream(reg_addr, fan_out):
|
2021-11-20 17:47:03 +00:00
|
|
|
async def main():
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
portal = await n.run_in_actor(
|
|
|
|
stream_from_aio,
|
|
|
|
infect_asyncio=True,
|
2022-04-12 18:24:30 +00:00
|
|
|
fan_out=fan_out,
|
2021-11-20 17:47:03 +00:00
|
|
|
)
|
2024-05-09 20:31:23 +00:00
|
|
|
# should raise RAE diectly
|
2021-11-20 17:47:03 +00:00
|
|
|
await portal.result()
|
|
|
|
|
|
|
|
trio.run(main)
|
|
|
|
|
|
|
|
|
2021-11-22 18:27:16 +00:00
|
|
|
# TODO: parametrize the above test and avoid the duplication here?
|
2023-10-18 19:39:20 +00:00
|
|
|
def test_trio_error_cancels_intertask_chan(reg_addr):
|
2021-11-22 18:27:16 +00:00
|
|
|
async def main():
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
portal = await n.run_in_actor(
|
|
|
|
stream_from_aio,
|
|
|
|
raise_err=True,
|
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
|
|
|
# should trigger remote actor error
|
|
|
|
await portal.result()
|
2021-11-20 17:47:03 +00:00
|
|
|
|
2024-05-09 20:31:23 +00:00
|
|
|
with pytest.raises(RemoteActorError) as excinfo:
|
2021-11-22 18:27:16 +00:00
|
|
|
trio.run(main)
|
2021-11-05 14:42:43 +00:00
|
|
|
|
2024-05-09 20:31:23 +00:00
|
|
|
# ensure boxed error type
|
|
|
|
excinfo.value.boxed_type == Exception
|
2021-11-05 14:42:43 +00:00
|
|
|
|
2021-11-22 18:27:16 +00:00
|
|
|
|
2023-10-18 19:39:20 +00:00
|
|
|
def test_trio_closes_early_and_channel_exits(reg_addr):
|
2021-11-22 18:27:16 +00:00
|
|
|
async def main():
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
portal = await n.run_in_actor(
|
|
|
|
stream_from_aio,
|
|
|
|
exit_early=True,
|
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
2024-05-09 20:31:23 +00:00
|
|
|
# should raise RAE diectly
|
2021-11-22 18:27:16 +00:00
|
|
|
await portal.result()
|
|
|
|
|
|
|
|
# should be a quiet exit on a simple channel exit
|
|
|
|
trio.run(main)
|
|
|
|
|
|
|
|
|
2023-10-18 19:39:20 +00:00
|
|
|
def test_aio_errors_and_channel_propagates_and_closes(reg_addr):
|
2021-11-22 18:27:16 +00:00
|
|
|
async def main():
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
portal = await n.run_in_actor(
|
|
|
|
stream_from_aio,
|
|
|
|
aio_raise_err=True,
|
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
2024-05-09 20:31:23 +00:00
|
|
|
# should trigger RAE directly, not an eg.
|
2021-11-22 18:27:16 +00:00
|
|
|
await portal.result()
|
|
|
|
|
2024-05-09 20:31:23 +00:00
|
|
|
with pytest.raises(
|
|
|
|
# NOTE: bc we directly wait on `Portal.result()` instead
|
|
|
|
# of capturing it inside the `ActorNursery` machinery.
|
|
|
|
expected_exception=RemoteActorError,
|
|
|
|
) as excinfo:
|
2021-11-22 18:27:16 +00:00
|
|
|
trio.run(main)
|
|
|
|
|
2024-05-09 20:31:23 +00:00
|
|
|
excinfo.value.boxed_type == Exception
|
2021-11-05 14:42:43 +00:00
|
|
|
|
|
|
|
|
2021-11-25 22:10:22 +00:00
|
|
|
@tractor.context
|
|
|
|
async def trio_to_aio_echo_server(
|
|
|
|
ctx: tractor.Context,
|
|
|
|
):
|
|
|
|
|
|
|
|
async def aio_echo_server(
|
|
|
|
to_trio: trio.MemorySendChannel,
|
|
|
|
from_trio: asyncio.Queue,
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
to_trio.send_nowait('start')
|
|
|
|
|
|
|
|
while True:
|
|
|
|
msg = await from_trio.get()
|
|
|
|
|
|
|
|
# echo the msg back
|
|
|
|
to_trio.send_nowait(msg)
|
|
|
|
|
|
|
|
# if we get the terminate sentinel
|
|
|
|
# break the echo loop
|
|
|
|
if msg is None:
|
|
|
|
print('breaking aio echo loop')
|
|
|
|
break
|
|
|
|
|
2022-07-14 20:09:05 +00:00
|
|
|
print('exiting asyncio task')
|
|
|
|
|
2021-11-25 22:10:22 +00:00
|
|
|
async with to_asyncio.open_channel_from(
|
|
|
|
aio_echo_server,
|
|
|
|
) as (first, chan):
|
|
|
|
|
|
|
|
assert first == 'start'
|
|
|
|
await ctx.started(first)
|
|
|
|
|
|
|
|
async with ctx.open_stream() as stream:
|
|
|
|
|
|
|
|
async for msg in stream:
|
|
|
|
print(f'asyncio echoing {msg}')
|
|
|
|
await chan.send(msg)
|
|
|
|
|
|
|
|
out = await chan.receive()
|
|
|
|
# echo back to parent actor-task
|
|
|
|
await stream.send(out)
|
|
|
|
|
|
|
|
if out is None:
|
|
|
|
try:
|
|
|
|
out = await chan.receive()
|
|
|
|
except trio.EndOfChannel:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise RuntimeError('aio channel never stopped?')
|
|
|
|
|
|
|
|
|
2021-11-28 02:55:04 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'raise_error_mid_stream',
|
|
|
|
[False, Exception, KeyboardInterrupt],
|
|
|
|
ids='raise_error={}'.format,
|
|
|
|
)
|
|
|
|
def test_echoserver_detailed_mechanics(
|
2023-10-18 19:39:20 +00:00
|
|
|
reg_addr,
|
2021-11-28 02:55:04 +00:00
|
|
|
raise_error_mid_stream,
|
|
|
|
):
|
2021-11-25 22:10:22 +00:00
|
|
|
|
|
|
|
async def main():
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
p = await n.start_actor(
|
|
|
|
'aio_server',
|
|
|
|
enable_modules=[__name__],
|
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
|
|
|
async with p.open_context(
|
|
|
|
trio_to_aio_echo_server,
|
|
|
|
) as (ctx, first):
|
|
|
|
|
|
|
|
assert first == 'start'
|
|
|
|
|
|
|
|
async with ctx.open_stream() as stream:
|
|
|
|
for i in range(100):
|
|
|
|
await stream.send(i)
|
|
|
|
out = await stream.receive()
|
|
|
|
assert i == out
|
|
|
|
|
2021-11-28 02:55:04 +00:00
|
|
|
if raise_error_mid_stream and i == 50:
|
|
|
|
raise raise_error_mid_stream
|
|
|
|
|
2021-11-25 22:10:22 +00:00
|
|
|
# send terminate msg
|
|
|
|
await stream.send(None)
|
|
|
|
out = await stream.receive()
|
|
|
|
assert out is None
|
|
|
|
|
|
|
|
if out is None:
|
|
|
|
# ensure the stream is stopped
|
|
|
|
# with trio.fail_after(0.1):
|
|
|
|
try:
|
|
|
|
await stream.receive()
|
|
|
|
except trio.EndOfChannel:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
pytest.fail(
|
2024-03-25 20:09:32 +00:00
|
|
|
'stream not stopped after sentinel ?!'
|
|
|
|
)
|
2021-11-25 22:10:22 +00:00
|
|
|
|
|
|
|
# TODO: the case where this blocks and
|
|
|
|
# is cancelled by kbi or out of task cancellation
|
|
|
|
await p.cancel_actor()
|
|
|
|
|
2021-11-28 02:55:04 +00:00
|
|
|
if raise_error_mid_stream:
|
|
|
|
with pytest.raises(raise_error_mid_stream):
|
|
|
|
trio.run(main)
|
|
|
|
|
|
|
|
else:
|
|
|
|
trio.run(main)
|
2024-03-25 20:09:32 +00:00
|
|
|
|
|
|
|
|
2024-06-24 17:52:19 +00:00
|
|
|
|
|
|
|
@tractor.context
|
|
|
|
async def manage_file(
|
|
|
|
ctx: tractor.Context,
|
|
|
|
tmp_path_str: str,
|
|
|
|
bg_aio_task: bool = False,
|
|
|
|
):
|
|
|
|
'''
|
|
|
|
Start an `asyncio` task that just sleeps after registering a context
|
|
|
|
with `Actor.lifetime_stack`. Trigger a SIGINT to kill the actor tree
|
|
|
|
and ensure the stack is closed in the infected mode child.
|
|
|
|
|
|
|
|
To verify the teardown state just write a tmpfile to the `testdir`
|
|
|
|
and delete it on actor close.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
tmp_path: Path = Path(tmp_path_str)
|
|
|
|
tmp_file: Path = tmp_path / f'{" ".join(ctx._actor.uid)}.file'
|
|
|
|
|
|
|
|
# create a the tmp file and tell the parent where it's at
|
|
|
|
assert not tmp_file.is_file()
|
|
|
|
tmp_file.touch()
|
|
|
|
|
|
|
|
stack: ExitStack = current_actor().lifetime_stack
|
|
|
|
stack.callback(tmp_file.unlink)
|
|
|
|
|
|
|
|
await ctx.started((
|
|
|
|
str(tmp_file),
|
|
|
|
os.getpid(),
|
|
|
|
))
|
|
|
|
|
|
|
|
# expect to be cancelled from here!
|
|
|
|
try:
|
|
|
|
|
|
|
|
# NOTE: turns out you don't even need to sched an aio task
|
|
|
|
# since the original issue, even though seemingly was due to
|
|
|
|
# the guest-run being abandoned + a `._debug.pause()` inside
|
|
|
|
# `._runtime._async_main()` (which was originally trying to
|
|
|
|
# debug the `.lifetime_stack` not closing), IS NOT actually
|
|
|
|
# the core issue?
|
|
|
|
#
|
|
|
|
# further notes:
|
|
|
|
#
|
|
|
|
# - `trio` only issues the " RuntimeWarning: Trio guest run
|
|
|
|
# got abandoned without properly finishing... weird stuff
|
|
|
|
# might happen" IFF you DO run a asyncio task here, BUT
|
|
|
|
# - the original issue of the `.lifetime_stack` not closing
|
|
|
|
# will still happen even if you don't run an `asyncio` task
|
|
|
|
# here even though the "abandon" messgage won't be shown..
|
|
|
|
#
|
|
|
|
# => ????? honestly i'm lost but it seems to be some issue
|
|
|
|
# with `asyncio` and SIGINT..
|
|
|
|
#
|
|
|
|
# XXX NOTE XXX SO, if this LINE IS UNCOMMENTED and
|
|
|
|
# `run_as_asyncio_guest()` is written WITHOUT THE
|
|
|
|
# `.cancel_soon()` soln, both of these tests will pass ??
|
|
|
|
# so maybe it has something to do with `asyncio` loop init
|
|
|
|
# state?!?
|
|
|
|
# honestly, this REALLY reminds me why i haven't used
|
|
|
|
# `asyncio` by choice in years.. XD
|
|
|
|
#
|
|
|
|
# await tractor.to_asyncio.run_task(aio_sleep_forever)
|
|
|
|
if bg_aio_task:
|
|
|
|
async with trio.open_nursery() as tn:
|
|
|
|
tn.start_soon(
|
|
|
|
tractor.to_asyncio.run_task,
|
|
|
|
aio_sleep_forever,
|
|
|
|
)
|
|
|
|
|
|
|
|
await trio.sleep_forever()
|
|
|
|
|
|
|
|
# signalled manually at the OS level (aka KBI) by the parent actor.
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print('child raised KBI..')
|
|
|
|
assert tmp_file.exists()
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
raise RuntimeError('shoulda received a KBI?')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'bg_aio_task',
|
|
|
|
[
|
|
|
|
False,
|
|
|
|
|
|
|
|
# NOTE: (and see notes in `manage_file()` above as well) if
|
|
|
|
# we FOR SURE SPAWN AN AIO TASK in the child it seems the
|
|
|
|
# "silent-abandon" case (as is described in detail in
|
|
|
|
# `to_asyncio.run_as_asyncio_guest()`) does not happen and
|
|
|
|
# `asyncio`'s loop will at least abandon the `trio` side
|
|
|
|
# loudly? .. prolly the state-spot to start looking for
|
|
|
|
# a soln that results in NO ABANDONMENT.. XD
|
|
|
|
True,
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
'bg_aio_task',
|
|
|
|
'just_trio_slee',
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'wait_for_ctx',
|
|
|
|
[
|
|
|
|
False,
|
|
|
|
True,
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
'raise_KBI_in_rent',
|
|
|
|
'wait_for_ctx',
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_sigint_closes_lifetime_stack(
|
|
|
|
tmp_path: Path,
|
|
|
|
wait_for_ctx: bool,
|
|
|
|
bg_aio_task: bool,
|
|
|
|
):
|
|
|
|
'''
|
|
|
|
Ensure that an infected child can use the `Actor.lifetime_stack`
|
|
|
|
to make a file on boot and it's automatically cleaned up by the
|
|
|
|
actor-lifetime-linked exit stack closure.
|
|
|
|
|
|
|
|
'''
|
|
|
|
async def main():
|
|
|
|
try:
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
p = await n.start_actor(
|
|
|
|
'file_mngr',
|
|
|
|
enable_modules=[__name__],
|
|
|
|
infect_asyncio=True,
|
|
|
|
)
|
|
|
|
async with p.open_context(
|
|
|
|
manage_file,
|
|
|
|
tmp_path_str=str(tmp_path),
|
|
|
|
bg_aio_task=bg_aio_task,
|
|
|
|
) as (ctx, first):
|
|
|
|
|
|
|
|
path_str, cpid = first
|
|
|
|
tmp_file: Path = Path(path_str)
|
|
|
|
assert tmp_file.exists()
|
|
|
|
|
|
|
|
# XXX originally to simulate what (hopefully)
|
|
|
|
# the below now triggers.. had to manually
|
|
|
|
# trigger a SIGINT from a ctl-c in the root.
|
|
|
|
# await trio.sleep_forever()
|
|
|
|
|
|
|
|
# XXX NOTE XXX signal infected-`asyncio` child to
|
|
|
|
# OS-cancel with SIGINT; this should trigger the
|
|
|
|
# bad `asyncio` cancel behaviour that can cause
|
|
|
|
# a guest-run abandon as was seen causing
|
|
|
|
# shm-buffer leaks in `piker`'s live quote stream
|
|
|
|
# susbys!
|
|
|
|
#
|
|
|
|
# await trio.sleep(.5)
|
|
|
|
await trio.sleep(.2)
|
|
|
|
os.kill(
|
|
|
|
cpid,
|
|
|
|
signal.SIGINT,
|
|
|
|
)
|
|
|
|
|
|
|
|
# XXX CASE 1: without the bug fixed, in
|
|
|
|
# the non-KBI-raised-in-parent case, this
|
|
|
|
# timeout should trigger!
|
|
|
|
if wait_for_ctx:
|
|
|
|
print('waiting for ctx outcome in parent..')
|
|
|
|
try:
|
|
|
|
with trio.fail_after(.7):
|
|
|
|
await ctx.wait_for_result()
|
|
|
|
except tractor.ContextCancelled as ctxc:
|
|
|
|
assert ctxc.canceller == ctx.chan.uid
|
|
|
|
raise
|
|
|
|
|
|
|
|
# XXX CASE 2: this seems to be the source of the
|
|
|
|
# original issue which exhibited BEFORE we put
|
|
|
|
# a `Actor.cancel_soon()` inside
|
|
|
|
# `run_as_asyncio_guest()`..
|
|
|
|
else:
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
pytest.fail('should have raised some kinda error?!?')
|
|
|
|
|
|
|
|
except (
|
|
|
|
KeyboardInterrupt,
|
|
|
|
ContextCancelled,
|
|
|
|
):
|
|
|
|
# XXX CASE 2: without the bug fixed, in the
|
|
|
|
# KBI-raised-in-parent case, the actor teardown should
|
|
|
|
# never get run (silently abaondoned by `asyncio`..) and
|
|
|
|
# thus the file should leak!
|
|
|
|
assert not tmp_file.exists()
|
|
|
|
assert ctx.maybe_error
|
|
|
|
|
|
|
|
trio.run(main)
|
|
|
|
|
|
|
|
|
2024-03-25 20:09:32 +00:00
|
|
|
# TODO: debug_mode tests once we get support for `asyncio`!
|
|
|
|
#
|
|
|
|
# -[ ] need tests to wrap both scripts:
|
|
|
|
# - [ ] infected_asyncio_echo_server.py
|
|
|
|
# - [ ] debugging/asyncio_bp.py
|
|
|
|
# -[ ] consider moving ^ (some of) these ^ to `test_debugger`?
|
|
|
|
#
|
|
|
|
# -[ ] missing impl outstanding includes:
|
|
|
|
# - [x] for sync pauses we need to ensure we open yet another
|
|
|
|
# `greenback` portal in the asyncio task
|
|
|
|
# => completed using `.bestow_portal(task)` inside
|
|
|
|
# `.to_asyncio._run_asyncio_task()` right?
|
|
|
|
# -[ ] translation func to get from `asyncio` task calling to
|
|
|
|
# `._debug.wait_for_parent_stdin_hijack()` which does root
|
|
|
|
# call to do TTY locking.
|
|
|
|
#
|
|
|
|
def test_sync_breakpoint():
|
|
|
|
'''
|
|
|
|
Verify we can do sync-func/code breakpointing using the
|
|
|
|
`breakpoint()` builtin inside infected mode actors.
|
|
|
|
|
|
|
|
'''
|
|
|
|
pytest.xfail('This support is not implemented yet!')
|
|
|
|
|
|
|
|
|
|
|
|
def test_debug_mode_crash_handling():
|
|
|
|
'''
|
|
|
|
Verify mult-actor crash handling works with a combo of infected-`asyncio`-mode
|
|
|
|
and normal `trio` actors despite nested process trees.
|
|
|
|
|
|
|
|
'''
|
|
|
|
pytest.xfail('This support is not implemented yet!')
|