Compare commits
61 Commits
3ccbfd7e54
...
e8bd834b5b
Author | SHA1 | Date |
---|---|---|
|
e8bd834b5b | |
|
863751b47b | |
|
46c8dbef1f | |
|
e7dbb52b34 | |
|
d044629cce | |
|
8832cdfe0d | |
|
f6fc43d58d | |
|
cdc513f25d | |
|
9eaee7a060 | |
|
63c087f08d | |
|
d5f80365b5 | |
|
d20f711fb0 | |
|
21509791e3 | |
|
ce6974690b | |
|
972325a28d | |
|
b4f890bd58 | |
|
e2fa5a4d05 | |
|
2f4c019f39 | |
|
2b1dbcb541 | |
|
49ebdc2e6a | |
|
daf37ed24c | |
|
0701874033 | |
|
4621c8c1b9 | |
|
a69f1a61a5 | |
|
0c9e1be883 | |
|
8731ab3134 | |
|
b38ff36e04 | |
|
819889702f | |
|
a36ee01592 | |
|
dd9fe0b043 | |
|
e10ab9741d | |
|
91a970091f | |
|
5bf550b64a | |
|
a3a3d0b8cb | |
|
c1e0328669 | |
|
cfb74e588d | |
|
3d2b6613e8 | |
|
2b124447c8 | |
|
5ffdda762a | |
|
9082efbe68 | |
|
14f34c111a | |
|
f947bdf80c | |
|
dbd79d8beb | |
|
15a4a2a51e | |
|
ebf9909cc4 | |
|
2d541fdd9b | |
|
5f0bfeae57 | |
|
8b0b4abb3c | |
|
51bd38976f | |
|
4868bf225c | |
|
f834b35aa9 | |
|
6d671f69b8 | |
|
94c89fd425 | |
|
0246c824b9 | |
|
2e17b084b2 | |
|
61d82d47c2 | |
|
7246749137 | |
|
4db377c01d | |
|
ef4c4be0bb | |
|
7ce4bc489e | |
|
dec2b1f0f5 |
|
@ -77,7 +77,9 @@ async def main(
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
async with tractor.open_nursery() as n:
|
async with tractor.open_nursery(
|
||||||
|
# debug_mode=True,
|
||||||
|
) as n:
|
||||||
|
|
||||||
p = await n.start_actor(
|
p = await n.start_actor(
|
||||||
'aio_daemon',
|
'aio_daemon',
|
||||||
|
|
|
@ -4,9 +4,15 @@ import trio
|
||||||
|
|
||||||
async def breakpoint_forever():
|
async def breakpoint_forever():
|
||||||
"Indefinitely re-enter debugger in child actor."
|
"Indefinitely re-enter debugger in child actor."
|
||||||
while True:
|
try:
|
||||||
yield 'yo'
|
while True:
|
||||||
await tractor.breakpoint()
|
yield 'yo'
|
||||||
|
await tractor.breakpoint()
|
||||||
|
except BaseException:
|
||||||
|
tractor.log.get_console_log().exception(
|
||||||
|
'Cancelled while trying to enter pause point!'
|
||||||
|
)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
async def name_error():
|
async def name_error():
|
||||||
|
@ -19,7 +25,7 @@ async def main():
|
||||||
"""
|
"""
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
debug_mode=True,
|
debug_mode=True,
|
||||||
loglevel='error',
|
loglevel='cancel',
|
||||||
) as n:
|
) as n:
|
||||||
|
|
||||||
p0 = await n.start_actor('bp_forever', enable_modules=[__name__])
|
p0 = await n.start_actor('bp_forever', enable_modules=[__name__])
|
||||||
|
@ -32,7 +38,7 @@ async def main():
|
||||||
try:
|
try:
|
||||||
await p1.run(name_error)
|
await p1.run(name_error)
|
||||||
except tractor.RemoteActorError as rae:
|
except tractor.RemoteActorError as rae:
|
||||||
assert rae.type is NameError
|
assert rae.boxed_type is NameError
|
||||||
|
|
||||||
async for i in stream:
|
async for i in stream:
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,7 @@ async def spawn_until(depth=0):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: notes on the new boxed-relayed errors through proxy actors
|
||||||
async def main():
|
async def main():
|
||||||
"""The main ``tractor`` routine.
|
"""The main ``tractor`` routine.
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,7 @@ async def main():
|
||||||
"""
|
"""
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
debug_mode=True,
|
debug_mode=True,
|
||||||
|
# loglevel='runtime',
|
||||||
) as n:
|
) as n:
|
||||||
|
|
||||||
# Spawn both actors, don't bother with collecting results
|
# Spawn both actors, don't bother with collecting results
|
||||||
|
|
|
@ -23,5 +23,6 @@ async def main():
|
||||||
n.start_soon(debug_actor.run, die)
|
n.start_soon(debug_actor.run, die)
|
||||||
n.start_soon(crash_boi.run, die)
|
n.start_soon(crash_boi.run, die)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
|
@ -2,10 +2,13 @@ import trio
|
||||||
import tractor
|
import tractor
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main(
|
||||||
|
registry_addrs: tuple[str, int]|None = None
|
||||||
|
):
|
||||||
|
|
||||||
async with tractor.open_root_actor(
|
async with tractor.open_root_actor(
|
||||||
debug_mode=True,
|
debug_mode=True,
|
||||||
|
# loglevel='runtime',
|
||||||
):
|
):
|
||||||
while True:
|
while True:
|
||||||
await tractor.breakpoint()
|
await tractor.breakpoint()
|
||||||
|
|
|
@ -3,17 +3,20 @@ import tractor
|
||||||
|
|
||||||
|
|
||||||
async def breakpoint_forever():
|
async def breakpoint_forever():
|
||||||
"""Indefinitely re-enter debugger in child actor.
|
'''
|
||||||
"""
|
Indefinitely re-enter debugger in child actor.
|
||||||
|
|
||||||
|
'''
|
||||||
while True:
|
while True:
|
||||||
await trio.sleep(0.1)
|
await trio.sleep(0.1)
|
||||||
await tractor.breakpoint()
|
await tractor.pause()
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
debug_mode=True,
|
debug_mode=True,
|
||||||
|
loglevel='cancel',
|
||||||
) as n:
|
) as n:
|
||||||
|
|
||||||
portal = await n.run_in_actor(
|
portal = await n.run_in_actor(
|
||||||
|
|
|
@ -3,16 +3,26 @@ import tractor
|
||||||
|
|
||||||
|
|
||||||
async def name_error():
|
async def name_error():
|
||||||
getattr(doggypants)
|
getattr(doggypants) # noqa (on purpose)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
debug_mode=True,
|
debug_mode=True,
|
||||||
) as n:
|
# loglevel='transport',
|
||||||
|
) as an:
|
||||||
|
|
||||||
portal = await n.run_in_actor(name_error)
|
# TODO: ideally the REPL arrives at this frame in the parent,
|
||||||
await portal.result()
|
# ABOVE the @api_frame of `Portal.run_in_actor()` (which
|
||||||
|
# should eventually not even be a portal method ... XD)
|
||||||
|
# await tractor.pause()
|
||||||
|
p: tractor.Portal = await an.run_in_actor(name_error)
|
||||||
|
|
||||||
|
# with this style, should raise on this line
|
||||||
|
await p.result()
|
||||||
|
|
||||||
|
# with this alt style should raise at `open_nusery()`
|
||||||
|
# return await p.result()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
import trio
|
||||||
|
import tractor
|
||||||
|
|
||||||
|
|
||||||
|
def sync_pause(
|
||||||
|
use_builtin: bool = True,
|
||||||
|
error: bool = False,
|
||||||
|
):
|
||||||
|
if use_builtin:
|
||||||
|
breakpoint(hide_tb=False)
|
||||||
|
|
||||||
|
else:
|
||||||
|
tractor.pause_from_sync()
|
||||||
|
|
||||||
|
if error:
|
||||||
|
raise RuntimeError('yoyo sync code error')
|
||||||
|
|
||||||
|
|
||||||
|
@tractor.context
|
||||||
|
async def start_n_sync_pause(
|
||||||
|
ctx: tractor.Context,
|
||||||
|
):
|
||||||
|
actor: tractor.Actor = tractor.current_actor()
|
||||||
|
|
||||||
|
# sync to parent-side task
|
||||||
|
await ctx.started()
|
||||||
|
|
||||||
|
print(f'entering SYNC PAUSE in {actor.uid}')
|
||||||
|
sync_pause()
|
||||||
|
print(f'back from SYNC PAUSE in {actor.uid}')
|
||||||
|
|
||||||
|
|
||||||
|
async def main() -> None:
|
||||||
|
async with tractor.open_nursery(
|
||||||
|
# NOTE: required for pausing from sync funcs
|
||||||
|
maybe_enable_greenback=True,
|
||||||
|
debug_mode=True,
|
||||||
|
) as an:
|
||||||
|
|
||||||
|
p: tractor.Portal = await an.start_actor(
|
||||||
|
'subactor',
|
||||||
|
enable_modules=[__name__],
|
||||||
|
# infect_asyncio=True,
|
||||||
|
debug_mode=True,
|
||||||
|
loglevel='cancel',
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO: 3 sub-actor usage cases:
|
||||||
|
# -[ ] via a `.run_in_actor()` call
|
||||||
|
# -[ ] via a `.run()`
|
||||||
|
# -[ ] via a `.open_context()`
|
||||||
|
#
|
||||||
|
async with p.open_context(
|
||||||
|
start_n_sync_pause,
|
||||||
|
) as (ctx, first):
|
||||||
|
assert first is None
|
||||||
|
|
||||||
|
await tractor.pause()
|
||||||
|
sync_pause()
|
||||||
|
|
||||||
|
# TODO: make this work!!
|
||||||
|
await trio.to_thread.run_sync(
|
||||||
|
sync_pause,
|
||||||
|
abandon_on_cancel=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
await ctx.cancel()
|
||||||
|
|
||||||
|
# TODO: case where we cancel from trio-side while asyncio task
|
||||||
|
# has debugger lock?
|
||||||
|
await p.cancel_actor()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
trio.run(main)
|
|
@ -0,0 +1,8 @@
|
||||||
|
# vim: ft=ini
|
||||||
|
# pytest.ini for tractor
|
||||||
|
|
||||||
|
[pytest]
|
||||||
|
# don't show frickin captured logs AGAIN in the report..
|
||||||
|
addopts = --show-capture='no'
|
||||||
|
log_cli = false
|
||||||
|
; minversion = 6.0
|
|
@ -114,12 +114,18 @@ _reg_addr: tuple[str, int] = (
|
||||||
'127.0.0.1',
|
'127.0.0.1',
|
||||||
random.randint(1000, 9999),
|
random.randint(1000, 9999),
|
||||||
)
|
)
|
||||||
_arb_addr = _reg_addr
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
def arb_addr():
|
def reg_addr() -> tuple[str, int]:
|
||||||
return _arb_addr
|
|
||||||
|
# globally override the runtime to the per-test-session-dynamic
|
||||||
|
# addr so that all tests never conflict with any other actor
|
||||||
|
# tree using the default.
|
||||||
|
from tractor import _root
|
||||||
|
_root._default_lo_addrs = [_reg_addr]
|
||||||
|
|
||||||
|
return _reg_addr
|
||||||
|
|
||||||
|
|
||||||
def pytest_generate_tests(metafunc):
|
def pytest_generate_tests(metafunc):
|
||||||
|
@ -161,30 +167,35 @@ def sig_prog(proc, sig):
|
||||||
def daemon(
|
def daemon(
|
||||||
loglevel: str,
|
loglevel: str,
|
||||||
testdir,
|
testdir,
|
||||||
arb_addr: tuple[str, int],
|
reg_addr: tuple[str, int],
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
Run a daemon actor as a "remote arbiter".
|
Run a daemon root actor as a separate actor-process tree and
|
||||||
|
"remote registrar" for discovery-protocol related tests.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
if loglevel in ('trace', 'debug'):
|
if loglevel in ('trace', 'debug'):
|
||||||
# too much logging will lock up the subproc (smh)
|
# XXX: too much logging will lock up the subproc (smh)
|
||||||
loglevel = 'info'
|
loglevel: str = 'info'
|
||||||
|
|
||||||
cmdargs = [
|
code: str = (
|
||||||
sys.executable, '-c',
|
"import tractor; "
|
||||||
"import tractor; tractor.run_daemon([], registry_addr={}, loglevel={})"
|
"tractor.run_daemon([], registry_addrs={reg_addrs}, loglevel={ll})"
|
||||||
.format(
|
).format(
|
||||||
arb_addr,
|
reg_addrs=str([reg_addr]),
|
||||||
"'{}'".format(loglevel) if loglevel else None)
|
ll="'{}'".format(loglevel) if loglevel else None,
|
||||||
|
)
|
||||||
|
cmd: list[str] = [
|
||||||
|
sys.executable,
|
||||||
|
'-c', code,
|
||||||
]
|
]
|
||||||
kwargs = dict()
|
kwargs = {}
|
||||||
if platform.system() == 'Windows':
|
if platform.system() == 'Windows':
|
||||||
# without this, tests hang on windows forever
|
# without this, tests hang on windows forever
|
||||||
kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
|
kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
|
||||||
|
|
||||||
proc = testdir.popen(
|
proc = testdir.popen(
|
||||||
cmdargs,
|
cmd,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
|
@ -95,6 +95,7 @@ def test_ipc_channel_break_during_stream(
|
||||||
mod: ModuleType = import_path(
|
mod: ModuleType = import_path(
|
||||||
examples_dir() / 'advanced_faults' / 'ipc_failure_during_stream.py',
|
examples_dir() / 'advanced_faults' / 'ipc_failure_during_stream.py',
|
||||||
root=examples_dir(),
|
root=examples_dir(),
|
||||||
|
consider_namespace_packages=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
# by def we expect KBI from user after a simulated "hang
|
# by def we expect KBI from user after a simulated "hang
|
||||||
|
|
|
@ -45,7 +45,7 @@ async def do_nuthin():
|
||||||
],
|
],
|
||||||
ids=['no_args', 'unexpected_args'],
|
ids=['no_args', 'unexpected_args'],
|
||||||
)
|
)
|
||||||
def test_remote_error(arb_addr, args_err):
|
def test_remote_error(reg_addr, args_err):
|
||||||
'''
|
'''
|
||||||
Verify an error raised in a subactor that is propagated
|
Verify an error raised in a subactor that is propagated
|
||||||
to the parent nursery, contains the underlying boxed builtin
|
to the parent nursery, contains the underlying boxed builtin
|
||||||
|
@ -57,7 +57,7 @@ def test_remote_error(arb_addr, args_err):
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
) as nursery:
|
) as nursery:
|
||||||
|
|
||||||
# on a remote type error caused by bad input args
|
# on a remote type error caused by bad input args
|
||||||
|
@ -77,7 +77,7 @@ def test_remote_error(arb_addr, args_err):
|
||||||
# of this actor nursery.
|
# of this actor nursery.
|
||||||
await portal.result()
|
await portal.result()
|
||||||
except tractor.RemoteActorError as err:
|
except tractor.RemoteActorError as err:
|
||||||
assert err.type == errtype
|
assert err.boxed_type == errtype
|
||||||
print("Look Maa that actor failed hard, hehh")
|
print("Look Maa that actor failed hard, hehh")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ def test_remote_error(arb_addr, args_err):
|
||||||
with pytest.raises(tractor.RemoteActorError) as excinfo:
|
with pytest.raises(tractor.RemoteActorError) as excinfo:
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
||||||
assert excinfo.value.type == errtype
|
assert excinfo.value.boxed_type == errtype
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# the root task will also error on the `.result()` call
|
# the root task will also error on the `.result()` call
|
||||||
|
@ -96,10 +96,10 @@ def test_remote_error(arb_addr, args_err):
|
||||||
|
|
||||||
# ensure boxed errors
|
# ensure boxed errors
|
||||||
for exc in excinfo.value.exceptions:
|
for exc in excinfo.value.exceptions:
|
||||||
assert exc.type == errtype
|
assert exc.boxed_type == errtype
|
||||||
|
|
||||||
|
|
||||||
def test_multierror(arb_addr):
|
def test_multierror(reg_addr):
|
||||||
'''
|
'''
|
||||||
Verify we raise a ``BaseExceptionGroup`` out of a nursery where
|
Verify we raise a ``BaseExceptionGroup`` out of a nursery where
|
||||||
more then one actor errors.
|
more then one actor errors.
|
||||||
|
@ -107,7 +107,7 @@ def test_multierror(arb_addr):
|
||||||
'''
|
'''
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
) as nursery:
|
) as nursery:
|
||||||
|
|
||||||
await nursery.run_in_actor(assert_err, name='errorer1')
|
await nursery.run_in_actor(assert_err, name='errorer1')
|
||||||
|
@ -117,7 +117,7 @@ def test_multierror(arb_addr):
|
||||||
try:
|
try:
|
||||||
await portal2.result()
|
await portal2.result()
|
||||||
except tractor.RemoteActorError as err:
|
except tractor.RemoteActorError as err:
|
||||||
assert err.type == AssertionError
|
assert err.boxed_type == AssertionError
|
||||||
print("Look Maa that first actor failed hard, hehh")
|
print("Look Maa that first actor failed hard, hehh")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@ -132,14 +132,14 @@ def test_multierror(arb_addr):
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'num_subactors', range(25, 26),
|
'num_subactors', range(25, 26),
|
||||||
)
|
)
|
||||||
def test_multierror_fast_nursery(arb_addr, start_method, num_subactors, delay):
|
def test_multierror_fast_nursery(reg_addr, start_method, num_subactors, delay):
|
||||||
"""Verify we raise a ``BaseExceptionGroup`` out of a nursery where
|
"""Verify we raise a ``BaseExceptionGroup`` out of a nursery where
|
||||||
more then one actor errors and also with a delay before failure
|
more then one actor errors and also with a delay before failure
|
||||||
to test failure during an ongoing spawning.
|
to test failure during an ongoing spawning.
|
||||||
"""
|
"""
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
) as nursery:
|
) as nursery:
|
||||||
|
|
||||||
for i in range(num_subactors):
|
for i in range(num_subactors):
|
||||||
|
@ -169,7 +169,7 @@ def test_multierror_fast_nursery(arb_addr, start_method, num_subactors, delay):
|
||||||
|
|
||||||
for exc in exceptions:
|
for exc in exceptions:
|
||||||
assert isinstance(exc, tractor.RemoteActorError)
|
assert isinstance(exc, tractor.RemoteActorError)
|
||||||
assert exc.type == AssertionError
|
assert exc.boxed_type == AssertionError
|
||||||
|
|
||||||
|
|
||||||
async def do_nothing():
|
async def do_nothing():
|
||||||
|
@ -177,15 +177,20 @@ async def do_nothing():
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('mechanism', ['nursery_cancel', KeyboardInterrupt])
|
@pytest.mark.parametrize('mechanism', ['nursery_cancel', KeyboardInterrupt])
|
||||||
def test_cancel_single_subactor(arb_addr, mechanism):
|
def test_cancel_single_subactor(reg_addr, mechanism):
|
||||||
"""Ensure a ``ActorNursery.start_actor()`` spawned subactor
|
'''
|
||||||
|
Ensure a ``ActorNursery.start_actor()`` spawned subactor
|
||||||
cancels when the nursery is cancelled.
|
cancels when the nursery is cancelled.
|
||||||
"""
|
|
||||||
|
'''
|
||||||
async def spawn_actor():
|
async def spawn_actor():
|
||||||
"""Spawn an actor that blocks indefinitely.
|
'''
|
||||||
"""
|
Spawn an actor that blocks indefinitely then cancel via
|
||||||
|
either `ActorNursery.cancel()` or an exception raise.
|
||||||
|
|
||||||
|
'''
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
) as nursery:
|
) as nursery:
|
||||||
|
|
||||||
portal = await nursery.start_actor(
|
portal = await nursery.start_actor(
|
||||||
|
@ -305,7 +310,7 @@ async def test_some_cancels_all(num_actors_and_errs, start_method, loglevel):
|
||||||
await portal.run(func, **kwargs)
|
await portal.run(func, **kwargs)
|
||||||
|
|
||||||
except tractor.RemoteActorError as err:
|
except tractor.RemoteActorError as err:
|
||||||
assert err.type == err_type
|
assert err.boxed_type == err_type
|
||||||
# we only expect this first error to propogate
|
# we only expect this first error to propogate
|
||||||
# (all other daemons are cancelled before they
|
# (all other daemons are cancelled before they
|
||||||
# can be scheduled)
|
# can be scheduled)
|
||||||
|
@ -324,11 +329,11 @@ async def test_some_cancels_all(num_actors_and_errs, start_method, loglevel):
|
||||||
assert len(err.exceptions) == num_actors
|
assert len(err.exceptions) == num_actors
|
||||||
for exc in err.exceptions:
|
for exc in err.exceptions:
|
||||||
if isinstance(exc, tractor.RemoteActorError):
|
if isinstance(exc, tractor.RemoteActorError):
|
||||||
assert exc.type == err_type
|
assert exc.boxed_type == err_type
|
||||||
else:
|
else:
|
||||||
assert isinstance(exc, trio.Cancelled)
|
assert isinstance(exc, trio.Cancelled)
|
||||||
elif isinstance(err, tractor.RemoteActorError):
|
elif isinstance(err, tractor.RemoteActorError):
|
||||||
assert err.type == err_type
|
assert err.boxed_type == err_type
|
||||||
|
|
||||||
assert n.cancelled is True
|
assert n.cancelled is True
|
||||||
assert not n._children
|
assert not n._children
|
||||||
|
@ -407,7 +412,7 @@ async def test_nested_multierrors(loglevel, start_method):
|
||||||
elif isinstance(subexc, tractor.RemoteActorError):
|
elif isinstance(subexc, tractor.RemoteActorError):
|
||||||
# on windows it seems we can't exactly be sure wtf
|
# on windows it seems we can't exactly be sure wtf
|
||||||
# will happen..
|
# will happen..
|
||||||
assert subexc.type in (
|
assert subexc.boxed_type in (
|
||||||
tractor.RemoteActorError,
|
tractor.RemoteActorError,
|
||||||
trio.Cancelled,
|
trio.Cancelled,
|
||||||
BaseExceptionGroup,
|
BaseExceptionGroup,
|
||||||
|
@ -417,7 +422,7 @@ async def test_nested_multierrors(loglevel, start_method):
|
||||||
for subsub in subexc.exceptions:
|
for subsub in subexc.exceptions:
|
||||||
|
|
||||||
if subsub in (tractor.RemoteActorError,):
|
if subsub in (tractor.RemoteActorError,):
|
||||||
subsub = subsub.type
|
subsub = subsub.boxed_type
|
||||||
|
|
||||||
assert type(subsub) in (
|
assert type(subsub) in (
|
||||||
trio.Cancelled,
|
trio.Cancelled,
|
||||||
|
@ -432,16 +437,16 @@ async def test_nested_multierrors(loglevel, start_method):
|
||||||
# we get back the (sent) cancel signal instead
|
# we get back the (sent) cancel signal instead
|
||||||
if is_win():
|
if is_win():
|
||||||
if isinstance(subexc, tractor.RemoteActorError):
|
if isinstance(subexc, tractor.RemoteActorError):
|
||||||
assert subexc.type in (
|
assert subexc.boxed_type in (
|
||||||
BaseExceptionGroup,
|
BaseExceptionGroup,
|
||||||
tractor.RemoteActorError
|
tractor.RemoteActorError
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
assert isinstance(subexc, BaseExceptionGroup)
|
assert isinstance(subexc, BaseExceptionGroup)
|
||||||
else:
|
else:
|
||||||
assert subexc.type is ExceptionGroup
|
assert subexc.boxed_type is ExceptionGroup
|
||||||
else:
|
else:
|
||||||
assert subexc.type in (
|
assert subexc.boxed_type in (
|
||||||
tractor.RemoteActorError,
|
tractor.RemoteActorError,
|
||||||
trio.Cancelled
|
trio.Cancelled
|
||||||
)
|
)
|
||||||
|
|
|
@ -142,7 +142,7 @@ async def open_actor_local_nursery(
|
||||||
)
|
)
|
||||||
def test_actor_managed_trio_nursery_task_error_cancels_aio(
|
def test_actor_managed_trio_nursery_task_error_cancels_aio(
|
||||||
asyncio_mode: bool,
|
asyncio_mode: bool,
|
||||||
arb_addr
|
reg_addr: tuple,
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
Verify that a ``trio`` nursery created managed in a child actor
|
Verify that a ``trio`` nursery created managed in a child actor
|
||||||
|
@ -171,4 +171,4 @@ def test_actor_managed_trio_nursery_task_error_cancels_aio(
|
||||||
|
|
||||||
# verify boxed error
|
# verify boxed error
|
||||||
err = excinfo.value
|
err = excinfo.value
|
||||||
assert isinstance(err.type(), NameError)
|
assert err.boxed_type is NameError
|
||||||
|
|
|
@ -795,7 +795,7 @@ async def test_callee_cancels_before_started(
|
||||||
|
|
||||||
# raises a special cancel signal
|
# raises a special cancel signal
|
||||||
except tractor.ContextCancelled as ce:
|
except tractor.ContextCancelled as ce:
|
||||||
ce.type == trio.Cancelled
|
ce.boxed_type == trio.Cancelled
|
||||||
|
|
||||||
# the traceback should be informative
|
# the traceback should be informative
|
||||||
assert 'itself' in ce.msgdata['tb_str']
|
assert 'itself' in ce.msgdata['tb_str']
|
||||||
|
@ -903,7 +903,7 @@ def test_one_end_stream_not_opened(
|
||||||
with pytest.raises(tractor.RemoteActorError) as excinfo:
|
with pytest.raises(tractor.RemoteActorError) as excinfo:
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
||||||
assert excinfo.value.type == StreamOverrun
|
assert excinfo.value.boxed_type == StreamOverrun
|
||||||
|
|
||||||
elif overrunner == 'callee':
|
elif overrunner == 'callee':
|
||||||
with pytest.raises(tractor.RemoteActorError) as excinfo:
|
with pytest.raises(tractor.RemoteActorError) as excinfo:
|
||||||
|
@ -912,7 +912,7 @@ def test_one_end_stream_not_opened(
|
||||||
# TODO: embedded remote errors so that we can verify the source
|
# TODO: embedded remote errors so that we can verify the source
|
||||||
# error? the callee delivers an error which is an overrun
|
# error? the callee delivers an error which is an overrun
|
||||||
# wrapped in a remote actor error.
|
# wrapped in a remote actor error.
|
||||||
assert excinfo.value.type == tractor.RemoteActorError
|
assert excinfo.value.boxed_type == tractor.RemoteActorError
|
||||||
|
|
||||||
else:
|
else:
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
@ -1131,7 +1131,7 @@ def test_maybe_allow_overruns_stream(
|
||||||
# NOTE: i tried to isolate to a deterministic case here
|
# NOTE: i tried to isolate to a deterministic case here
|
||||||
# based on timeing, but i was kinda wasted, and i don't
|
# based on timeing, but i was kinda wasted, and i don't
|
||||||
# think it's sane to catch them..
|
# think it's sane to catch them..
|
||||||
assert err.type in (
|
assert err.boxed_type in (
|
||||||
tractor.RemoteActorError,
|
tractor.RemoteActorError,
|
||||||
StreamOverrun,
|
StreamOverrun,
|
||||||
)
|
)
|
||||||
|
@ -1139,10 +1139,10 @@ def test_maybe_allow_overruns_stream(
|
||||||
elif (
|
elif (
|
||||||
slow_side == 'child'
|
slow_side == 'child'
|
||||||
):
|
):
|
||||||
assert err.type == StreamOverrun
|
assert err.boxed_type == StreamOverrun
|
||||||
|
|
||||||
elif slow_side == 'parent':
|
elif slow_side == 'parent':
|
||||||
assert err.type == tractor.RemoteActorError
|
assert err.boxed_type == tractor.RemoteActorError
|
||||||
assert 'StreamOverrun' in err.msgdata['tb_str']
|
assert 'StreamOverrun' in err.msgdata['tb_str']
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -83,7 +83,7 @@ has_nested_actors = pytest.mark.has_nested_actors
|
||||||
def spawn(
|
def spawn(
|
||||||
start_method,
|
start_method,
|
||||||
testdir,
|
testdir,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
) -> 'pexpect.spawn':
|
) -> 'pexpect.spawn':
|
||||||
|
|
||||||
if start_method != 'trio':
|
if start_method != 'trio':
|
||||||
|
@ -203,7 +203,7 @@ def ctlc(
|
||||||
# XXX: disable pygments highlighting for auto-tests
|
# XXX: disable pygments highlighting for auto-tests
|
||||||
# since some envs (like actions CI) will struggle
|
# since some envs (like actions CI) will struggle
|
||||||
# the the added color-char encoding..
|
# the the added color-char encoding..
|
||||||
from tractor._debug import TractorConfig
|
from tractor.devx._debug import TractorConfig
|
||||||
TractorConfig.use_pygements = False
|
TractorConfig.use_pygements = False
|
||||||
|
|
||||||
yield use_ctlc
|
yield use_ctlc
|
||||||
|
@ -685,7 +685,7 @@ def test_multi_daemon_subactors(
|
||||||
# now the root actor won't clobber the bp_forever child
|
# now the root actor won't clobber the bp_forever child
|
||||||
# during it's first access to the debug lock, but will instead
|
# during it's first access to the debug lock, but will instead
|
||||||
# wait for the lock to release, by the edge triggered
|
# wait for the lock to release, by the edge triggered
|
||||||
# ``_debug.Lock.no_remote_has_tty`` event before sending cancel messages
|
# ``devx._debug.Lock.no_remote_has_tty`` event before sending cancel messages
|
||||||
# (via portals) to its underlings B)
|
# (via portals) to its underlings B)
|
||||||
|
|
||||||
# at some point here there should have been some warning msg from
|
# at some point here there should have been some warning msg from
|
||||||
|
@ -1025,3 +1025,67 @@ def test_different_debug_mode_per_actor(
|
||||||
# instead crashed completely
|
# instead crashed completely
|
||||||
assert "tractor._exceptions.RemoteActorError: ('crash_boi'" in before
|
assert "tractor._exceptions.RemoteActorError: ('crash_boi'" in before
|
||||||
assert "RuntimeError" in before
|
assert "RuntimeError" in before
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_pause_from_sync(
|
||||||
|
spawn,
|
||||||
|
ctlc: bool
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Verify we can use the `pdbp` REPL from sync functions AND from
|
||||||
|
any thread spawned with `trio.to_thread.run_sync()`.
|
||||||
|
|
||||||
|
`examples/debugging/sync_bp.py`
|
||||||
|
|
||||||
|
'''
|
||||||
|
child = spawn('sync_bp')
|
||||||
|
child.expect(PROMPT)
|
||||||
|
assert_before(
|
||||||
|
child,
|
||||||
|
[
|
||||||
|
'`greenback` portal opened!',
|
||||||
|
# pre-prompt line
|
||||||
|
_pause_msg, "('root'",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
if ctlc:
|
||||||
|
do_ctlc(child)
|
||||||
|
child.sendline('c')
|
||||||
|
child.expect(PROMPT)
|
||||||
|
|
||||||
|
# XXX shouldn't see gb loaded again
|
||||||
|
before = str(child.before.decode())
|
||||||
|
assert not in_prompt_msg(
|
||||||
|
before,
|
||||||
|
['`greenback` portal opened!'],
|
||||||
|
)
|
||||||
|
assert_before(
|
||||||
|
child,
|
||||||
|
[_pause_msg, "('root'",],
|
||||||
|
)
|
||||||
|
|
||||||
|
if ctlc:
|
||||||
|
do_ctlc(child)
|
||||||
|
child.sendline('c')
|
||||||
|
child.expect(PROMPT)
|
||||||
|
assert_before(
|
||||||
|
child,
|
||||||
|
[_pause_msg, "('subactor'",],
|
||||||
|
)
|
||||||
|
|
||||||
|
if ctlc:
|
||||||
|
do_ctlc(child)
|
||||||
|
child.sendline('c')
|
||||||
|
child.expect(PROMPT)
|
||||||
|
# non-main thread case
|
||||||
|
# TODO: should we agument the pre-prompt msg in this case?
|
||||||
|
assert_before(
|
||||||
|
child,
|
||||||
|
[_pause_msg, "('root'",],
|
||||||
|
)
|
||||||
|
|
||||||
|
if ctlc:
|
||||||
|
do_ctlc(child)
|
||||||
|
child.sendline('c')
|
||||||
|
child.expect(pexpect.EOF)
|
||||||
|
|
|
@ -14,19 +14,19 @@ import trio
|
||||||
|
|
||||||
|
|
||||||
@tractor_test
|
@tractor_test
|
||||||
async def test_reg_then_unreg(arb_addr):
|
async def test_reg_then_unreg(reg_addr):
|
||||||
actor = tractor.current_actor()
|
actor = tractor.current_actor()
|
||||||
assert actor.is_arbiter
|
assert actor.is_arbiter
|
||||||
assert len(actor._registry) == 1 # only self is registered
|
assert len(actor._registry) == 1 # only self is registered
|
||||||
|
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
) as n:
|
) as n:
|
||||||
|
|
||||||
portal = await n.start_actor('actor', enable_modules=[__name__])
|
portal = await n.start_actor('actor', enable_modules=[__name__])
|
||||||
uid = portal.channel.uid
|
uid = portal.channel.uid
|
||||||
|
|
||||||
async with tractor.get_arbiter(*arb_addr) as aportal:
|
async with tractor.get_arbiter(*reg_addr) as aportal:
|
||||||
# this local actor should be the arbiter
|
# this local actor should be the arbiter
|
||||||
assert actor is aportal.actor
|
assert actor is aportal.actor
|
||||||
|
|
||||||
|
@ -52,15 +52,27 @@ async def hi():
|
||||||
return the_line.format(tractor.current_actor().name)
|
return the_line.format(tractor.current_actor().name)
|
||||||
|
|
||||||
|
|
||||||
async def say_hello(other_actor):
|
async def say_hello(
|
||||||
|
other_actor: str,
|
||||||
|
reg_addr: tuple[str, int],
|
||||||
|
):
|
||||||
await trio.sleep(1) # wait for other actor to spawn
|
await trio.sleep(1) # wait for other actor to spawn
|
||||||
async with tractor.find_actor(other_actor) as portal:
|
async with tractor.find_actor(
|
||||||
|
other_actor,
|
||||||
|
registry_addrs=[reg_addr],
|
||||||
|
) as portal:
|
||||||
assert portal is not None
|
assert portal is not None
|
||||||
return await portal.run(__name__, 'hi')
|
return await portal.run(__name__, 'hi')
|
||||||
|
|
||||||
|
|
||||||
async def say_hello_use_wait(other_actor):
|
async def say_hello_use_wait(
|
||||||
async with tractor.wait_for_actor(other_actor) as portal:
|
other_actor: str,
|
||||||
|
reg_addr: tuple[str, int],
|
||||||
|
):
|
||||||
|
async with tractor.wait_for_actor(
|
||||||
|
other_actor,
|
||||||
|
registry_addr=reg_addr,
|
||||||
|
) as portal:
|
||||||
assert portal is not None
|
assert portal is not None
|
||||||
result = await portal.run(__name__, 'hi')
|
result = await portal.run(__name__, 'hi')
|
||||||
return result
|
return result
|
||||||
|
@ -68,21 +80,29 @@ async def say_hello_use_wait(other_actor):
|
||||||
|
|
||||||
@tractor_test
|
@tractor_test
|
||||||
@pytest.mark.parametrize('func', [say_hello, say_hello_use_wait])
|
@pytest.mark.parametrize('func', [say_hello, say_hello_use_wait])
|
||||||
async def test_trynamic_trio(func, start_method, arb_addr):
|
async def test_trynamic_trio(
|
||||||
"""Main tractor entry point, the "master" process (for now
|
func,
|
||||||
acts as the "director").
|
start_method,
|
||||||
"""
|
reg_addr,
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Root actor acting as the "director" and running one-shot-task-actors
|
||||||
|
for the directed subs.
|
||||||
|
|
||||||
|
'''
|
||||||
async with tractor.open_nursery() as n:
|
async with tractor.open_nursery() as n:
|
||||||
print("Alright... Action!")
|
print("Alright... Action!")
|
||||||
|
|
||||||
donny = await n.run_in_actor(
|
donny = await n.run_in_actor(
|
||||||
func,
|
func,
|
||||||
other_actor='gretchen',
|
other_actor='gretchen',
|
||||||
|
reg_addr=reg_addr,
|
||||||
name='donny',
|
name='donny',
|
||||||
)
|
)
|
||||||
gretchen = await n.run_in_actor(
|
gretchen = await n.run_in_actor(
|
||||||
func,
|
func,
|
||||||
other_actor='donny',
|
other_actor='donny',
|
||||||
|
reg_addr=reg_addr,
|
||||||
name='gretchen',
|
name='gretchen',
|
||||||
)
|
)
|
||||||
print(await gretchen.result())
|
print(await gretchen.result())
|
||||||
|
@ -130,7 +150,7 @@ async def unpack_reg(actor_or_portal):
|
||||||
|
|
||||||
|
|
||||||
async def spawn_and_check_registry(
|
async def spawn_and_check_registry(
|
||||||
arb_addr: tuple,
|
reg_addr: tuple,
|
||||||
use_signal: bool,
|
use_signal: bool,
|
||||||
remote_arbiter: bool = False,
|
remote_arbiter: bool = False,
|
||||||
with_streaming: bool = False,
|
with_streaming: bool = False,
|
||||||
|
@ -138,9 +158,9 @@ async def spawn_and_check_registry(
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
async with tractor.open_root_actor(
|
async with tractor.open_root_actor(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
):
|
):
|
||||||
async with tractor.get_arbiter(*arb_addr) as portal:
|
async with tractor.get_arbiter(*reg_addr) as portal:
|
||||||
# runtime needs to be up to call this
|
# runtime needs to be up to call this
|
||||||
actor = tractor.current_actor()
|
actor = tractor.current_actor()
|
||||||
|
|
||||||
|
@ -212,17 +232,19 @@ async def spawn_and_check_registry(
|
||||||
def test_subactors_unregister_on_cancel(
|
def test_subactors_unregister_on_cancel(
|
||||||
start_method,
|
start_method,
|
||||||
use_signal,
|
use_signal,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
with_streaming,
|
with_streaming,
|
||||||
):
|
):
|
||||||
"""Verify that cancelling a nursery results in all subactors
|
'''
|
||||||
|
Verify that cancelling a nursery results in all subactors
|
||||||
deregistering themselves with the arbiter.
|
deregistering themselves with the arbiter.
|
||||||
"""
|
|
||||||
|
'''
|
||||||
with pytest.raises(KeyboardInterrupt):
|
with pytest.raises(KeyboardInterrupt):
|
||||||
trio.run(
|
trio.run(
|
||||||
partial(
|
partial(
|
||||||
spawn_and_check_registry,
|
spawn_and_check_registry,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
use_signal,
|
use_signal,
|
||||||
remote_arbiter=False,
|
remote_arbiter=False,
|
||||||
with_streaming=with_streaming,
|
with_streaming=with_streaming,
|
||||||
|
@ -236,7 +258,7 @@ def test_subactors_unregister_on_cancel_remote_daemon(
|
||||||
daemon,
|
daemon,
|
||||||
start_method,
|
start_method,
|
||||||
use_signal,
|
use_signal,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
with_streaming,
|
with_streaming,
|
||||||
):
|
):
|
||||||
"""Verify that cancelling a nursery results in all subactors
|
"""Verify that cancelling a nursery results in all subactors
|
||||||
|
@ -247,7 +269,7 @@ def test_subactors_unregister_on_cancel_remote_daemon(
|
||||||
trio.run(
|
trio.run(
|
||||||
partial(
|
partial(
|
||||||
spawn_and_check_registry,
|
spawn_and_check_registry,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
use_signal,
|
use_signal,
|
||||||
remote_arbiter=True,
|
remote_arbiter=True,
|
||||||
with_streaming=with_streaming,
|
with_streaming=with_streaming,
|
||||||
|
@ -261,7 +283,7 @@ async def streamer(agen):
|
||||||
|
|
||||||
|
|
||||||
async def close_chans_before_nursery(
|
async def close_chans_before_nursery(
|
||||||
arb_addr: tuple,
|
reg_addr: tuple,
|
||||||
use_signal: bool,
|
use_signal: bool,
|
||||||
remote_arbiter: bool = False,
|
remote_arbiter: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -274,9 +296,9 @@ async def close_chans_before_nursery(
|
||||||
entries_at_end = 1
|
entries_at_end = 1
|
||||||
|
|
||||||
async with tractor.open_root_actor(
|
async with tractor.open_root_actor(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
):
|
):
|
||||||
async with tractor.get_arbiter(*arb_addr) as aportal:
|
async with tractor.get_arbiter(*reg_addr) as aportal:
|
||||||
try:
|
try:
|
||||||
get_reg = partial(unpack_reg, aportal)
|
get_reg = partial(unpack_reg, aportal)
|
||||||
|
|
||||||
|
@ -328,7 +350,7 @@ async def close_chans_before_nursery(
|
||||||
def test_close_channel_explicit(
|
def test_close_channel_explicit(
|
||||||
start_method,
|
start_method,
|
||||||
use_signal,
|
use_signal,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
):
|
):
|
||||||
"""Verify that closing a stream explicitly and killing the actor's
|
"""Verify that closing a stream explicitly and killing the actor's
|
||||||
"root nursery" **before** the containing nursery tears down also
|
"root nursery" **before** the containing nursery tears down also
|
||||||
|
@ -338,7 +360,7 @@ def test_close_channel_explicit(
|
||||||
trio.run(
|
trio.run(
|
||||||
partial(
|
partial(
|
||||||
close_chans_before_nursery,
|
close_chans_before_nursery,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
use_signal,
|
use_signal,
|
||||||
remote_arbiter=False,
|
remote_arbiter=False,
|
||||||
),
|
),
|
||||||
|
@ -350,7 +372,7 @@ def test_close_channel_explicit_remote_arbiter(
|
||||||
daemon,
|
daemon,
|
||||||
start_method,
|
start_method,
|
||||||
use_signal,
|
use_signal,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
):
|
):
|
||||||
"""Verify that closing a stream explicitly and killing the actor's
|
"""Verify that closing a stream explicitly and killing the actor's
|
||||||
"root nursery" **before** the containing nursery tears down also
|
"root nursery" **before** the containing nursery tears down also
|
||||||
|
@ -360,7 +382,7 @@ def test_close_channel_explicit_remote_arbiter(
|
||||||
trio.run(
|
trio.run(
|
||||||
partial(
|
partial(
|
||||||
close_chans_before_nursery,
|
close_chans_before_nursery,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
use_signal,
|
use_signal,
|
||||||
remote_arbiter=True,
|
remote_arbiter=True,
|
||||||
),
|
),
|
||||||
|
|
|
@ -20,7 +20,7 @@ from tractor._testing import (
|
||||||
def run_example_in_subproc(
|
def run_example_in_subproc(
|
||||||
loglevel: str,
|
loglevel: str,
|
||||||
testdir,
|
testdir,
|
||||||
arb_addr: tuple[str, int],
|
reg_addr: tuple[str, int],
|
||||||
):
|
):
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
|
|
|
@ -47,7 +47,7 @@ async def trio_cancels_single_aio_task():
|
||||||
await tractor.to_asyncio.run_task(sleep_forever)
|
await tractor.to_asyncio.run_task(sleep_forever)
|
||||||
|
|
||||||
|
|
||||||
def test_trio_cancels_aio_on_actor_side(arb_addr):
|
def test_trio_cancels_aio_on_actor_side(reg_addr):
|
||||||
'''
|
'''
|
||||||
Spawn an infected actor that is cancelled by the ``trio`` side
|
Spawn an infected actor that is cancelled by the ``trio`` side
|
||||||
task using std cancel scope apis.
|
task using std cancel scope apis.
|
||||||
|
@ -55,7 +55,7 @@ def test_trio_cancels_aio_on_actor_side(arb_addr):
|
||||||
'''
|
'''
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr
|
registry_addrs=[reg_addr]
|
||||||
) as n:
|
) as n:
|
||||||
await n.run_in_actor(
|
await n.run_in_actor(
|
||||||
trio_cancels_single_aio_task,
|
trio_cancels_single_aio_task,
|
||||||
|
@ -94,7 +94,7 @@ async def asyncio_actor(
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def test_aio_simple_error(arb_addr):
|
def test_aio_simple_error(reg_addr):
|
||||||
'''
|
'''
|
||||||
Verify a simple remote asyncio error propagates back through trio
|
Verify a simple remote asyncio error propagates back through trio
|
||||||
to the parent actor.
|
to the parent actor.
|
||||||
|
@ -103,7 +103,7 @@ def test_aio_simple_error(arb_addr):
|
||||||
'''
|
'''
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr
|
registry_addrs=[reg_addr]
|
||||||
) as n:
|
) as n:
|
||||||
await n.run_in_actor(
|
await n.run_in_actor(
|
||||||
asyncio_actor,
|
asyncio_actor,
|
||||||
|
@ -128,10 +128,10 @@ def test_aio_simple_error(arb_addr):
|
||||||
assert err
|
assert err
|
||||||
|
|
||||||
assert isinstance(err, RemoteActorError)
|
assert isinstance(err, RemoteActorError)
|
||||||
assert err.type == AssertionError
|
assert err.boxed_type == AssertionError
|
||||||
|
|
||||||
|
|
||||||
def test_tractor_cancels_aio(arb_addr):
|
def test_tractor_cancels_aio(reg_addr):
|
||||||
'''
|
'''
|
||||||
Verify we can cancel a spawned asyncio task gracefully.
|
Verify we can cancel a spawned asyncio task gracefully.
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ def test_tractor_cancels_aio(arb_addr):
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
||||||
|
|
||||||
def test_trio_cancels_aio(arb_addr):
|
def test_trio_cancels_aio(reg_addr):
|
||||||
'''
|
'''
|
||||||
Much like the above test with ``tractor.Portal.cancel_actor()``
|
Much like the above test with ``tractor.Portal.cancel_actor()``
|
||||||
except we just use a standard ``trio`` cancellation api.
|
except we just use a standard ``trio`` cancellation api.
|
||||||
|
@ -206,7 +206,7 @@ async def trio_ctx(
|
||||||
ids='parent_actor_cancels_child={}'.format
|
ids='parent_actor_cancels_child={}'.format
|
||||||
)
|
)
|
||||||
def test_context_spawns_aio_task_that_errors(
|
def test_context_spawns_aio_task_that_errors(
|
||||||
arb_addr,
|
reg_addr,
|
||||||
parent_cancels: bool,
|
parent_cancels: bool,
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
|
@ -272,7 +272,7 @@ def test_context_spawns_aio_task_that_errors(
|
||||||
|
|
||||||
err = excinfo.value
|
err = excinfo.value
|
||||||
assert isinstance(err, expect)
|
assert isinstance(err, expect)
|
||||||
assert err.type == AssertionError
|
assert err.boxed_type == AssertionError
|
||||||
|
|
||||||
|
|
||||||
async def aio_cancel():
|
async def aio_cancel():
|
||||||
|
@ -288,7 +288,7 @@ async def aio_cancel():
|
||||||
await sleep_forever()
|
await sleep_forever()
|
||||||
|
|
||||||
|
|
||||||
def test_aio_cancelled_from_aio_causes_trio_cancelled(arb_addr):
|
def test_aio_cancelled_from_aio_causes_trio_cancelled(reg_addr):
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery() as n:
|
async with tractor.open_nursery() as n:
|
||||||
|
@ -314,7 +314,7 @@ def test_aio_cancelled_from_aio_causes_trio_cancelled(arb_addr):
|
||||||
assert err
|
assert err
|
||||||
|
|
||||||
# ensure boxed error is correct
|
# ensure boxed error is correct
|
||||||
assert err.type == to_asyncio.AsyncioCancelled
|
assert err.boxed_type == to_asyncio.AsyncioCancelled
|
||||||
|
|
||||||
|
|
||||||
# TODO: verify open_channel_from will fail on this..
|
# TODO: verify open_channel_from will fail on this..
|
||||||
|
@ -436,7 +436,7 @@ async def stream_from_aio(
|
||||||
'fan_out', [False, True],
|
'fan_out', [False, True],
|
||||||
ids='fan_out_w_chan_subscribe={}'.format
|
ids='fan_out_w_chan_subscribe={}'.format
|
||||||
)
|
)
|
||||||
def test_basic_interloop_channel_stream(arb_addr, fan_out):
|
def test_basic_interloop_channel_stream(reg_addr, fan_out):
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery() as n:
|
async with tractor.open_nursery() as n:
|
||||||
portal = await n.run_in_actor(
|
portal = await n.run_in_actor(
|
||||||
|
@ -450,7 +450,7 @@ def test_basic_interloop_channel_stream(arb_addr, fan_out):
|
||||||
|
|
||||||
|
|
||||||
# TODO: parametrize the above test and avoid the duplication here?
|
# TODO: parametrize the above test and avoid the duplication here?
|
||||||
def test_trio_error_cancels_intertask_chan(arb_addr):
|
def test_trio_error_cancels_intertask_chan(reg_addr):
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery() as n:
|
async with tractor.open_nursery() as n:
|
||||||
portal = await n.run_in_actor(
|
portal = await n.run_in_actor(
|
||||||
|
@ -466,10 +466,10 @@ def test_trio_error_cancels_intertask_chan(arb_addr):
|
||||||
|
|
||||||
# ensure boxed errors
|
# ensure boxed errors
|
||||||
for exc in excinfo.value.exceptions:
|
for exc in excinfo.value.exceptions:
|
||||||
assert exc.type == Exception
|
assert exc.boxed_type == Exception
|
||||||
|
|
||||||
|
|
||||||
def test_trio_closes_early_and_channel_exits(arb_addr):
|
def test_trio_closes_early_and_channel_exits(reg_addr):
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery() as n:
|
async with tractor.open_nursery() as n:
|
||||||
portal = await n.run_in_actor(
|
portal = await n.run_in_actor(
|
||||||
|
@ -484,7 +484,7 @@ def test_trio_closes_early_and_channel_exits(arb_addr):
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
||||||
|
|
||||||
def test_aio_errors_and_channel_propagates_and_closes(arb_addr):
|
def test_aio_errors_and_channel_propagates_and_closes(reg_addr):
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery() as n:
|
async with tractor.open_nursery() as n:
|
||||||
portal = await n.run_in_actor(
|
portal = await n.run_in_actor(
|
||||||
|
@ -500,7 +500,7 @@ def test_aio_errors_and_channel_propagates_and_closes(arb_addr):
|
||||||
|
|
||||||
# ensure boxed errors
|
# ensure boxed errors
|
||||||
for exc in excinfo.value.exceptions:
|
for exc in excinfo.value.exceptions:
|
||||||
assert exc.type == Exception
|
assert exc.boxed_type == Exception
|
||||||
|
|
||||||
|
|
||||||
@tractor.context
|
@tractor.context
|
||||||
|
@ -561,7 +561,7 @@ async def trio_to_aio_echo_server(
|
||||||
ids='raise_error={}'.format,
|
ids='raise_error={}'.format,
|
||||||
)
|
)
|
||||||
def test_echoserver_detailed_mechanics(
|
def test_echoserver_detailed_mechanics(
|
||||||
arb_addr,
|
reg_addr,
|
||||||
raise_error_mid_stream,
|
raise_error_mid_stream,
|
||||||
):
|
):
|
||||||
|
|
||||||
|
@ -601,7 +601,8 @@ def test_echoserver_detailed_mechanics(
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
pytest.fail(
|
pytest.fail(
|
||||||
"stream wasn't stopped after sentinel?!")
|
'stream not stopped after sentinel ?!'
|
||||||
|
)
|
||||||
|
|
||||||
# TODO: the case where this blocks and
|
# TODO: the case where this blocks and
|
||||||
# is cancelled by kbi or out of task cancellation
|
# is cancelled by kbi or out of task cancellation
|
||||||
|
@ -613,3 +614,37 @@ def test_echoserver_detailed_mechanics(
|
||||||
|
|
||||||
else:
|
else:
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
||||||
|
|
||||||
|
# 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!')
|
||||||
|
|
|
@ -16,6 +16,11 @@ from tractor import ( # typing
|
||||||
Portal,
|
Portal,
|
||||||
Context,
|
Context,
|
||||||
ContextCancelled,
|
ContextCancelled,
|
||||||
|
RemoteActorError,
|
||||||
|
)
|
||||||
|
from tractor._testing import (
|
||||||
|
# tractor_test,
|
||||||
|
expect_ctxc,
|
||||||
)
|
)
|
||||||
|
|
||||||
# XXX TODO cases:
|
# XXX TODO cases:
|
||||||
|
@ -156,10 +161,11 @@ def test_do_not_swallow_error_before_started_by_remote_contextcancelled(
|
||||||
):
|
):
|
||||||
await trio.sleep_forever()
|
await trio.sleep_forever()
|
||||||
|
|
||||||
with pytest.raises(tractor.RemoteActorError) as excinfo:
|
with pytest.raises(RemoteActorError) as excinfo:
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
||||||
assert excinfo.value.type == TypeError
|
rae = excinfo.value
|
||||||
|
assert rae.boxed_type == TypeError
|
||||||
|
|
||||||
|
|
||||||
@tractor.context
|
@tractor.context
|
||||||
|
@ -739,14 +745,16 @@ def test_peer_canceller(
|
||||||
with pytest.raises(ContextCancelled) as excinfo:
|
with pytest.raises(ContextCancelled) as excinfo:
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
||||||
assert excinfo.value.type == ContextCancelled
|
assert excinfo.value.boxed_type == ContextCancelled
|
||||||
assert excinfo.value.canceller[0] == 'canceller'
|
assert excinfo.value.canceller[0] == 'canceller'
|
||||||
|
|
||||||
|
|
||||||
@tractor.context
|
@tractor.context
|
||||||
async def basic_echo_server(
|
async def basic_echo_server(
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
peer_name: str = 'stepbro',
|
peer_name: str = 'wittle_bruv',
|
||||||
|
|
||||||
|
err_after: int|None = None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
|
@ -774,17 +782,31 @@ async def basic_echo_server(
|
||||||
# assert 0
|
# assert 0
|
||||||
await ipc.send(resp)
|
await ipc.send(resp)
|
||||||
|
|
||||||
|
if (
|
||||||
|
err_after
|
||||||
|
and i > err_after
|
||||||
|
):
|
||||||
|
raise RuntimeError(
|
||||||
|
f'Simulated error in `{peer_name}`'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@tractor.context
|
@tractor.context
|
||||||
async def serve_subactors(
|
async def serve_subactors(
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
peer_name: str,
|
peer_name: str,
|
||||||
|
debug_mode: bool,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
async with open_nursery() as an:
|
async with open_nursery() as an:
|
||||||
|
|
||||||
|
# sanity
|
||||||
|
if debug_mode:
|
||||||
|
assert tractor._state.debug_mode()
|
||||||
|
|
||||||
await ctx.started(peer_name)
|
await ctx.started(peer_name)
|
||||||
async with ctx.open_stream() as reqs:
|
async with ctx.open_stream() as ipc:
|
||||||
async for msg in reqs:
|
async for msg in ipc:
|
||||||
peer_name: str = msg
|
peer_name: str = msg
|
||||||
peer: Portal = await an.start_actor(
|
peer: Portal = await an.start_actor(
|
||||||
name=peer_name,
|
name=peer_name,
|
||||||
|
@ -795,7 +817,7 @@ async def serve_subactors(
|
||||||
f'{peer_name}\n'
|
f'{peer_name}\n'
|
||||||
f'|_{peer}\n'
|
f'|_{peer}\n'
|
||||||
)
|
)
|
||||||
await reqs.send((
|
await ipc.send((
|
||||||
peer.chan.uid,
|
peer.chan.uid,
|
||||||
peer.chan.raddr,
|
peer.chan.raddr,
|
||||||
))
|
))
|
||||||
|
@ -807,14 +829,20 @@ async def serve_subactors(
|
||||||
async def client_req_subactor(
|
async def client_req_subactor(
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
peer_name: str,
|
peer_name: str,
|
||||||
|
debug_mode: bool,
|
||||||
|
|
||||||
# used to simulate a user causing an error to be raised
|
# used to simulate a user causing an error to be raised
|
||||||
# directly in thread (like a KBI) to better replicate the
|
# directly in thread (like a KBI) to better replicate the
|
||||||
# case where a `modden` CLI client would hang afer requesting
|
# case where a `modden` CLI client would hang afer requesting
|
||||||
# a `Context.cancel()` to `bigd`'s wks spawner.
|
# a `Context.cancel()` to `bigd`'s wks spawner.
|
||||||
reraise_on_cancel: str|None = None,
|
reraise_on_cancel: str|None = None,
|
||||||
|
sub_err_after: int|None = None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
# sanity
|
||||||
|
if debug_mode:
|
||||||
|
assert tractor._state.debug_mode()
|
||||||
|
|
||||||
# TODO: other cases to do with sub lifetimes:
|
# TODO: other cases to do with sub lifetimes:
|
||||||
# -[ ] test that we can have the server spawn a sub
|
# -[ ] test that we can have the server spawn a sub
|
||||||
# that lives longer then ctx with this client.
|
# that lives longer then ctx with this client.
|
||||||
|
@ -836,6 +864,7 @@ async def client_req_subactor(
|
||||||
spawner.open_context(
|
spawner.open_context(
|
||||||
serve_subactors,
|
serve_subactors,
|
||||||
peer_name=peer_name,
|
peer_name=peer_name,
|
||||||
|
debug_mode=debug_mode,
|
||||||
) as (spawner_ctx, first),
|
) as (spawner_ctx, first),
|
||||||
):
|
):
|
||||||
assert first == peer_name
|
assert first == peer_name
|
||||||
|
@ -857,6 +886,7 @@ async def client_req_subactor(
|
||||||
await tell_little_bro(
|
await tell_little_bro(
|
||||||
actor_name=sub_uid[0],
|
actor_name=sub_uid[0],
|
||||||
caller='client',
|
caller='client',
|
||||||
|
err_after=sub_err_after,
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO: test different scope-layers of
|
# TODO: test different scope-layers of
|
||||||
|
@ -868,9 +898,7 @@ async def client_req_subactor(
|
||||||
# TODO: would be super nice to have a special injected
|
# TODO: would be super nice to have a special injected
|
||||||
# cancel type here (maybe just our ctxc) but using
|
# cancel type here (maybe just our ctxc) but using
|
||||||
# some native mechanism in `trio` :p
|
# some native mechanism in `trio` :p
|
||||||
except (
|
except trio.Cancelled as err:
|
||||||
trio.Cancelled
|
|
||||||
) as err:
|
|
||||||
_err = err
|
_err = err
|
||||||
if reraise_on_cancel:
|
if reraise_on_cancel:
|
||||||
errtype = globals()['__builtins__'][reraise_on_cancel]
|
errtype = globals()['__builtins__'][reraise_on_cancel]
|
||||||
|
@ -897,7 +925,9 @@ async def client_req_subactor(
|
||||||
|
|
||||||
async def tell_little_bro(
|
async def tell_little_bro(
|
||||||
actor_name: str,
|
actor_name: str,
|
||||||
caller: str = ''
|
|
||||||
|
caller: str = '',
|
||||||
|
err_after: int|None = None,
|
||||||
):
|
):
|
||||||
# contact target actor, do a stream dialog.
|
# contact target actor, do a stream dialog.
|
||||||
async with (
|
async with (
|
||||||
|
@ -906,10 +936,12 @@ async def tell_little_bro(
|
||||||
) as lb,
|
) as lb,
|
||||||
lb.open_context(
|
lb.open_context(
|
||||||
basic_echo_server,
|
basic_echo_server,
|
||||||
|
|
||||||
|
# XXX proxy any delayed err condition
|
||||||
|
err_after=err_after,
|
||||||
) as (sub_ctx, first),
|
) as (sub_ctx, first),
|
||||||
sub_ctx.open_stream(
|
|
||||||
basic_echo_server,
|
sub_ctx.open_stream() as echo_ipc,
|
||||||
) as echo_ipc,
|
|
||||||
):
|
):
|
||||||
actor: Actor = current_actor()
|
actor: Actor = current_actor()
|
||||||
uid: tuple = actor.uid
|
uid: tuple = actor.uid
|
||||||
|
@ -936,9 +968,15 @@ async def tell_little_bro(
|
||||||
'raise_client_error',
|
'raise_client_error',
|
||||||
[None, 'KeyboardInterrupt'],
|
[None, 'KeyboardInterrupt'],
|
||||||
)
|
)
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'raise_sub_spawn_error_after',
|
||||||
|
[None, 50],
|
||||||
|
)
|
||||||
def test_peer_spawns_and_cancels_service_subactor(
|
def test_peer_spawns_and_cancels_service_subactor(
|
||||||
debug_mode: bool,
|
debug_mode: bool,
|
||||||
raise_client_error: str,
|
raise_client_error: str,
|
||||||
|
reg_addr: tuple[str, int],
|
||||||
|
raise_sub_spawn_error_after: int|None,
|
||||||
):
|
):
|
||||||
# NOTE: this tests for the modden `mod wks open piker` bug
|
# NOTE: this tests for the modden `mod wks open piker` bug
|
||||||
# discovered as part of implementing workspace ctx
|
# discovered as part of implementing workspace ctx
|
||||||
|
@ -952,10 +990,21 @@ def test_peer_spawns_and_cancels_service_subactor(
|
||||||
# and the server's spawned child should cancel and terminate!
|
# and the server's spawned child should cancel and terminate!
|
||||||
peer_name: str = 'little_bro'
|
peer_name: str = 'little_bro'
|
||||||
|
|
||||||
|
def check_inner_rte(rae: RemoteActorError):
|
||||||
|
'''
|
||||||
|
Validate the little_bro's relayed inception!
|
||||||
|
|
||||||
|
'''
|
||||||
|
assert rae.boxed_type is RemoteActorError
|
||||||
|
assert rae.src_type is RuntimeError
|
||||||
|
assert 'client' in rae.relay_uid
|
||||||
|
assert peer_name in rae.src_uid
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
# NOTE: to halt the peer tasks on ctxc, uncomment this.
|
# NOTE: to halt the peer tasks on ctxc, uncomment this.
|
||||||
debug_mode=debug_mode,
|
debug_mode=debug_mode,
|
||||||
|
registry_addrs=[reg_addr],
|
||||||
) as an:
|
) as an:
|
||||||
server: Portal = await an.start_actor(
|
server: Portal = await an.start_actor(
|
||||||
(server_name := 'spawn_server'),
|
(server_name := 'spawn_server'),
|
||||||
|
@ -974,14 +1023,24 @@ def test_peer_spawns_and_cancels_service_subactor(
|
||||||
server.open_context(
|
server.open_context(
|
||||||
serve_subactors,
|
serve_subactors,
|
||||||
peer_name=peer_name,
|
peer_name=peer_name,
|
||||||
|
debug_mode=debug_mode,
|
||||||
|
|
||||||
) as (spawn_ctx, first),
|
) as (spawn_ctx, first),
|
||||||
|
|
||||||
client.open_context(
|
client.open_context(
|
||||||
client_req_subactor,
|
client_req_subactor,
|
||||||
peer_name=peer_name,
|
peer_name=peer_name,
|
||||||
|
debug_mode=debug_mode,
|
||||||
reraise_on_cancel=raise_client_error,
|
reraise_on_cancel=raise_client_error,
|
||||||
|
|
||||||
|
# trigger for error condition in sub
|
||||||
|
# during streaming.
|
||||||
|
sub_err_after=raise_sub_spawn_error_after,
|
||||||
|
|
||||||
) as (client_ctx, client_says),
|
) as (client_ctx, client_says),
|
||||||
):
|
):
|
||||||
|
root: Actor = current_actor()
|
||||||
|
spawner_uid: tuple = spawn_ctx.chan.uid
|
||||||
print(
|
print(
|
||||||
f'Server says: {first}\n'
|
f'Server says: {first}\n'
|
||||||
f'Client says: {client_says}\n'
|
f'Client says: {client_says}\n'
|
||||||
|
@ -991,6 +1050,7 @@ def test_peer_spawns_and_cancels_service_subactor(
|
||||||
# (grandchild of this root actor) "little_bro"
|
# (grandchild of this root actor) "little_bro"
|
||||||
# and ensure we can also use it as an echo
|
# and ensure we can also use it as an echo
|
||||||
# server.
|
# server.
|
||||||
|
sub: Portal
|
||||||
async with tractor.wait_for_actor(
|
async with tractor.wait_for_actor(
|
||||||
name=peer_name,
|
name=peer_name,
|
||||||
) as sub:
|
) as sub:
|
||||||
|
@ -1002,56 +1062,139 @@ def test_peer_spawns_and_cancels_service_subactor(
|
||||||
f'.uid: {sub.actor.uid}\n'
|
f'.uid: {sub.actor.uid}\n'
|
||||||
f'chan.raddr: {sub.chan.raddr}\n'
|
f'chan.raddr: {sub.chan.raddr}\n'
|
||||||
)
|
)
|
||||||
await tell_little_bro(
|
|
||||||
actor_name=peer_name,
|
|
||||||
caller='root',
|
|
||||||
)
|
|
||||||
|
|
||||||
# signal client to raise a KBI
|
async with expect_ctxc(
|
||||||
await client_ctx.cancel()
|
yay=raise_sub_spawn_error_after,
|
||||||
print('root cancelled client, checking that sub-spawn is down')
|
reraise=False,
|
||||||
|
):
|
||||||
|
await tell_little_bro(
|
||||||
|
actor_name=peer_name,
|
||||||
|
caller='root',
|
||||||
|
)
|
||||||
|
|
||||||
async with tractor.find_actor(
|
if not raise_sub_spawn_error_after:
|
||||||
name=peer_name,
|
|
||||||
) as sub:
|
|
||||||
assert not sub
|
|
||||||
|
|
||||||
print('root cancelling server/client sub-actors')
|
# signal client to cancel and maybe raise a KBI
|
||||||
|
await client_ctx.cancel()
|
||||||
|
print(
|
||||||
|
'-> root cancelling client,\n'
|
||||||
|
'-> root checking `client_ctx.result()`,\n'
|
||||||
|
f'-> checking that sub-spawn {peer_name} is down\n'
|
||||||
|
)
|
||||||
|
# else:
|
||||||
|
|
||||||
# await tractor.pause()
|
try:
|
||||||
res = await client_ctx.result(hide_tb=False)
|
res = await client_ctx.result(hide_tb=False)
|
||||||
assert isinstance(res, ContextCancelled)
|
|
||||||
assert client_ctx.cancel_acked
|
# in remote (relayed inception) error
|
||||||
assert res.canceller == current_actor().uid
|
# case, we should error on the line above!
|
||||||
|
if raise_sub_spawn_error_after:
|
||||||
|
pytest.fail(
|
||||||
|
'Never rxed proxied `RemoteActorError[RuntimeError]` !?'
|
||||||
|
)
|
||||||
|
|
||||||
|
assert isinstance(res, ContextCancelled)
|
||||||
|
assert client_ctx.cancel_acked
|
||||||
|
assert res.canceller == root.uid
|
||||||
|
|
||||||
|
except RemoteActorError as rae:
|
||||||
|
_err = rae
|
||||||
|
assert raise_sub_spawn_error_after
|
||||||
|
|
||||||
|
# since this is a "relayed error" via the client
|
||||||
|
# sub-actor, it is expected to be
|
||||||
|
# a `RemoteActorError` boxing another
|
||||||
|
# `RemoteActorError` otherwise known as
|
||||||
|
# an "inception" (from `trio`'s parlance)
|
||||||
|
# ((or maybe a "Matryoshka" and/or "matron"
|
||||||
|
# in our own working parlance)) which
|
||||||
|
# contains the source error from the
|
||||||
|
# little_bro: a `RuntimeError`.
|
||||||
|
#
|
||||||
|
check_inner_rte(rae)
|
||||||
|
assert rae.relay_uid == client.chan.uid
|
||||||
|
assert rae.src_uid == sub.chan.uid
|
||||||
|
|
||||||
|
assert not client_ctx.cancel_acked
|
||||||
|
assert (
|
||||||
|
client_ctx.maybe_error
|
||||||
|
is client_ctx.outcome
|
||||||
|
is rae
|
||||||
|
)
|
||||||
|
raise
|
||||||
|
# await tractor.pause()
|
||||||
|
|
||||||
|
else:
|
||||||
|
assert not raise_sub_spawn_error_after
|
||||||
|
|
||||||
|
# cancelling the spawner sub should
|
||||||
|
# transitively cancel it's sub, the little
|
||||||
|
# bruv.
|
||||||
|
print('root cancelling server/client sub-actors')
|
||||||
|
await spawn_ctx.cancel()
|
||||||
|
async with tractor.find_actor(
|
||||||
|
name=peer_name,
|
||||||
|
) as sub:
|
||||||
|
assert not sub
|
||||||
|
|
||||||
await spawn_ctx.cancel()
|
|
||||||
# await server.cancel_actor()
|
# await server.cancel_actor()
|
||||||
|
|
||||||
|
except RemoteActorError as rae:
|
||||||
|
# XXX more-or-less same as above handler
|
||||||
|
# this is just making sure the error bubbles out
|
||||||
|
# of the
|
||||||
|
_err = rae
|
||||||
|
assert raise_sub_spawn_error_after
|
||||||
|
raise
|
||||||
|
|
||||||
# since we called `.cancel_actor()`, `.cancel_ack`
|
# since we called `.cancel_actor()`, `.cancel_ack`
|
||||||
# will not be set on the ctx bc `ctx.cancel()` was not
|
# will not be set on the ctx bc `ctx.cancel()` was not
|
||||||
# called directly fot this confext.
|
# called directly fot this confext.
|
||||||
except ContextCancelled as ctxc:
|
except ContextCancelled as ctxc:
|
||||||
print('caught ctxc from contexts!')
|
_ctxc = ctxc
|
||||||
assert ctxc.canceller == current_actor().uid
|
print(
|
||||||
|
f'{root.uid} caught ctxc from ctx with {client_ctx.chan.uid}\n'
|
||||||
|
f'{repr(ctxc)}\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
if not raise_sub_spawn_error_after:
|
||||||
|
assert ctxc.canceller == root.uid
|
||||||
|
else:
|
||||||
|
assert ctxc.canceller == spawner_uid
|
||||||
|
|
||||||
assert ctxc is spawn_ctx.outcome
|
assert ctxc is spawn_ctx.outcome
|
||||||
assert ctxc is spawn_ctx.maybe_error
|
assert ctxc is spawn_ctx.maybe_error
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# assert spawn_ctx.cancel_acked
|
if raise_sub_spawn_error_after:
|
||||||
assert spawn_ctx.cancel_acked
|
pytest.fail(
|
||||||
assert client_ctx.cancel_acked
|
'context block(s) in PARENT never raised?!?'
|
||||||
|
)
|
||||||
|
|
||||||
await client.cancel_actor()
|
if not raise_sub_spawn_error_after:
|
||||||
await server.cancel_actor()
|
# assert spawn_ctx.cancel_acked
|
||||||
|
assert spawn_ctx.cancel_acked
|
||||||
|
assert client_ctx.cancel_acked
|
||||||
|
|
||||||
# WOA WOA WOA! we need this to close..!!!??
|
await client.cancel_actor()
|
||||||
# that's super bad XD
|
await server.cancel_actor()
|
||||||
|
|
||||||
# TODO: why isn't this working!?!?
|
# WOA WOA WOA! we need this to close..!!!??
|
||||||
# we're now outside the `.open_context()` block so
|
# that's super bad XD
|
||||||
# the internal `Context._scope: CancelScope` should be
|
|
||||||
# gracefully "closed" ;)
|
|
||||||
|
|
||||||
# assert spawn_ctx.cancelled_caught
|
# TODO: why isn't this working!?!?
|
||||||
|
# we're now outside the `.open_context()` block so
|
||||||
|
# the internal `Context._scope: CancelScope` should be
|
||||||
|
# gracefully "closed" ;)
|
||||||
|
|
||||||
trio.run(main)
|
# assert spawn_ctx.cancelled_caught
|
||||||
|
|
||||||
|
if raise_sub_spawn_error_after:
|
||||||
|
with pytest.raises(RemoteActorError) as excinfo:
|
||||||
|
trio.run(main)
|
||||||
|
|
||||||
|
rae: RemoteActorError = excinfo.value
|
||||||
|
check_inner_rte(rae)
|
||||||
|
|
||||||
|
else:
|
||||||
|
trio.run(main)
|
||||||
|
|
|
@ -58,7 +58,7 @@ async def context_stream(
|
||||||
|
|
||||||
|
|
||||||
async def stream_from_single_subactor(
|
async def stream_from_single_subactor(
|
||||||
arb_addr,
|
reg_addr,
|
||||||
start_method,
|
start_method,
|
||||||
stream_func,
|
stream_func,
|
||||||
):
|
):
|
||||||
|
@ -67,7 +67,7 @@ async def stream_from_single_subactor(
|
||||||
# only one per host address, spawns an actor if None
|
# only one per host address, spawns an actor if None
|
||||||
|
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
start_method=start_method,
|
start_method=start_method,
|
||||||
) as nursery:
|
) as nursery:
|
||||||
|
|
||||||
|
@ -118,13 +118,13 @@ async def stream_from_single_subactor(
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'stream_func', [async_gen_stream, context_stream]
|
'stream_func', [async_gen_stream, context_stream]
|
||||||
)
|
)
|
||||||
def test_stream_from_single_subactor(arb_addr, start_method, stream_func):
|
def test_stream_from_single_subactor(reg_addr, start_method, stream_func):
|
||||||
"""Verify streaming from a spawned async generator.
|
"""Verify streaming from a spawned async generator.
|
||||||
"""
|
"""
|
||||||
trio.run(
|
trio.run(
|
||||||
partial(
|
partial(
|
||||||
stream_from_single_subactor,
|
stream_from_single_subactor,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
start_method,
|
start_method,
|
||||||
stream_func=stream_func,
|
stream_func=stream_func,
|
||||||
),
|
),
|
||||||
|
@ -228,14 +228,14 @@ async def a_quadruple_example():
|
||||||
return result_stream
|
return result_stream
|
||||||
|
|
||||||
|
|
||||||
async def cancel_after(wait, arb_addr):
|
async def cancel_after(wait, reg_addr):
|
||||||
async with tractor.open_root_actor(arbiter_addr=arb_addr):
|
async with tractor.open_root_actor(registry_addrs=[reg_addr]):
|
||||||
with trio.move_on_after(wait):
|
with trio.move_on_after(wait):
|
||||||
return await a_quadruple_example()
|
return await a_quadruple_example()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='module')
|
@pytest.fixture(scope='module')
|
||||||
def time_quad_ex(arb_addr, ci_env, spawn_backend):
|
def time_quad_ex(reg_addr, ci_env, spawn_backend):
|
||||||
if spawn_backend == 'mp':
|
if spawn_backend == 'mp':
|
||||||
"""no idea but the mp *nix runs are flaking out here often...
|
"""no idea but the mp *nix runs are flaking out here often...
|
||||||
"""
|
"""
|
||||||
|
@ -243,7 +243,7 @@ def time_quad_ex(arb_addr, ci_env, spawn_backend):
|
||||||
|
|
||||||
timeout = 7 if platform.system() in ('Windows', 'Darwin') else 4
|
timeout = 7 if platform.system() in ('Windows', 'Darwin') else 4
|
||||||
start = time.time()
|
start = time.time()
|
||||||
results = trio.run(cancel_after, timeout, arb_addr)
|
results = trio.run(cancel_after, timeout, reg_addr)
|
||||||
diff = time.time() - start
|
diff = time.time() - start
|
||||||
assert results
|
assert results
|
||||||
return results, diff
|
return results, diff
|
||||||
|
@ -263,14 +263,14 @@ def test_a_quadruple_example(time_quad_ex, ci_env, spawn_backend):
|
||||||
list(map(lambda i: i/10, range(3, 9)))
|
list(map(lambda i: i/10, range(3, 9)))
|
||||||
)
|
)
|
||||||
def test_not_fast_enough_quad(
|
def test_not_fast_enough_quad(
|
||||||
arb_addr, time_quad_ex, cancel_delay, ci_env, spawn_backend
|
reg_addr, time_quad_ex, cancel_delay, ci_env, spawn_backend
|
||||||
):
|
):
|
||||||
"""Verify we can cancel midway through the quad example and all actors
|
"""Verify we can cancel midway through the quad example and all actors
|
||||||
cancel gracefully.
|
cancel gracefully.
|
||||||
"""
|
"""
|
||||||
results, diff = time_quad_ex
|
results, diff = time_quad_ex
|
||||||
delay = max(diff - cancel_delay, 0)
|
delay = max(diff - cancel_delay, 0)
|
||||||
results = trio.run(cancel_after, delay, arb_addr)
|
results = trio.run(cancel_after, delay, reg_addr)
|
||||||
system = platform.system()
|
system = platform.system()
|
||||||
if system in ('Windows', 'Darwin') and results is not None:
|
if system in ('Windows', 'Darwin') and results is not None:
|
||||||
# In CI envoirments it seems later runs are quicker then the first
|
# In CI envoirments it seems later runs are quicker then the first
|
||||||
|
@ -283,7 +283,7 @@ def test_not_fast_enough_quad(
|
||||||
|
|
||||||
@tractor_test
|
@tractor_test
|
||||||
async def test_respawn_consumer_task(
|
async def test_respawn_consumer_task(
|
||||||
arb_addr,
|
reg_addr,
|
||||||
spawn_backend,
|
spawn_backend,
|
||||||
loglevel,
|
loglevel,
|
||||||
):
|
):
|
||||||
|
|
|
@ -24,7 +24,7 @@ async def test_no_runtime():
|
||||||
|
|
||||||
|
|
||||||
@tractor_test
|
@tractor_test
|
||||||
async def test_self_is_registered(arb_addr):
|
async def test_self_is_registered(reg_addr):
|
||||||
"Verify waiting on the arbiter to register itself using the standard api."
|
"Verify waiting on the arbiter to register itself using the standard api."
|
||||||
actor = tractor.current_actor()
|
actor = tractor.current_actor()
|
||||||
assert actor.is_arbiter
|
assert actor.is_arbiter
|
||||||
|
@ -34,20 +34,20 @@ async def test_self_is_registered(arb_addr):
|
||||||
|
|
||||||
|
|
||||||
@tractor_test
|
@tractor_test
|
||||||
async def test_self_is_registered_localportal(arb_addr):
|
async def test_self_is_registered_localportal(reg_addr):
|
||||||
"Verify waiting on the arbiter to register itself using a local portal."
|
"Verify waiting on the arbiter to register itself using a local portal."
|
||||||
actor = tractor.current_actor()
|
actor = tractor.current_actor()
|
||||||
assert actor.is_arbiter
|
assert actor.is_arbiter
|
||||||
async with tractor.get_arbiter(*arb_addr) as portal:
|
async with tractor.get_arbiter(*reg_addr) as portal:
|
||||||
assert isinstance(portal, tractor._portal.LocalPortal)
|
assert isinstance(portal, tractor._portal.LocalPortal)
|
||||||
|
|
||||||
with trio.fail_after(0.2):
|
with trio.fail_after(0.2):
|
||||||
sockaddr = await portal.run_from_ns(
|
sockaddr = await portal.run_from_ns(
|
||||||
'self', 'wait_for_actor', name='root')
|
'self', 'wait_for_actor', name='root')
|
||||||
assert sockaddr[0] == arb_addr
|
assert sockaddr[0] == reg_addr
|
||||||
|
|
||||||
|
|
||||||
def test_local_actor_async_func(arb_addr):
|
def test_local_actor_async_func(reg_addr):
|
||||||
"""Verify a simple async function in-process.
|
"""Verify a simple async function in-process.
|
||||||
"""
|
"""
|
||||||
nums = []
|
nums = []
|
||||||
|
@ -55,7 +55,7 @@ def test_local_actor_async_func(arb_addr):
|
||||||
async def print_loop():
|
async def print_loop():
|
||||||
|
|
||||||
async with tractor.open_root_actor(
|
async with tractor.open_root_actor(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
):
|
):
|
||||||
# arbiter is started in-proc if dne
|
# arbiter is started in-proc if dne
|
||||||
assert tractor.current_actor().is_arbiter
|
assert tractor.current_actor().is_arbiter
|
||||||
|
|
|
@ -30,9 +30,9 @@ def test_abort_on_sigint(daemon):
|
||||||
|
|
||||||
|
|
||||||
@tractor_test
|
@tractor_test
|
||||||
async def test_cancel_remote_arbiter(daemon, arb_addr):
|
async def test_cancel_remote_arbiter(daemon, reg_addr):
|
||||||
assert not tractor.current_actor().is_arbiter
|
assert not tractor.current_actor().is_arbiter
|
||||||
async with tractor.get_arbiter(*arb_addr) as portal:
|
async with tractor.get_arbiter(*reg_addr) as portal:
|
||||||
await portal.cancel_actor()
|
await portal.cancel_actor()
|
||||||
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
@ -41,16 +41,16 @@ async def test_cancel_remote_arbiter(daemon, arb_addr):
|
||||||
|
|
||||||
# no arbiter socket should exist
|
# no arbiter socket should exist
|
||||||
with pytest.raises(OSError):
|
with pytest.raises(OSError):
|
||||||
async with tractor.get_arbiter(*arb_addr) as portal:
|
async with tractor.get_arbiter(*reg_addr) as portal:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def test_register_duplicate_name(daemon, arb_addr):
|
def test_register_duplicate_name(daemon, reg_addr):
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
) as n:
|
) as n:
|
||||||
|
|
||||||
assert not tractor.current_actor().is_arbiter
|
assert not tractor.current_actor().is_arbiter
|
||||||
|
|
|
@ -159,7 +159,7 @@ async def test_required_args(callwith_expecterror):
|
||||||
)
|
)
|
||||||
def test_multi_actor_subs_arbiter_pub(
|
def test_multi_actor_subs_arbiter_pub(
|
||||||
loglevel,
|
loglevel,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
pub_actor,
|
pub_actor,
|
||||||
):
|
):
|
||||||
"""Try out the neato @pub decorator system.
|
"""Try out the neato @pub decorator system.
|
||||||
|
@ -169,7 +169,7 @@ def test_multi_actor_subs_arbiter_pub(
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
enable_modules=[__name__],
|
enable_modules=[__name__],
|
||||||
) as n:
|
) as n:
|
||||||
|
|
||||||
|
@ -254,12 +254,12 @@ def test_multi_actor_subs_arbiter_pub(
|
||||||
|
|
||||||
def test_single_subactor_pub_multitask_subs(
|
def test_single_subactor_pub_multitask_subs(
|
||||||
loglevel,
|
loglevel,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
):
|
):
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
enable_modules=[__name__],
|
enable_modules=[__name__],
|
||||||
) as n:
|
) as n:
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,19 @@ async def sleep_back_actor(
|
||||||
func_name,
|
func_name,
|
||||||
func_defined,
|
func_defined,
|
||||||
exposed_mods,
|
exposed_mods,
|
||||||
|
*,
|
||||||
|
reg_addr: tuple,
|
||||||
):
|
):
|
||||||
if actor_name:
|
if actor_name:
|
||||||
async with tractor.find_actor(actor_name) as portal:
|
async with tractor.find_actor(
|
||||||
|
actor_name,
|
||||||
|
# NOTE: must be set manually since
|
||||||
|
# the subactor doesn't have the reg_addr
|
||||||
|
# fixture code run in it!
|
||||||
|
# TODO: maybe we should just set this once in the
|
||||||
|
# _state mod and derive to all children?
|
||||||
|
registry_addrs=[reg_addr],
|
||||||
|
) as portal:
|
||||||
try:
|
try:
|
||||||
await portal.run(__name__, func_name)
|
await portal.run(__name__, func_name)
|
||||||
except tractor.RemoteActorError as err:
|
except tractor.RemoteActorError as err:
|
||||||
|
@ -26,7 +36,7 @@ async def sleep_back_actor(
|
||||||
if not exposed_mods:
|
if not exposed_mods:
|
||||||
expect = tractor.ModuleNotExposed
|
expect = tractor.ModuleNotExposed
|
||||||
|
|
||||||
assert err.type is expect
|
assert err.boxed_type is expect
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
await trio.sleep(float('inf'))
|
await trio.sleep(float('inf'))
|
||||||
|
@ -52,11 +62,17 @@ async def short_sleep():
|
||||||
'fail_on_syntax',
|
'fail_on_syntax',
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_rpc_errors(arb_addr, to_call, testdir):
|
def test_rpc_errors(
|
||||||
"""Test errors when making various RPC requests to an actor
|
reg_addr,
|
||||||
|
to_call,
|
||||||
|
testdir,
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Test errors when making various RPC requests to an actor
|
||||||
that either doesn't have the requested module exposed or doesn't define
|
that either doesn't have the requested module exposed or doesn't define
|
||||||
the named function.
|
the named function.
|
||||||
"""
|
|
||||||
|
'''
|
||||||
exposed_mods, funcname, inside_err = to_call
|
exposed_mods, funcname, inside_err = to_call
|
||||||
subactor_exposed_mods = []
|
subactor_exposed_mods = []
|
||||||
func_defined = globals().get(funcname, False)
|
func_defined = globals().get(funcname, False)
|
||||||
|
@ -84,8 +100,13 @@ def test_rpc_errors(arb_addr, to_call, testdir):
|
||||||
|
|
||||||
# spawn a subactor which calls us back
|
# spawn a subactor which calls us back
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
registry_addrs=[reg_addr],
|
||||||
enable_modules=exposed_mods.copy(),
|
enable_modules=exposed_mods.copy(),
|
||||||
|
|
||||||
|
# NOTE: will halt test in REPL if uncommented, so only
|
||||||
|
# do that if actually debugging subactor but keep it
|
||||||
|
# disabled for the test.
|
||||||
|
# debug_mode=True,
|
||||||
) as n:
|
) as n:
|
||||||
|
|
||||||
actor = tractor.current_actor()
|
actor = tractor.current_actor()
|
||||||
|
@ -102,6 +123,7 @@ def test_rpc_errors(arb_addr, to_call, testdir):
|
||||||
exposed_mods=exposed_mods,
|
exposed_mods=exposed_mods,
|
||||||
func_defined=True if func_defined else False,
|
func_defined=True if func_defined else False,
|
||||||
enable_modules=subactor_exposed_mods,
|
enable_modules=subactor_exposed_mods,
|
||||||
|
reg_addr=reg_addr,
|
||||||
)
|
)
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
|
@ -128,4 +150,4 @@ def test_rpc_errors(arb_addr, to_call, testdir):
|
||||||
))
|
))
|
||||||
|
|
||||||
if getattr(value, 'type', None):
|
if getattr(value, 'type', None):
|
||||||
assert value.type is inside_err
|
assert value.boxed_type is inside_err
|
||||||
|
|
|
@ -16,14 +16,14 @@ data_to_pass_down = {'doggy': 10, 'kitty': 4}
|
||||||
async def spawn(
|
async def spawn(
|
||||||
is_arbiter: bool,
|
is_arbiter: bool,
|
||||||
data: dict,
|
data: dict,
|
||||||
arb_addr: tuple[str, int],
|
reg_addr: tuple[str, int],
|
||||||
):
|
):
|
||||||
namespaces = [__name__]
|
namespaces = [__name__]
|
||||||
|
|
||||||
await trio.sleep(0.1)
|
await trio.sleep(0.1)
|
||||||
|
|
||||||
async with tractor.open_root_actor(
|
async with tractor.open_root_actor(
|
||||||
arbiter_addr=arb_addr,
|
arbiter_addr=reg_addr,
|
||||||
):
|
):
|
||||||
|
|
||||||
actor = tractor.current_actor()
|
actor = tractor.current_actor()
|
||||||
|
@ -32,8 +32,7 @@ async def spawn(
|
||||||
|
|
||||||
if actor.is_arbiter:
|
if actor.is_arbiter:
|
||||||
|
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery() as nursery:
|
||||||
) as nursery:
|
|
||||||
|
|
||||||
# forks here
|
# forks here
|
||||||
portal = await nursery.run_in_actor(
|
portal = await nursery.run_in_actor(
|
||||||
|
@ -41,7 +40,7 @@ async def spawn(
|
||||||
is_arbiter=False,
|
is_arbiter=False,
|
||||||
name='sub-actor',
|
name='sub-actor',
|
||||||
data=data,
|
data=data,
|
||||||
arb_addr=arb_addr,
|
reg_addr=reg_addr,
|
||||||
enable_modules=namespaces,
|
enable_modules=namespaces,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -55,12 +54,14 @@ async def spawn(
|
||||||
return 10
|
return 10
|
||||||
|
|
||||||
|
|
||||||
def test_local_arbiter_subactor_global_state(arb_addr):
|
def test_local_arbiter_subactor_global_state(
|
||||||
|
reg_addr,
|
||||||
|
):
|
||||||
result = trio.run(
|
result = trio.run(
|
||||||
spawn,
|
spawn,
|
||||||
True,
|
True,
|
||||||
data_to_pass_down,
|
data_to_pass_down,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
)
|
)
|
||||||
assert result == 10
|
assert result == 10
|
||||||
|
|
||||||
|
@ -140,7 +141,7 @@ async def check_loglevel(level):
|
||||||
def test_loglevel_propagated_to_subactor(
|
def test_loglevel_propagated_to_subactor(
|
||||||
start_method,
|
start_method,
|
||||||
capfd,
|
capfd,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
):
|
):
|
||||||
if start_method == 'mp_forkserver':
|
if start_method == 'mp_forkserver':
|
||||||
pytest.skip(
|
pytest.skip(
|
||||||
|
@ -152,7 +153,7 @@ def test_loglevel_propagated_to_subactor(
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
name='arbiter',
|
name='arbiter',
|
||||||
start_method=start_method,
|
start_method=start_method,
|
||||||
arbiter_addr=arb_addr,
|
arbiter_addr=reg_addr,
|
||||||
|
|
||||||
) as tn:
|
) as tn:
|
||||||
await tn.run_in_actor(
|
await tn.run_in_actor(
|
||||||
|
|
|
@ -66,13 +66,13 @@ async def ensure_sequence(
|
||||||
async def open_sequence_streamer(
|
async def open_sequence_streamer(
|
||||||
|
|
||||||
sequence: list[int],
|
sequence: list[int],
|
||||||
arb_addr: tuple[str, int],
|
reg_addr: tuple[str, int],
|
||||||
start_method: str,
|
start_method: str,
|
||||||
|
|
||||||
) -> tractor.MsgStream:
|
) -> tractor.MsgStream:
|
||||||
|
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=arb_addr,
|
arbiter_addr=reg_addr,
|
||||||
start_method=start_method,
|
start_method=start_method,
|
||||||
) as tn:
|
) as tn:
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ async def open_sequence_streamer(
|
||||||
|
|
||||||
|
|
||||||
def test_stream_fan_out_to_local_subscriptions(
|
def test_stream_fan_out_to_local_subscriptions(
|
||||||
arb_addr,
|
reg_addr,
|
||||||
start_method,
|
start_method,
|
||||||
):
|
):
|
||||||
|
|
||||||
|
@ -103,7 +103,7 @@ def test_stream_fan_out_to_local_subscriptions(
|
||||||
|
|
||||||
async with open_sequence_streamer(
|
async with open_sequence_streamer(
|
||||||
sequence,
|
sequence,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
start_method,
|
start_method,
|
||||||
) as stream:
|
) as stream:
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ def test_stream_fan_out_to_local_subscriptions(
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_consumer_and_parent_maybe_lag(
|
def test_consumer_and_parent_maybe_lag(
|
||||||
arb_addr,
|
reg_addr,
|
||||||
start_method,
|
start_method,
|
||||||
task_delays,
|
task_delays,
|
||||||
):
|
):
|
||||||
|
@ -150,7 +150,7 @@ def test_consumer_and_parent_maybe_lag(
|
||||||
|
|
||||||
async with open_sequence_streamer(
|
async with open_sequence_streamer(
|
||||||
sequence,
|
sequence,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
start_method,
|
start_method,
|
||||||
) as stream:
|
) as stream:
|
||||||
|
|
||||||
|
@ -211,7 +211,7 @@ def test_consumer_and_parent_maybe_lag(
|
||||||
|
|
||||||
|
|
||||||
def test_faster_task_to_recv_is_cancelled_by_slower(
|
def test_faster_task_to_recv_is_cancelled_by_slower(
|
||||||
arb_addr,
|
reg_addr,
|
||||||
start_method,
|
start_method,
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
|
@ -225,7 +225,7 @@ def test_faster_task_to_recv_is_cancelled_by_slower(
|
||||||
|
|
||||||
async with open_sequence_streamer(
|
async with open_sequence_streamer(
|
||||||
sequence,
|
sequence,
|
||||||
arb_addr,
|
reg_addr,
|
||||||
start_method,
|
start_method,
|
||||||
|
|
||||||
) as stream:
|
) as stream:
|
||||||
|
@ -302,7 +302,7 @@ def test_subscribe_errors_after_close():
|
||||||
|
|
||||||
|
|
||||||
def test_ensure_slow_consumers_lag_out(
|
def test_ensure_slow_consumers_lag_out(
|
||||||
arb_addr,
|
reg_addr,
|
||||||
start_method,
|
start_method,
|
||||||
):
|
):
|
||||||
'''This is a pure local task test; no tractor
|
'''This is a pure local task test; no tractor
|
||||||
|
|
|
@ -18,74 +18,48 @@
|
||||||
tractor: structured concurrent ``trio``-"actors".
|
tractor: structured concurrent ``trio``-"actors".
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from ._clustering import open_actor_cluster
|
|
||||||
|
from ._clustering import (
|
||||||
|
open_actor_cluster as open_actor_cluster,
|
||||||
|
)
|
||||||
from ._context import (
|
from ._context import (
|
||||||
Context, # the type
|
Context as Context, # the type
|
||||||
context, # a func-decorator
|
context as context, # a func-decorator
|
||||||
)
|
)
|
||||||
from ._streaming import (
|
from ._streaming import (
|
||||||
MsgStream,
|
MsgStream as MsgStream,
|
||||||
stream,
|
stream as stream,
|
||||||
)
|
)
|
||||||
from ._discovery import (
|
from ._discovery import (
|
||||||
get_arbiter,
|
get_arbiter as get_arbiter,
|
||||||
find_actor,
|
find_actor as find_actor,
|
||||||
wait_for_actor,
|
wait_for_actor as wait_for_actor,
|
||||||
query_actor,
|
query_actor as query_actor,
|
||||||
|
)
|
||||||
|
from ._supervise import (
|
||||||
|
open_nursery as open_nursery,
|
||||||
|
ActorNursery as ActorNursery,
|
||||||
)
|
)
|
||||||
from ._supervise import open_nursery
|
|
||||||
from ._state import (
|
from ._state import (
|
||||||
current_actor,
|
current_actor as current_actor,
|
||||||
is_root_process,
|
is_root_process as is_root_process,
|
||||||
)
|
)
|
||||||
from ._exceptions import (
|
from ._exceptions import (
|
||||||
RemoteActorError,
|
RemoteActorError as RemoteActorError,
|
||||||
ModuleNotExposed,
|
ModuleNotExposed as ModuleNotExposed,
|
||||||
ContextCancelled,
|
ContextCancelled as ContextCancelled,
|
||||||
)
|
)
|
||||||
from .devx import (
|
from .devx import (
|
||||||
breakpoint,
|
breakpoint as breakpoint,
|
||||||
pause,
|
pause as pause,
|
||||||
pause_from_sync,
|
pause_from_sync as pause_from_sync,
|
||||||
post_mortem,
|
post_mortem as post_mortem,
|
||||||
)
|
)
|
||||||
from . import msg
|
from . import msg as msg
|
||||||
from ._root import (
|
from ._root import (
|
||||||
run_daemon,
|
run_daemon as run_daemon,
|
||||||
open_root_actor,
|
open_root_actor as open_root_actor,
|
||||||
)
|
)
|
||||||
from ._ipc import Channel
|
from ._ipc import Channel as Channel
|
||||||
from ._portal import Portal
|
from ._portal import Portal as Portal
|
||||||
from ._runtime import Actor
|
from ._runtime import Actor as Actor
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'Actor',
|
|
||||||
'BaseExceptionGroup',
|
|
||||||
'Channel',
|
|
||||||
'Context',
|
|
||||||
'ContextCancelled',
|
|
||||||
'ModuleNotExposed',
|
|
||||||
'MsgStream',
|
|
||||||
'Portal',
|
|
||||||
'RemoteActorError',
|
|
||||||
'breakpoint',
|
|
||||||
'context',
|
|
||||||
'current_actor',
|
|
||||||
'find_actor',
|
|
||||||
'query_actor',
|
|
||||||
'get_arbiter',
|
|
||||||
'is_root_process',
|
|
||||||
'msg',
|
|
||||||
'open_actor_cluster',
|
|
||||||
'open_nursery',
|
|
||||||
'open_root_actor',
|
|
||||||
'pause',
|
|
||||||
'post_mortem',
|
|
||||||
'pause_from_sync',
|
|
||||||
'query_actor',
|
|
||||||
'run_daemon',
|
|
||||||
'stream',
|
|
||||||
'to_asyncio',
|
|
||||||
'wait_for_actor',
|
|
||||||
]
|
|
||||||
|
|
|
@ -36,6 +36,7 @@ def parse_ipaddr(arg):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
__tracebackhide__: bool = True
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--uid", type=parse_uid)
|
parser.add_argument("--uid", type=parse_uid)
|
||||||
|
|
|
@ -351,7 +351,7 @@ class Context:
|
||||||
by the runtime in 2 ways:
|
by the runtime in 2 ways:
|
||||||
- by entering ``Portal.open_context()`` which is the primary
|
- by entering ``Portal.open_context()`` which is the primary
|
||||||
public API for any "caller" task or,
|
public API for any "caller" task or,
|
||||||
- by the RPC machinery's `._runtime._invoke()` as a `ctx` arg
|
- by the RPC machinery's `._rpc._invoke()` as a `ctx` arg
|
||||||
to a remotely scheduled "callee" function.
|
to a remotely scheduled "callee" function.
|
||||||
|
|
||||||
AND is always constructed using the below ``mk_context()``.
|
AND is always constructed using the below ``mk_context()``.
|
||||||
|
@ -361,10 +361,10 @@ class Context:
|
||||||
`trio.Task`s. Contexts are allocated on each side of any task
|
`trio.Task`s. Contexts are allocated on each side of any task
|
||||||
RPC-linked msg dialog, i.e. for every request to a remote
|
RPC-linked msg dialog, i.e. for every request to a remote
|
||||||
actor from a `Portal`. On the "callee" side a context is
|
actor from a `Portal`. On the "callee" side a context is
|
||||||
always allocated inside ``._runtime._invoke()``.
|
always allocated inside ``._rpc._invoke()``.
|
||||||
|
|
||||||
# TODO: more detailed writeup on cancellation, error and
|
TODO: more detailed writeup on cancellation, error and
|
||||||
# streaming semantics..
|
streaming semantics..
|
||||||
|
|
||||||
A context can be cancelled and (possibly eventually restarted) from
|
A context can be cancelled and (possibly eventually restarted) from
|
||||||
either side of the underlying IPC channel, it can also open task
|
either side of the underlying IPC channel, it can also open task
|
||||||
|
@ -1209,7 +1209,9 @@ class Context:
|
||||||
# await pause()
|
# await pause()
|
||||||
log.warning(
|
log.warning(
|
||||||
'Stream was terminated by EoC\n\n'
|
'Stream was terminated by EoC\n\n'
|
||||||
f'{repr(eoc)}\n'
|
# NOTE: won't show the error <Type> but
|
||||||
|
# does show txt followed by IPC msg.
|
||||||
|
f'{str(eoc)}\n'
|
||||||
)
|
)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
@ -1306,7 +1308,7 @@ class Context:
|
||||||
# `._cancel_called == True`.
|
# `._cancel_called == True`.
|
||||||
not raise_overrun_from_self
|
not raise_overrun_from_self
|
||||||
and isinstance(remote_error, RemoteActorError)
|
and isinstance(remote_error, RemoteActorError)
|
||||||
and remote_error.msgdata['type_str'] == 'StreamOverrun'
|
and remote_error.msgdata['boxed_type_str'] == 'StreamOverrun'
|
||||||
and tuple(remote_error.msgdata['sender']) == our_uid
|
and tuple(remote_error.msgdata['sender']) == our_uid
|
||||||
):
|
):
|
||||||
# NOTE: we set the local scope error to any "self
|
# NOTE: we set the local scope error to any "self
|
||||||
|
@ -1883,6 +1885,19 @@ class Context:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: exception tb masking by using a manual
|
||||||
|
# `.__aexit__()`/.__aenter__()` pair on a type?
|
||||||
|
# => currently this is one of the few places we can't easily
|
||||||
|
# mask errors - on the exit side of a `Portal.open_context()`..
|
||||||
|
# there's # => currently this is one of the few places we can't
|
||||||
|
# there's 2 ways to approach it:
|
||||||
|
# - manually write an @acm type as per above
|
||||||
|
# - use `contextlib.AsyncContextDecorator` to override the default
|
||||||
|
# impl to suppress traceback frames:
|
||||||
|
# * https://docs.python.org/3/library/contextlib.html#contextlib.AsyncContextDecorator
|
||||||
|
# * https://docs.python.org/3/library/contextlib.html#contextlib.ContextDecorator
|
||||||
|
# - also we could just override directly the underlying
|
||||||
|
# `contextlib._AsyncGeneratorContextManager`?
|
||||||
@acm
|
@acm
|
||||||
async def open_context_from_portal(
|
async def open_context_from_portal(
|
||||||
portal: Portal,
|
portal: Portal,
|
||||||
|
|
|
@ -15,32 +15,45 @@
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Actor discovery API.
|
Discovery (protocols) API for automatic addressing and location
|
||||||
|
management of (service) actors.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
from typing import (
|
from typing import (
|
||||||
Optional,
|
|
||||||
Union,
|
|
||||||
AsyncGenerator,
|
AsyncGenerator,
|
||||||
|
AsyncContextManager,
|
||||||
|
TYPE_CHECKING,
|
||||||
)
|
)
|
||||||
from contextlib import asynccontextmanager as acm
|
from contextlib import asynccontextmanager as acm
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
from .trionics import gather_contexts
|
||||||
from ._ipc import _connect_chan, Channel
|
from ._ipc import _connect_chan, Channel
|
||||||
from ._portal import (
|
from ._portal import (
|
||||||
Portal,
|
Portal,
|
||||||
open_portal,
|
open_portal,
|
||||||
LocalPortal,
|
LocalPortal,
|
||||||
)
|
)
|
||||||
from ._state import current_actor, _runtime_vars
|
from ._state import (
|
||||||
|
current_actor,
|
||||||
|
_runtime_vars,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ._runtime import Actor
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def get_arbiter(
|
async def get_registry(
|
||||||
|
|
||||||
host: str,
|
host: str,
|
||||||
port: int,
|
port: int,
|
||||||
|
|
||||||
) -> AsyncGenerator[Union[Portal, LocalPortal], None]:
|
) -> AsyncGenerator[
|
||||||
|
Portal | LocalPortal | None,
|
||||||
|
None,
|
||||||
|
]:
|
||||||
'''
|
'''
|
||||||
Return a portal instance connected to a local or remote
|
Return a portal instance connected to a local or remote
|
||||||
arbiter.
|
arbiter.
|
||||||
|
@ -51,16 +64,33 @@ async def get_arbiter(
|
||||||
if not actor:
|
if not actor:
|
||||||
raise RuntimeError("No actor instance has been defined yet?")
|
raise RuntimeError("No actor instance has been defined yet?")
|
||||||
|
|
||||||
if actor.is_arbiter:
|
if actor.is_registrar:
|
||||||
# we're already the arbiter
|
# we're already the arbiter
|
||||||
# (likely a re-entrant call from the arbiter actor)
|
# (likely a re-entrant call from the arbiter actor)
|
||||||
yield LocalPortal(actor, Channel((host, port)))
|
yield LocalPortal(
|
||||||
|
actor,
|
||||||
|
Channel((host, port))
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
async with _connect_chan(host, port) as chan:
|
async with (
|
||||||
|
_connect_chan(host, port) as chan,
|
||||||
|
open_portal(chan) as regstr_ptl,
|
||||||
|
):
|
||||||
|
yield regstr_ptl
|
||||||
|
|
||||||
async with open_portal(chan) as arb_portal:
|
|
||||||
|
|
||||||
yield arb_portal
|
|
||||||
|
# TODO: deprecate and this remove _arbiter form!
|
||||||
|
@acm
|
||||||
|
async def get_arbiter(*args, **kwargs):
|
||||||
|
warnings.warn(
|
||||||
|
'`tractor.get_arbiter()` is now deprecated!\n'
|
||||||
|
'Use `.get_registry()` instead!',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
async with get_registry(*args, **kwargs) as to_yield:
|
||||||
|
yield to_yield
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
|
@ -68,51 +98,80 @@ async def get_root(
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> AsyncGenerator[Portal, None]:
|
) -> AsyncGenerator[Portal, None]:
|
||||||
|
|
||||||
|
# TODO: rename mailbox to `_root_maddr` when we finally
|
||||||
|
# add and impl libp2p multi-addrs?
|
||||||
host, port = _runtime_vars['_root_mailbox']
|
host, port = _runtime_vars['_root_mailbox']
|
||||||
assert host is not None
|
assert host is not None
|
||||||
|
|
||||||
async with _connect_chan(host, port) as chan:
|
async with (
|
||||||
async with open_portal(chan, **kwargs) as portal:
|
_connect_chan(host, port) as chan,
|
||||||
yield portal
|
open_portal(chan, **kwargs) as portal,
|
||||||
|
):
|
||||||
|
yield portal
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def query_actor(
|
async def query_actor(
|
||||||
name: str,
|
name: str,
|
||||||
arbiter_sockaddr: Optional[tuple[str, int]] = None,
|
arbiter_sockaddr: tuple[str, int] | None = None,
|
||||||
|
regaddr: tuple[str, int] | None = None,
|
||||||
|
|
||||||
) -> AsyncGenerator[tuple[str, int], None]:
|
) -> AsyncGenerator[
|
||||||
|
tuple[str, int] | None,
|
||||||
|
None,
|
||||||
|
]:
|
||||||
'''
|
'''
|
||||||
Simple address lookup for a given actor name.
|
Make a transport address lookup for an actor name to a specific
|
||||||
|
registrar.
|
||||||
|
|
||||||
Returns the (socket) address or ``None``.
|
Returns the (socket) address or ``None`` if no entry under that
|
||||||
|
name exists for the given registrar listening @ `regaddr`.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
actor = current_actor()
|
actor: Actor = current_actor()
|
||||||
async with get_arbiter(
|
if (
|
||||||
*arbiter_sockaddr or actor._arb_addr
|
name == 'registrar'
|
||||||
) as arb_portal:
|
and actor.is_registrar
|
||||||
|
):
|
||||||
|
raise RuntimeError(
|
||||||
|
'The current actor IS the registry!?'
|
||||||
|
)
|
||||||
|
|
||||||
sockaddr = await arb_portal.run_from_ns(
|
if arbiter_sockaddr is not None:
|
||||||
|
warnings.warn(
|
||||||
|
'`tractor.query_actor(regaddr=<blah>)` is deprecated.\n'
|
||||||
|
'Use `registry_addrs: list[tuple]` instead!',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
regaddr: list[tuple[str, int]] = arbiter_sockaddr
|
||||||
|
|
||||||
|
reg_portal: Portal
|
||||||
|
regaddr: tuple[str, int] = regaddr or actor.reg_addrs[0]
|
||||||
|
async with get_registry(*regaddr) as reg_portal:
|
||||||
|
# TODO: return portals to all available actors - for now
|
||||||
|
# just the last one that registered
|
||||||
|
sockaddr: tuple[str, int] = await reg_portal.run_from_ns(
|
||||||
'self',
|
'self',
|
||||||
'find_actor',
|
'find_actor',
|
||||||
name=name,
|
name=name,
|
||||||
)
|
)
|
||||||
|
yield sockaddr
|
||||||
# TODO: return portals to all available actors - for now just
|
|
||||||
# the last one that registered
|
|
||||||
if name == 'arbiter' and actor.is_arbiter:
|
|
||||||
raise RuntimeError("The current actor is the arbiter")
|
|
||||||
|
|
||||||
yield sockaddr if sockaddr else None
|
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def find_actor(
|
async def find_actor(
|
||||||
name: str,
|
name: str,
|
||||||
arbiter_sockaddr: tuple[str, int] | None = None
|
arbiter_sockaddr: tuple[str, int]|None = None,
|
||||||
|
registry_addrs: list[tuple[str, int]]|None = None,
|
||||||
|
|
||||||
) -> AsyncGenerator[Optional[Portal], None]:
|
only_first: bool = True,
|
||||||
|
raise_on_none: bool = False,
|
||||||
|
|
||||||
|
) -> AsyncGenerator[
|
||||||
|
Portal | list[Portal] | None,
|
||||||
|
None,
|
||||||
|
]:
|
||||||
'''
|
'''
|
||||||
Ask the arbiter to find actor(s) by name.
|
Ask the arbiter to find actor(s) by name.
|
||||||
|
|
||||||
|
@ -120,24 +179,83 @@ async def find_actor(
|
||||||
known to the arbiter.
|
known to the arbiter.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
async with query_actor(
|
if arbiter_sockaddr is not None:
|
||||||
name=name,
|
warnings.warn(
|
||||||
arbiter_sockaddr=arbiter_sockaddr,
|
'`tractor.find_actor(arbiter_sockaddr=<blah>)` is deprecated.\n'
|
||||||
) as sockaddr:
|
'Use `registry_addrs: list[tuple]` instead!',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
registry_addrs: list[tuple[str, int]] = [arbiter_sockaddr]
|
||||||
|
|
||||||
if sockaddr:
|
@acm
|
||||||
async with _connect_chan(*sockaddr) as chan:
|
async def maybe_open_portal_from_reg_addr(
|
||||||
async with open_portal(chan) as portal:
|
addr: tuple[str, int],
|
||||||
yield portal
|
):
|
||||||
else:
|
async with query_actor(
|
||||||
|
name=name,
|
||||||
|
regaddr=addr,
|
||||||
|
) as sockaddr:
|
||||||
|
if sockaddr:
|
||||||
|
async with _connect_chan(*sockaddr) as chan:
|
||||||
|
async with open_portal(chan) as portal:
|
||||||
|
yield portal
|
||||||
|
else:
|
||||||
|
yield None
|
||||||
|
|
||||||
|
if not registry_addrs:
|
||||||
|
# XXX NOTE: make sure to dynamically read the value on
|
||||||
|
# every call since something may change it globally (eg.
|
||||||
|
# like in our discovery test suite)!
|
||||||
|
from . import _root
|
||||||
|
registry_addrs = (
|
||||||
|
_runtime_vars['_registry_addrs']
|
||||||
|
or
|
||||||
|
_root._default_lo_addrs
|
||||||
|
)
|
||||||
|
|
||||||
|
maybe_portals: list[
|
||||||
|
AsyncContextManager[tuple[str, int]]
|
||||||
|
] = list(
|
||||||
|
maybe_open_portal_from_reg_addr(addr)
|
||||||
|
for addr in registry_addrs
|
||||||
|
)
|
||||||
|
|
||||||
|
async with gather_contexts(
|
||||||
|
mngrs=maybe_portals,
|
||||||
|
) as portals:
|
||||||
|
# log.runtime(
|
||||||
|
# 'Gathered portals:\n'
|
||||||
|
# f'{portals}'
|
||||||
|
# )
|
||||||
|
# NOTE: `gather_contexts()` will return a
|
||||||
|
# `tuple[None, None, ..., None]` if no contact
|
||||||
|
# can be made with any regstrar at any of the
|
||||||
|
# N provided addrs!
|
||||||
|
if not any(portals):
|
||||||
|
if raise_on_none:
|
||||||
|
raise RuntimeError(
|
||||||
|
f'No actor "{name}" found registered @ {registry_addrs}'
|
||||||
|
)
|
||||||
yield None
|
yield None
|
||||||
|
return
|
||||||
|
|
||||||
|
portals: list[Portal] = list(portals)
|
||||||
|
if only_first:
|
||||||
|
yield portals[0]
|
||||||
|
|
||||||
|
else:
|
||||||
|
# TODO: currently this may return multiple portals
|
||||||
|
# given there are multi-homed or multiple registrars..
|
||||||
|
# SO, we probably need de-duplication logic?
|
||||||
|
yield portals
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def wait_for_actor(
|
async def wait_for_actor(
|
||||||
name: str,
|
name: str,
|
||||||
arbiter_sockaddr: tuple[str, int] | None = None,
|
arbiter_sockaddr: tuple[str, int] | None = None,
|
||||||
# registry_addr: tuple[str, int] | None = None,
|
registry_addr: tuple[str, int] | None = None,
|
||||||
|
|
||||||
) -> AsyncGenerator[Portal, None]:
|
) -> AsyncGenerator[Portal, None]:
|
||||||
'''
|
'''
|
||||||
|
@ -146,17 +264,31 @@ async def wait_for_actor(
|
||||||
A portal to the first registered actor is returned.
|
A portal to the first registered actor is returned.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
actor = current_actor()
|
actor: Actor = current_actor()
|
||||||
|
|
||||||
async with get_arbiter(
|
if arbiter_sockaddr is not None:
|
||||||
*arbiter_sockaddr or actor._arb_addr,
|
warnings.warn(
|
||||||
) as arb_portal:
|
'`tractor.wait_for_actor(arbiter_sockaddr=<foo>)` is deprecated.\n'
|
||||||
sockaddrs = await arb_portal.run_from_ns(
|
'Use `registry_addr: tuple` instead!',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
registry_addr: tuple[str, int] = arbiter_sockaddr
|
||||||
|
|
||||||
|
# TODO: use `.trionics.gather_contexts()` like
|
||||||
|
# above in `find_actor()` as well?
|
||||||
|
reg_portal: Portal
|
||||||
|
regaddr: tuple[str, int] = registry_addr or actor.reg_addrs[0]
|
||||||
|
async with get_registry(*regaddr) as reg_portal:
|
||||||
|
sockaddrs = await reg_portal.run_from_ns(
|
||||||
'self',
|
'self',
|
||||||
'wait_for_actor',
|
'wait_for_actor',
|
||||||
name=name,
|
name=name,
|
||||||
)
|
)
|
||||||
sockaddr = sockaddrs[-1]
|
|
||||||
|
# get latest registered addr by default?
|
||||||
|
# TODO: offer multi-portal yields in multi-homed case?
|
||||||
|
sockaddr: tuple[str, int] = sockaddrs[-1]
|
||||||
|
|
||||||
async with _connect_chan(*sockaddr) as chan:
|
async with _connect_chan(*sockaddr) as chan:
|
||||||
async with open_portal(chan) as portal:
|
async with open_portal(chan) as portal:
|
||||||
|
|
|
@ -47,8 +47,8 @@ log = get_logger(__name__)
|
||||||
|
|
||||||
def _mp_main(
|
def _mp_main(
|
||||||
|
|
||||||
actor: Actor, # type: ignore
|
actor: Actor,
|
||||||
accept_addr: tuple[str, int],
|
accept_addrs: list[tuple[str, int]],
|
||||||
forkserver_info: tuple[Any, Any, Any, Any, Any],
|
forkserver_info: tuple[Any, Any, Any, Any, Any],
|
||||||
start_method: SpawnMethodKey,
|
start_method: SpawnMethodKey,
|
||||||
parent_addr: tuple[str, int] | None = None,
|
parent_addr: tuple[str, int] | None = None,
|
||||||
|
@ -77,8 +77,8 @@ def _mp_main(
|
||||||
log.debug(f"parent_addr is {parent_addr}")
|
log.debug(f"parent_addr is {parent_addr}")
|
||||||
trio_main = partial(
|
trio_main = partial(
|
||||||
async_main,
|
async_main,
|
||||||
actor,
|
actor=actor,
|
||||||
accept_addr,
|
accept_addrs=accept_addrs,
|
||||||
parent_addr=parent_addr
|
parent_addr=parent_addr
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
|
@ -96,7 +96,7 @@ def _mp_main(
|
||||||
|
|
||||||
def _trio_main(
|
def _trio_main(
|
||||||
|
|
||||||
actor: Actor, # type: ignore
|
actor: Actor,
|
||||||
*,
|
*,
|
||||||
parent_addr: tuple[str, int] | None = None,
|
parent_addr: tuple[str, int] | None = None,
|
||||||
infect_asyncio: bool = False,
|
infect_asyncio: bool = False,
|
||||||
|
@ -106,6 +106,7 @@ def _trio_main(
|
||||||
Entry point for a `trio_run_in_process` subactor.
|
Entry point for a `trio_run_in_process` subactor.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
__tracebackhide__: bool = True
|
||||||
_state._current_actor = actor
|
_state._current_actor = actor
|
||||||
trio_main = partial(
|
trio_main = partial(
|
||||||
async_main,
|
async_main,
|
||||||
|
|
|
@ -58,16 +58,44 @@ class InternalError(RuntimeError):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
_body_fields: list[str] = [
|
_body_fields: list[str] = [
|
||||||
'src_actor_uid',
|
'boxed_type',
|
||||||
|
'src_type',
|
||||||
|
# TODO: format this better if we're going to include it.
|
||||||
|
# 'relay_path',
|
||||||
|
'src_uid',
|
||||||
|
|
||||||
|
# only in sub-types
|
||||||
'canceller',
|
'canceller',
|
||||||
'sender',
|
'sender',
|
||||||
]
|
]
|
||||||
|
|
||||||
_msgdata_keys: list[str] = [
|
_msgdata_keys: list[str] = [
|
||||||
'type_str',
|
'boxed_type_str',
|
||||||
] + _body_fields
|
] + _body_fields
|
||||||
|
|
||||||
|
|
||||||
|
def get_err_type(type_name: str) -> BaseException|None:
|
||||||
|
'''
|
||||||
|
Look up an exception type by name from the set of locally
|
||||||
|
known namespaces:
|
||||||
|
|
||||||
|
- `builtins`
|
||||||
|
- `tractor._exceptions`
|
||||||
|
- `trio`
|
||||||
|
|
||||||
|
'''
|
||||||
|
for ns in [
|
||||||
|
builtins,
|
||||||
|
_this_mod,
|
||||||
|
trio,
|
||||||
|
]:
|
||||||
|
if type_ref := getattr(
|
||||||
|
ns,
|
||||||
|
type_name,
|
||||||
|
False,
|
||||||
|
):
|
||||||
|
return type_ref
|
||||||
|
|
||||||
|
|
||||||
# TODO: rename to just `RemoteError`?
|
# TODO: rename to just `RemoteError`?
|
||||||
class RemoteActorError(Exception):
|
class RemoteActorError(Exception):
|
||||||
|
@ -81,13 +109,14 @@ class RemoteActorError(Exception):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
reprol_fields: list[str] = [
|
reprol_fields: list[str] = [
|
||||||
'src_actor_uid',
|
'src_uid',
|
||||||
|
'relay_path',
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
message: str,
|
message: str,
|
||||||
suberror_type: Type[BaseException] | None = None,
|
boxed_type: Type[BaseException]|None = None,
|
||||||
**msgdata
|
**msgdata
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -101,20 +130,112 @@ class RemoteActorError(Exception):
|
||||||
# - .remote_type
|
# - .remote_type
|
||||||
# also pertains to our long long oustanding issue XD
|
# also pertains to our long long oustanding issue XD
|
||||||
# https://github.com/goodboy/tractor/issues/5
|
# https://github.com/goodboy/tractor/issues/5
|
||||||
self.boxed_type: str = suberror_type
|
#
|
||||||
|
# TODO: always set ._boxed_type` as `None` by default
|
||||||
|
# and instead render if from `.boxed_type_str`?
|
||||||
|
self._boxed_type: BaseException = boxed_type
|
||||||
|
self._src_type: BaseException|None = None
|
||||||
self.msgdata: dict[str, Any] = msgdata
|
self.msgdata: dict[str, Any] = msgdata
|
||||||
|
|
||||||
@property
|
# TODO: mask out eventually or place in `pack_error()`
|
||||||
def type(self) -> str:
|
# pre-`return` lines?
|
||||||
return self.boxed_type
|
# sanity on inceptions
|
||||||
|
if boxed_type is RemoteActorError:
|
||||||
|
assert self.src_type_str != 'RemoteActorError'
|
||||||
|
assert self.src_uid not in self.relay_path
|
||||||
|
|
||||||
|
# ensure type-str matches and round-tripping from that
|
||||||
|
# str results in same error type.
|
||||||
|
#
|
||||||
|
# TODO NOTE: this is currently exclusively for the
|
||||||
|
# `ContextCancelled(boxed_type=trio.Cancelled)` case as is
|
||||||
|
# used inside `._rpc._invoke()` atm though probably we
|
||||||
|
# should better emphasize that special (one off?) case
|
||||||
|
# either by customizing `ContextCancelled.__init__()` or
|
||||||
|
# through a special factor func?
|
||||||
|
elif boxed_type:
|
||||||
|
if not self.msgdata.get('boxed_type_str'):
|
||||||
|
self.msgdata['boxed_type_str'] = str(
|
||||||
|
type(boxed_type).__name__
|
||||||
|
)
|
||||||
|
|
||||||
|
assert self.boxed_type_str == self.msgdata['boxed_type_str']
|
||||||
|
assert self.boxed_type is boxed_type
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def type_str(self) -> str:
|
def src_type_str(self) -> str:
|
||||||
return str(type(self.boxed_type).__name__)
|
'''
|
||||||
|
String-name of the source error's type.
|
||||||
|
|
||||||
|
This should be the same as `.boxed_type_str` when unpacked
|
||||||
|
at the first relay/hop's receiving actor.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self.msgdata['src_type_str']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def src_actor_uid(self) -> tuple[str, str]|None:
|
def src_type(self) -> str:
|
||||||
return self.msgdata.get('src_actor_uid')
|
'''
|
||||||
|
Error type raised by original remote faulting actor.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if self._src_type is None:
|
||||||
|
self._src_type = get_err_type(
|
||||||
|
self.msgdata['src_type_str']
|
||||||
|
)
|
||||||
|
|
||||||
|
return self._src_type
|
||||||
|
|
||||||
|
@property
|
||||||
|
def boxed_type_str(self) -> str:
|
||||||
|
'''
|
||||||
|
String-name of the (last hop's) boxed error type.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self.msgdata['boxed_type_str']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def boxed_type(self) -> str:
|
||||||
|
'''
|
||||||
|
Error type boxed by last actor IPC hop.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if self._boxed_type is None:
|
||||||
|
self._boxed_type = get_err_type(
|
||||||
|
self.msgdata['boxed_type_str']
|
||||||
|
)
|
||||||
|
|
||||||
|
return self._boxed_type
|
||||||
|
|
||||||
|
@property
|
||||||
|
def relay_path(self) -> list[tuple]:
|
||||||
|
'''
|
||||||
|
Return the list of actors which consecutively relayed
|
||||||
|
a boxed `RemoteActorError` the src error up until THIS
|
||||||
|
actor's hop.
|
||||||
|
|
||||||
|
NOTE: a `list` field with the same name is expected to be
|
||||||
|
passed/updated in `.msgdata`.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self.msgdata['relay_path']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def relay_uid(self) -> tuple[str, str]|None:
|
||||||
|
return tuple(
|
||||||
|
self.msgdata['relay_path'][-1]
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def src_uid(self) -> tuple[str, str]|None:
|
||||||
|
if src_uid := (
|
||||||
|
self.msgdata.get('src_uid')
|
||||||
|
):
|
||||||
|
return tuple(src_uid)
|
||||||
|
# TODO: use path lookup instead?
|
||||||
|
# return tuple(
|
||||||
|
# self.msgdata['relay_path'][0]
|
||||||
|
# )
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def tb_str(
|
def tb_str(
|
||||||
|
@ -129,28 +250,56 @@ class RemoteActorError(Exception):
|
||||||
|
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
def _mk_fields_str(
|
||||||
|
self,
|
||||||
|
fields: list[str],
|
||||||
|
end_char: str = '\n',
|
||||||
|
) -> str:
|
||||||
|
_repr: str = ''
|
||||||
|
for key in fields:
|
||||||
|
val: Any|None = (
|
||||||
|
getattr(self, key, None)
|
||||||
|
or
|
||||||
|
self.msgdata.get(key)
|
||||||
|
)
|
||||||
|
# TODO: for `.relay_path` on multiline?
|
||||||
|
# if not isinstance(val, str):
|
||||||
|
# val_str = pformat(val)
|
||||||
|
# else:
|
||||||
|
val_str: str = repr(val)
|
||||||
|
|
||||||
|
if val:
|
||||||
|
_repr += f'{key}={val_str}{end_char}'
|
||||||
|
|
||||||
|
return _repr
|
||||||
|
|
||||||
def reprol(self) -> str:
|
def reprol(self) -> str:
|
||||||
'''
|
'''
|
||||||
Represent this error for "one line" display, like in
|
Represent this error for "one line" display, like in
|
||||||
a field of our `Context.__repr__()` output.
|
a field of our `Context.__repr__()` output.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
_repr: str = f'{type(self).__name__}('
|
# TODO: use this matryoshka emjoi XD
|
||||||
for key in self.reprol_fields:
|
# => 🪆
|
||||||
val: Any|None = self.msgdata.get(key)
|
reprol_str: str = f'{type(self).__name__}('
|
||||||
if val:
|
_repr: str = self._mk_fields_str(
|
||||||
_repr += f'{key}={repr(val)} '
|
self.reprol_fields,
|
||||||
|
end_char=' ',
|
||||||
return _repr
|
)
|
||||||
|
return (
|
||||||
|
reprol_str
|
||||||
|
+
|
||||||
|
_repr
|
||||||
|
)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
|
'''
|
||||||
|
Nicely formatted boxed error meta data + traceback.
|
||||||
|
|
||||||
fields: str = ''
|
'''
|
||||||
for key in _body_fields:
|
fields: str = self._mk_fields_str(
|
||||||
val: str|None = self.msgdata.get(key)
|
_body_fields,
|
||||||
if val:
|
)
|
||||||
fields += f'{key}={val}\n'
|
|
||||||
|
|
||||||
fields: str = textwrap.indent(
|
fields: str = textwrap.indent(
|
||||||
fields,
|
fields,
|
||||||
# prefix=' '*2,
|
# prefix=' '*2,
|
||||||
|
@ -165,8 +314,6 @@ class RemoteActorError(Exception):
|
||||||
f' ------ - ------\n'
|
f' ------ - ------\n'
|
||||||
f' _|\n'
|
f' _|\n'
|
||||||
)
|
)
|
||||||
# f'|\n'
|
|
||||||
# f' |\n'
|
|
||||||
if indent:
|
if indent:
|
||||||
body: str = textwrap.indent(
|
body: str = textwrap.indent(
|
||||||
body,
|
body,
|
||||||
|
@ -178,9 +325,47 @@ class RemoteActorError(Exception):
|
||||||
')>'
|
')>'
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO: local recontruction of remote exception deats
|
def unwrap(
|
||||||
|
self,
|
||||||
|
) -> BaseException:
|
||||||
|
'''
|
||||||
|
Unpack the inner-most source error from it's original IPC msg data.
|
||||||
|
|
||||||
|
We attempt to reconstruct (as best as we can) the original
|
||||||
|
`Exception` from as it would have been raised in the
|
||||||
|
failing actor's remote env.
|
||||||
|
|
||||||
|
'''
|
||||||
|
src_type_ref: Type[BaseException] = self.src_type
|
||||||
|
if not src_type_ref:
|
||||||
|
raise TypeError(
|
||||||
|
'Failed to lookup src error type:\n'
|
||||||
|
f'{self.src_type_str}'
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO: better tb insertion and all the fancier dunder
|
||||||
|
# metadata stuff as per `.__context__` etc. and friends:
|
||||||
|
# https://github.com/python-trio/trio/issues/611
|
||||||
|
return src_type_ref(self.tb_str)
|
||||||
|
|
||||||
|
# TODO: local recontruction of nested inception for a given
|
||||||
|
# "hop" / relay-node in this error's relay_path?
|
||||||
|
# => so would render a `RAE[RAE[RAE[Exception]]]` instance
|
||||||
|
# with all inner errors unpacked?
|
||||||
|
# -[ ] if this is useful shouldn't be too hard to impl right?
|
||||||
# def unbox(self) -> BaseException:
|
# def unbox(self) -> BaseException:
|
||||||
# ...
|
# '''
|
||||||
|
# Unbox to the prior relays (aka last boxing actor's)
|
||||||
|
# inner error.
|
||||||
|
|
||||||
|
# '''
|
||||||
|
# if not self.relay_path:
|
||||||
|
# return self.unwrap()
|
||||||
|
|
||||||
|
# # TODO..
|
||||||
|
# # return self.boxed_type(
|
||||||
|
# # boxed_type=get_type_ref(..
|
||||||
|
# raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class InternalActorError(RemoteActorError):
|
class InternalActorError(RemoteActorError):
|
||||||
|
@ -232,7 +417,7 @@ class ContextCancelled(RemoteActorError):
|
||||||
f'{self}'
|
f'{self}'
|
||||||
)
|
)
|
||||||
|
|
||||||
# to make `.__repr__()` work uniformly
|
# TODO: to make `.__repr__()` work uniformly?
|
||||||
# src_actor_uid = canceller
|
# src_actor_uid = canceller
|
||||||
|
|
||||||
|
|
||||||
|
@ -283,7 +468,8 @@ class MessagingError(Exception):
|
||||||
|
|
||||||
|
|
||||||
def pack_error(
|
def pack_error(
|
||||||
exc: BaseException,
|
exc: BaseException|RemoteActorError,
|
||||||
|
|
||||||
tb: str|None = None,
|
tb: str|None = None,
|
||||||
cid: str|None = None,
|
cid: str|None = None,
|
||||||
|
|
||||||
|
@ -300,27 +486,56 @@ def pack_error(
|
||||||
else:
|
else:
|
||||||
tb_str = traceback.format_exc()
|
tb_str = traceback.format_exc()
|
||||||
|
|
||||||
error_msg: dict[
|
error_msg: dict[ # for IPC
|
||||||
str,
|
str,
|
||||||
str | tuple[str, str]
|
str | tuple[str, str]
|
||||||
] = {
|
] = {}
|
||||||
'tb_str': tb_str,
|
our_uid: tuple = current_actor().uid
|
||||||
'type_str': type(exc).__name__,
|
|
||||||
'boxed_type': type(exc).__name__,
|
|
||||||
'src_actor_uid': current_actor().uid,
|
|
||||||
}
|
|
||||||
|
|
||||||
# TODO: ?just wholesale proxy `.msgdata: dict`?
|
|
||||||
# XXX WARNING, when i swapped these ctx-semantics
|
|
||||||
# tests started hanging..???!!!???
|
|
||||||
# if msgdata := exc.getattr('msgdata', {}):
|
|
||||||
# error_msg.update(msgdata)
|
|
||||||
if (
|
if (
|
||||||
isinstance(exc, ContextCancelled)
|
isinstance(exc, RemoteActorError)
|
||||||
or isinstance(exc, StreamOverrun)
|
|
||||||
):
|
):
|
||||||
error_msg.update(exc.msgdata)
|
error_msg.update(exc.msgdata)
|
||||||
|
|
||||||
|
# an onion/inception we need to pack
|
||||||
|
if (
|
||||||
|
type(exc) is RemoteActorError
|
||||||
|
and (boxed := exc.boxed_type)
|
||||||
|
and boxed != RemoteActorError
|
||||||
|
):
|
||||||
|
# sanity on source error (if needed when tweaking this)
|
||||||
|
assert (src_type := exc.src_type) != RemoteActorError
|
||||||
|
assert error_msg['src_type_str'] != 'RemoteActorError'
|
||||||
|
assert error_msg['src_type_str'] == src_type.__name__
|
||||||
|
assert error_msg['src_uid'] != our_uid
|
||||||
|
|
||||||
|
# set the boxed type to be another boxed type thus
|
||||||
|
# creating an "inception" when unpacked by
|
||||||
|
# `unpack_error()` in another actor who gets "relayed"
|
||||||
|
# this error Bo
|
||||||
|
#
|
||||||
|
# NOTE on WHY: since we are re-boxing and already
|
||||||
|
# boxed src error, we want to overwrite the original
|
||||||
|
# `boxed_type_str` and instead set it to the type of
|
||||||
|
# the input `exc` type.
|
||||||
|
error_msg['boxed_type_str'] = 'RemoteActorError'
|
||||||
|
|
||||||
|
else:
|
||||||
|
error_msg['src_uid'] = our_uid
|
||||||
|
error_msg['src_type_str'] = type(exc).__name__
|
||||||
|
error_msg['boxed_type_str'] = type(exc).__name__
|
||||||
|
|
||||||
|
# XXX alawys append us the last relay in error propagation path
|
||||||
|
error_msg.setdefault(
|
||||||
|
'relay_path',
|
||||||
|
[],
|
||||||
|
).append(our_uid)
|
||||||
|
|
||||||
|
# XXX NOTE: always ensure the traceback-str is from the
|
||||||
|
# locally raised error (**not** the prior relay's boxed
|
||||||
|
# content's `.msgdata`).
|
||||||
|
error_msg['tb_str'] = tb_str
|
||||||
|
|
||||||
pkt: dict = {'error': error_msg}
|
pkt: dict = {'error': error_msg}
|
||||||
if cid:
|
if cid:
|
||||||
pkt['cid'] = cid
|
pkt['cid'] = cid
|
||||||
|
@ -329,7 +544,6 @@ def pack_error(
|
||||||
|
|
||||||
|
|
||||||
def unpack_error(
|
def unpack_error(
|
||||||
|
|
||||||
msg: dict[str, Any],
|
msg: dict[str, Any],
|
||||||
|
|
||||||
chan: Channel|None = None,
|
chan: Channel|None = None,
|
||||||
|
@ -357,35 +571,32 @@ def unpack_error(
|
||||||
|
|
||||||
# retrieve the remote error's msg encoded details
|
# retrieve the remote error's msg encoded details
|
||||||
tb_str: str = error_dict.get('tb_str', '')
|
tb_str: str = error_dict.get('tb_str', '')
|
||||||
message: str = f'{chan.uid}\n' + tb_str
|
message: str = (
|
||||||
type_name: str = (
|
f'{chan.uid}\n'
|
||||||
error_dict.get('type_str')
|
+
|
||||||
or error_dict['boxed_type']
|
tb_str
|
||||||
)
|
)
|
||||||
suberror_type: Type[BaseException] = Exception
|
|
||||||
|
|
||||||
if type_name == 'ContextCancelled':
|
# try to lookup a suitable error type from the local runtime
|
||||||
|
# env then use it to construct a local instance.
|
||||||
|
boxed_type_str: str = error_dict['boxed_type_str']
|
||||||
|
boxed_type: Type[BaseException] = get_err_type(boxed_type_str)
|
||||||
|
|
||||||
|
if boxed_type_str == 'ContextCancelled':
|
||||||
box_type = ContextCancelled
|
box_type = ContextCancelled
|
||||||
suberror_type = box_type
|
assert boxed_type is box_type
|
||||||
|
|
||||||
else: # try to lookup a suitable local error type
|
# TODO: already included by `_this_mod` in else loop right?
|
||||||
for ns in [
|
#
|
||||||
builtins,
|
# we have an inception/onion-error so ensure
|
||||||
_this_mod,
|
# we include the relay_path info and the
|
||||||
trio,
|
# original source error.
|
||||||
]:
|
elif boxed_type_str == 'RemoteActorError':
|
||||||
if suberror_type := getattr(
|
assert boxed_type is RemoteActorError
|
||||||
ns,
|
assert len(error_dict['relay_path']) >= 1
|
||||||
type_name,
|
|
||||||
False,
|
|
||||||
):
|
|
||||||
break
|
|
||||||
|
|
||||||
exc = box_type(
|
exc = box_type(
|
||||||
message,
|
message,
|
||||||
suberror_type=suberror_type,
|
|
||||||
|
|
||||||
# unpack other fields into error type init
|
|
||||||
**error_dict,
|
**error_dict,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -501,6 +712,11 @@ def _raise_from_no_key_in_msg(
|
||||||
# destined for the `Context.result()` call during ctx-exit!
|
# destined for the `Context.result()` call during ctx-exit!
|
||||||
stream._eoc: Exception = eoc
|
stream._eoc: Exception = eoc
|
||||||
|
|
||||||
|
# in case there already is some underlying remote error
|
||||||
|
# that arrived which is probably the source of this stream
|
||||||
|
# closure
|
||||||
|
ctx.maybe_raise()
|
||||||
|
|
||||||
raise eoc from src_err
|
raise eoc from src_err
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|
|
@ -517,7 +517,9 @@ class Channel:
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def _connect_chan(
|
async def _connect_chan(
|
||||||
host: str, port: int
|
host: str,
|
||||||
|
port: int
|
||||||
|
|
||||||
) -> typing.AsyncGenerator[Channel, None]:
|
) -> typing.AsyncGenerator[Channel, None]:
|
||||||
'''
|
'''
|
||||||
Create and connect a channel with disconnect on context manager
|
Create and connect a channel with disconnect on context manager
|
||||||
|
|
|
@ -0,0 +1,151 @@
|
||||||
|
# tractor: structured concurrent "actors".
|
||||||
|
# Copyright 2018-eternity Tyler Goodlet.
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
Multiaddress parser and utils according the spec(s) defined by
|
||||||
|
`libp2p` and used in dependent project such as `ipfs`:
|
||||||
|
|
||||||
|
- https://docs.libp2p.io/concepts/fundamentals/addressing/
|
||||||
|
- https://github.com/libp2p/specs/blob/master/addressing/README.md
|
||||||
|
|
||||||
|
'''
|
||||||
|
from typing import Iterator
|
||||||
|
|
||||||
|
from bidict import bidict
|
||||||
|
|
||||||
|
# TODO: see if we can leverage libp2p ecosys projects instead of
|
||||||
|
# rolling our own (parser) impls of the above addressing specs:
|
||||||
|
# - https://github.com/libp2p/py-libp2p
|
||||||
|
# - https://docs.libp2p.io/concepts/nat/circuit-relay/#relay-addresses
|
||||||
|
# prots: bidict[int, str] = bidict({
|
||||||
|
prots: bidict[int, str] = {
|
||||||
|
'ipv4': 3,
|
||||||
|
'ipv6': 3,
|
||||||
|
'wg': 3,
|
||||||
|
|
||||||
|
'tcp': 4,
|
||||||
|
'udp': 4,
|
||||||
|
|
||||||
|
# TODO: support the next-gen shite Bo
|
||||||
|
# 'quic': 4,
|
||||||
|
# 'ssh': 7, # via rsyscall bootstrapping
|
||||||
|
}
|
||||||
|
|
||||||
|
prot_params: dict[str, tuple[str]] = {
|
||||||
|
'ipv4': ('addr',),
|
||||||
|
'ipv6': ('addr',),
|
||||||
|
'wg': ('addr', 'port', 'pubkey'),
|
||||||
|
|
||||||
|
'tcp': ('port',),
|
||||||
|
'udp': ('port',),
|
||||||
|
|
||||||
|
# 'quic': ('port',),
|
||||||
|
# 'ssh': ('port',),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def iter_prot_layers(
|
||||||
|
multiaddr: str,
|
||||||
|
) -> Iterator[
|
||||||
|
tuple[
|
||||||
|
int,
|
||||||
|
list[str]
|
||||||
|
]
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Unpack a libp2p style "multiaddress" into multiple "segments"
|
||||||
|
for each "layer" of the protocoll stack (in OSI terms).
|
||||||
|
|
||||||
|
'''
|
||||||
|
tokens: list[str] = multiaddr.split('/')
|
||||||
|
root, tokens = tokens[0], tokens[1:]
|
||||||
|
assert not root # there is a root '/' on LHS
|
||||||
|
itokens = iter(tokens)
|
||||||
|
|
||||||
|
prot: str | None = None
|
||||||
|
params: list[str] = []
|
||||||
|
for token in itokens:
|
||||||
|
# every prot path should start with a known
|
||||||
|
# key-str.
|
||||||
|
if token in prots:
|
||||||
|
if prot is None:
|
||||||
|
prot: str = token
|
||||||
|
else:
|
||||||
|
yield prot, params
|
||||||
|
prot = token
|
||||||
|
|
||||||
|
params = []
|
||||||
|
|
||||||
|
elif token not in prots:
|
||||||
|
params.append(token)
|
||||||
|
|
||||||
|
else:
|
||||||
|
yield prot, params
|
||||||
|
|
||||||
|
|
||||||
|
def parse_maddr(
|
||||||
|
multiaddr: str,
|
||||||
|
) -> dict[str, str | int | dict]:
|
||||||
|
'''
|
||||||
|
Parse a libp2p style "multiaddress" into its distinct protocol
|
||||||
|
segments where each segment is of the form:
|
||||||
|
|
||||||
|
`../<protocol>/<param0>/<param1>/../<paramN>`
|
||||||
|
|
||||||
|
and is loaded into a (order preserving) `layers: dict[str,
|
||||||
|
dict[str, Any]` which holds each protocol-layer-segment of the
|
||||||
|
original `str` path as a separate entry according to its approx
|
||||||
|
OSI "layer number".
|
||||||
|
|
||||||
|
Any `paramN` in the path must be distinctly defined by a str-token in the
|
||||||
|
(module global) `prot_params` table.
|
||||||
|
|
||||||
|
For eg. for wireguard which requires an address, port number and publickey
|
||||||
|
the protocol params are specified as the entry:
|
||||||
|
|
||||||
|
'wg': ('addr', 'port', 'pubkey'),
|
||||||
|
|
||||||
|
and are thus parsed from a maddr in that order:
|
||||||
|
`'/wg/1.1.1.1/51820/<pubkey>'`
|
||||||
|
|
||||||
|
'''
|
||||||
|
layers: dict[str, str | int | dict] = {}
|
||||||
|
for (
|
||||||
|
prot_key,
|
||||||
|
params,
|
||||||
|
) in iter_prot_layers(multiaddr):
|
||||||
|
|
||||||
|
layer: int = prots[prot_key] # OSI layer used for sorting
|
||||||
|
ep: dict[str, int | str] = {'layer': layer}
|
||||||
|
layers[prot_key] = ep
|
||||||
|
|
||||||
|
# TODO; validation and resolving of names:
|
||||||
|
# - each param via a validator provided as part of the
|
||||||
|
# prot_params def? (also see `"port"` case below..)
|
||||||
|
# - do a resolv step that will check addrs against
|
||||||
|
# any loaded network.resolv: dict[str, str]
|
||||||
|
rparams: list = list(reversed(params))
|
||||||
|
for key in prot_params[prot_key]:
|
||||||
|
val: str | int = rparams.pop()
|
||||||
|
|
||||||
|
# TODO: UGHH, dunno what we should do for validation
|
||||||
|
# here, put it in the params spec somehow?
|
||||||
|
if key == 'port':
|
||||||
|
val = int(val)
|
||||||
|
|
||||||
|
ep[key] = val
|
||||||
|
|
||||||
|
return layers
|
|
@ -461,7 +461,12 @@ class LocalPortal:
|
||||||
actor: 'Actor' # type: ignore # noqa
|
actor: 'Actor' # type: ignore # noqa
|
||||||
channel: Channel
|
channel: Channel
|
||||||
|
|
||||||
async def run_from_ns(self, ns: str, func_name: str, **kwargs) -> Any:
|
async def run_from_ns(
|
||||||
|
self,
|
||||||
|
ns: str,
|
||||||
|
func_name: str,
|
||||||
|
**kwargs,
|
||||||
|
) -> Any:
|
||||||
'''
|
'''
|
||||||
Run a requested local function from a namespace path and
|
Run a requested local function from a namespace path and
|
||||||
return it's result.
|
return it's result.
|
||||||
|
|
254
tractor/_root.py
254
tractor/_root.py
|
@ -22,10 +22,10 @@ from contextlib import asynccontextmanager
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import os
|
from typing import Callable
|
||||||
import typing
|
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
|
@ -47,8 +47,14 @@ from ._exceptions import is_multi_cancelled
|
||||||
|
|
||||||
|
|
||||||
# set at startup and after forks
|
# set at startup and after forks
|
||||||
_default_arbiter_host: str = '127.0.0.1'
|
_default_host: str = '127.0.0.1'
|
||||||
_default_arbiter_port: int = 1616
|
_default_port: int = 1616
|
||||||
|
|
||||||
|
# default registry always on localhost
|
||||||
|
_default_lo_addrs: list[tuple[str, int]] = [(
|
||||||
|
_default_host,
|
||||||
|
_default_port,
|
||||||
|
)]
|
||||||
|
|
||||||
|
|
||||||
logger = log.get_logger('tractor')
|
logger = log.get_logger('tractor')
|
||||||
|
@ -59,38 +65,73 @@ async def open_root_actor(
|
||||||
|
|
||||||
*,
|
*,
|
||||||
# defaults are above
|
# defaults are above
|
||||||
arbiter_addr: tuple[str, int] | None = None,
|
registry_addrs: list[tuple[str, int]]|None = None,
|
||||||
|
|
||||||
# defaults are above
|
# defaults are above
|
||||||
registry_addr: tuple[str, int] | None = None,
|
arbiter_addr: tuple[str, int]|None = None,
|
||||||
|
|
||||||
name: str | None = 'root',
|
name: str|None = 'root',
|
||||||
|
|
||||||
# either the `multiprocessing` start method:
|
# either the `multiprocessing` start method:
|
||||||
# https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
|
# https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
|
||||||
# OR `trio` (the new default).
|
# OR `trio` (the new default).
|
||||||
start_method: _spawn.SpawnMethodKey | None = None,
|
start_method: _spawn.SpawnMethodKey|None = None,
|
||||||
|
|
||||||
# enables the multi-process debugger support
|
# enables the multi-process debugger support
|
||||||
debug_mode: bool = False,
|
debug_mode: bool = False,
|
||||||
|
maybe_enable_greenback: bool = False, # `.pause_from_sync()/breakpoint()` support
|
||||||
|
enable_stack_on_sig: bool = False,
|
||||||
|
|
||||||
# internal logging
|
# internal logging
|
||||||
loglevel: str | None = None,
|
loglevel: str|None = None,
|
||||||
|
|
||||||
enable_modules: list | None = None,
|
enable_modules: list|None = None,
|
||||||
rpc_module_paths: list | None = None,
|
rpc_module_paths: list|None = None,
|
||||||
|
|
||||||
) -> typing.Any:
|
# NOTE: allow caller to ensure that only one registry exists
|
||||||
|
# and that this call creates it.
|
||||||
|
ensure_registry: bool = False,
|
||||||
|
|
||||||
|
) -> Actor:
|
||||||
'''
|
'''
|
||||||
Runtime init entry point for ``tractor``.
|
Runtime init entry point for ``tractor``.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
# TODO: stick this in a `@cm` defined in `devx._debug`?
|
||||||
|
#
|
||||||
# Override the global debugger hook to make it play nice with
|
# Override the global debugger hook to make it play nice with
|
||||||
# ``trio``, see much discussion in:
|
# ``trio``, see much discussion in:
|
||||||
# https://github.com/python-trio/trio/issues/1155#issuecomment-742964018
|
# https://github.com/python-trio/trio/issues/1155#issuecomment-742964018
|
||||||
builtin_bp_handler = sys.breakpointhook
|
builtin_bp_handler: Callable = sys.breakpointhook
|
||||||
orig_bp_path: str | None = os.environ.get('PYTHONBREAKPOINT', None)
|
orig_bp_path: str|None = os.environ.get(
|
||||||
os.environ['PYTHONBREAKPOINT'] = 'tractor.devx._debug.pause_from_sync'
|
'PYTHONBREAKPOINT',
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
debug_mode
|
||||||
|
and maybe_enable_greenback
|
||||||
|
and await _debug.maybe_init_greenback(
|
||||||
|
raise_not_found=False,
|
||||||
|
)
|
||||||
|
):
|
||||||
|
os.environ['PYTHONBREAKPOINT'] = (
|
||||||
|
'tractor.devx._debug.pause_from_sync'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# TODO: disable `breakpoint()` by default (without
|
||||||
|
# `greenback`) since it will break any multi-actor
|
||||||
|
# usage by a clobbered TTY's stdstreams!
|
||||||
|
def block_bps(*args, **kwargs):
|
||||||
|
raise RuntimeError(
|
||||||
|
'Trying to use `breakpoint()` eh?\n'
|
||||||
|
'Welp, `tractor` blocks `breakpoint()` built-in calls by default!\n'
|
||||||
|
'If you need to use it please install `greenback` and set '
|
||||||
|
'`debug_mode=True` when opening the runtime '
|
||||||
|
'(either via `.open_nursery()` or `open_root_actor()`)\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
sys.breakpointhook = block_bps
|
||||||
|
# os.environ['PYTHONBREAKPOINT'] = None
|
||||||
|
|
||||||
# attempt to retreive ``trio``'s sigint handler and stash it
|
# attempt to retreive ``trio``'s sigint handler and stash it
|
||||||
# on our debugger lock state.
|
# on our debugger lock state.
|
||||||
|
@ -100,7 +141,11 @@ async def open_root_actor(
|
||||||
_state._runtime_vars['_is_root'] = True
|
_state._runtime_vars['_is_root'] = True
|
||||||
|
|
||||||
# caps based rpc list
|
# caps based rpc list
|
||||||
enable_modules = enable_modules or []
|
enable_modules = (
|
||||||
|
enable_modules
|
||||||
|
or
|
||||||
|
[]
|
||||||
|
)
|
||||||
|
|
||||||
if rpc_module_paths:
|
if rpc_module_paths:
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
|
@ -116,20 +161,19 @@ async def open_root_actor(
|
||||||
|
|
||||||
if arbiter_addr is not None:
|
if arbiter_addr is not None:
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
'`arbiter_addr` is now deprecated and has been renamed to'
|
'`arbiter_addr` is now deprecated\n'
|
||||||
'`registry_addr`.\nUse that instead..',
|
'Use `registry_addrs: list[tuple]` instead..',
|
||||||
DeprecationWarning,
|
DeprecationWarning,
|
||||||
stacklevel=2,
|
stacklevel=2,
|
||||||
)
|
)
|
||||||
|
registry_addrs = [arbiter_addr]
|
||||||
|
|
||||||
registry_addr = (host, port) = (
|
registry_addrs: list[tuple[str, int]] = (
|
||||||
registry_addr
|
registry_addrs
|
||||||
or arbiter_addr
|
or
|
||||||
or (
|
_default_lo_addrs
|
||||||
_default_arbiter_host,
|
|
||||||
_default_arbiter_port,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
assert registry_addrs
|
||||||
|
|
||||||
loglevel = (
|
loglevel = (
|
||||||
loglevel
|
loglevel
|
||||||
|
@ -167,7 +211,11 @@ async def open_root_actor(
|
||||||
assert _log
|
assert _log
|
||||||
|
|
||||||
# TODO: factor this into `.devx._stackscope`!!
|
# TODO: factor this into `.devx._stackscope`!!
|
||||||
if debug_mode:
|
if (
|
||||||
|
debug_mode
|
||||||
|
and
|
||||||
|
enable_stack_on_sig
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
logger.info('Enabling `stackscope` traces on SIGUSR1')
|
logger.info('Enabling `stackscope` traces on SIGUSR1')
|
||||||
from .devx import enable_stack_on_sig
|
from .devx import enable_stack_on_sig
|
||||||
|
@ -177,73 +225,131 @@ async def open_root_actor(
|
||||||
'`stackscope` not installed for use in debug mode!'
|
'`stackscope` not installed for use in debug mode!'
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
# closed into below ping task-func
|
||||||
# make a temporary connection to see if an arbiter exists,
|
ponged_addrs: list[tuple[str, int]] = []
|
||||||
# if one can't be made quickly we assume none exists.
|
|
||||||
arbiter_found = False
|
|
||||||
|
|
||||||
# TODO: this connect-and-bail forces us to have to carefully
|
async def ping_tpt_socket(
|
||||||
# rewrap TCP 104-connection-reset errors as EOF so as to avoid
|
addr: tuple[str, int],
|
||||||
# propagating cancel-causing errors to the channel-msg loop
|
timeout: float = 1,
|
||||||
# machinery. Likely it would be better to eventually have
|
) -> None:
|
||||||
# a "discovery" protocol with basic handshake instead.
|
'''
|
||||||
with trio.move_on_after(1):
|
Attempt temporary connection to see if a registry is
|
||||||
async with _connect_chan(host, port):
|
listening at the requested address by a tranport layer
|
||||||
arbiter_found = True
|
ping.
|
||||||
|
|
||||||
except OSError:
|
If a connection can't be made quickly we assume none no
|
||||||
# TODO: make this a "discovery" log level?
|
server is listening at that addr.
|
||||||
logger.warning(f"No actor registry found @ {host}:{port}")
|
|
||||||
|
|
||||||
# create a local actor and start up its main routine/task
|
'''
|
||||||
if arbiter_found:
|
try:
|
||||||
|
# TODO: this connect-and-bail forces us to have to
|
||||||
|
# carefully rewrap TCP 104-connection-reset errors as
|
||||||
|
# EOF so as to avoid propagating cancel-causing errors
|
||||||
|
# to the channel-msg loop machinery. Likely it would
|
||||||
|
# be better to eventually have a "discovery" protocol
|
||||||
|
# with basic handshake instead?
|
||||||
|
with trio.move_on_after(timeout):
|
||||||
|
async with _connect_chan(*addr):
|
||||||
|
ponged_addrs.append(addr)
|
||||||
|
|
||||||
|
except OSError:
|
||||||
|
# TODO: make this a "discovery" log level?
|
||||||
|
logger.warning(f'No actor registry found @ {addr}')
|
||||||
|
|
||||||
|
async with trio.open_nursery() as tn:
|
||||||
|
for addr in registry_addrs:
|
||||||
|
tn.start_soon(
|
||||||
|
ping_tpt_socket,
|
||||||
|
tuple(addr), # TODO: just drop this requirement?
|
||||||
|
)
|
||||||
|
|
||||||
|
trans_bind_addrs: list[tuple[str, int]] = []
|
||||||
|
|
||||||
|
# Create a new local root-actor instance which IS NOT THE
|
||||||
|
# REGISTRAR
|
||||||
|
if ponged_addrs:
|
||||||
|
|
||||||
|
if ensure_registry:
|
||||||
|
raise RuntimeError(
|
||||||
|
f'Failed to open `{name}`@{ponged_addrs}: '
|
||||||
|
'registry socket(s) already bound'
|
||||||
|
)
|
||||||
|
|
||||||
# we were able to connect to an arbiter
|
# we were able to connect to an arbiter
|
||||||
logger.info(f"Arbiter seems to exist @ {host}:{port}")
|
logger.info(
|
||||||
|
f'Registry(s) seem(s) to exist @ {ponged_addrs}'
|
||||||
|
)
|
||||||
|
|
||||||
actor = Actor(
|
actor = Actor(
|
||||||
name or 'anonymous',
|
name=name or 'anonymous',
|
||||||
arbiter_addr=registry_addr,
|
registry_addrs=ponged_addrs,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
enable_modules=enable_modules,
|
enable_modules=enable_modules,
|
||||||
)
|
)
|
||||||
host, port = (host, 0)
|
# DO NOT use the registry_addrs as the transport server
|
||||||
|
# addrs for this new non-registar, root-actor.
|
||||||
|
for host, port in ponged_addrs:
|
||||||
|
# NOTE: zero triggers dynamic OS port allocation
|
||||||
|
trans_bind_addrs.append((host, 0))
|
||||||
|
|
||||||
|
# Start this local actor as the "registrar", aka a regular
|
||||||
|
# actor who manages the local registry of "mailboxes" of
|
||||||
|
# other process-tree-local sub-actors.
|
||||||
else:
|
else:
|
||||||
# start this local actor as the arbiter (aka a regular actor who
|
|
||||||
# manages the local registry of "mailboxes")
|
|
||||||
|
|
||||||
# Note that if the current actor is the arbiter it is desirable
|
# NOTE that if the current actor IS THE REGISTAR, the
|
||||||
# for it to stay up indefinitely until a re-election process has
|
# following init steps are taken:
|
||||||
# taken place - which is not implemented yet FYI).
|
# - the tranport layer server is bound to each (host, port)
|
||||||
|
# pair defined in provided registry_addrs, or the default.
|
||||||
|
trans_bind_addrs = registry_addrs
|
||||||
|
|
||||||
|
# - it is normally desirable for any registrar to stay up
|
||||||
|
# indefinitely until either all registered (child/sub)
|
||||||
|
# actors are terminated (via SC supervision) or,
|
||||||
|
# a re-election process has taken place.
|
||||||
|
# NOTE: all of ^ which is not implemented yet - see:
|
||||||
|
# https://github.com/goodboy/tractor/issues/216
|
||||||
|
# https://github.com/goodboy/tractor/pull/348
|
||||||
|
# https://github.com/goodboy/tractor/issues/296
|
||||||
|
|
||||||
actor = Arbiter(
|
actor = Arbiter(
|
||||||
name or 'arbiter',
|
name or 'registrar',
|
||||||
arbiter_addr=registry_addr,
|
registry_addrs=registry_addrs,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
enable_modules=enable_modules,
|
enable_modules=enable_modules,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Start up main task set via core actor-runtime nurseries.
|
||||||
try:
|
try:
|
||||||
# assign process-local actor
|
# assign process-local actor
|
||||||
_state._current_actor = actor
|
_state._current_actor = actor
|
||||||
|
|
||||||
# start local channel-server and fake the portal API
|
# start local channel-server and fake the portal API
|
||||||
# NOTE: this won't block since we provide the nursery
|
# NOTE: this won't block since we provide the nursery
|
||||||
logger.info(f"Starting local {actor} @ {host}:{port}")
|
ml_addrs_str: str = '\n'.join(
|
||||||
|
f'@{addr}' for addr in trans_bind_addrs
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
f'Starting local {actor.uid} on the following transport addrs:\n'
|
||||||
|
f'{ml_addrs_str}'
|
||||||
|
)
|
||||||
|
|
||||||
# start the actor runtime in a new task
|
# start the actor runtime in a new task
|
||||||
async with trio.open_nursery() as nursery:
|
async with trio.open_nursery() as nursery:
|
||||||
|
|
||||||
# ``_runtime.async_main()`` creates an internal nursery and
|
# ``_runtime.async_main()`` creates an internal nursery
|
||||||
# thus blocks here until the entire underlying actor tree has
|
# and blocks here until any underlying actor(-process)
|
||||||
# terminated thereby conducting structured concurrency.
|
# tree has terminated thereby conducting so called
|
||||||
|
# "end-to-end" structured concurrency throughout an
|
||||||
|
# entire hierarchical python sub-process set; all
|
||||||
|
# "actor runtime" primitives are SC-compat and thus all
|
||||||
|
# transitively spawned actors/processes must be as
|
||||||
|
# well.
|
||||||
await nursery.start(
|
await nursery.start(
|
||||||
partial(
|
partial(
|
||||||
async_main,
|
async_main,
|
||||||
actor,
|
actor,
|
||||||
accept_addr=(host, port),
|
accept_addrs=trans_bind_addrs,
|
||||||
parent_addr=None
|
parent_addr=None
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -255,7 +361,7 @@ async def open_root_actor(
|
||||||
BaseExceptionGroup,
|
BaseExceptionGroup,
|
||||||
) as err:
|
) as err:
|
||||||
|
|
||||||
entered = await _debug._maybe_enter_pm(err)
|
entered: bool = await _debug._maybe_enter_pm(err)
|
||||||
if (
|
if (
|
||||||
not entered
|
not entered
|
||||||
and
|
and
|
||||||
|
@ -263,7 +369,8 @@ async def open_root_actor(
|
||||||
):
|
):
|
||||||
logger.exception('Root actor crashed:\n')
|
logger.exception('Root actor crashed:\n')
|
||||||
|
|
||||||
# always re-raise
|
# ALWAYS re-raise any error bubbled up from the
|
||||||
|
# runtime!
|
||||||
raise
|
raise
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
@ -284,13 +391,15 @@ async def open_root_actor(
|
||||||
_state._current_actor = None
|
_state._current_actor = None
|
||||||
_state._last_actor_terminated = actor
|
_state._last_actor_terminated = actor
|
||||||
|
|
||||||
# restore breakpoint hook state
|
# restore built-in `breakpoint()` hook state
|
||||||
sys.breakpointhook = builtin_bp_handler
|
if debug_mode:
|
||||||
if orig_bp_path is not None:
|
if builtin_bp_handler is not None:
|
||||||
os.environ['PYTHONBREAKPOINT'] = orig_bp_path
|
sys.breakpointhook = builtin_bp_handler
|
||||||
else:
|
if orig_bp_path is not None:
|
||||||
# clear env back to having no entry
|
os.environ['PYTHONBREAKPOINT'] = orig_bp_path
|
||||||
os.environ.pop('PYTHONBREAKPOINT')
|
else:
|
||||||
|
# clear env back to having no entry
|
||||||
|
os.environ.pop('PYTHONBREAKPOINT')
|
||||||
|
|
||||||
logger.runtime("Root actor terminated")
|
logger.runtime("Root actor terminated")
|
||||||
|
|
||||||
|
@ -300,10 +409,7 @@ def run_daemon(
|
||||||
|
|
||||||
# runtime kwargs
|
# runtime kwargs
|
||||||
name: str | None = 'root',
|
name: str | None = 'root',
|
||||||
registry_addr: tuple[str, int] = (
|
registry_addrs: list[tuple[str, int]] = _default_lo_addrs,
|
||||||
_default_arbiter_host,
|
|
||||||
_default_arbiter_port,
|
|
||||||
),
|
|
||||||
|
|
||||||
start_method: str | None = None,
|
start_method: str | None = None,
|
||||||
debug_mode: bool = False,
|
debug_mode: bool = False,
|
||||||
|
@ -327,7 +433,7 @@ def run_daemon(
|
||||||
async def _main():
|
async def _main():
|
||||||
|
|
||||||
async with open_root_actor(
|
async with open_root_actor(
|
||||||
registry_addr=registry_addr,
|
registry_addrs=registry_addrs,
|
||||||
name=name,
|
name=name,
|
||||||
start_method=start_method,
|
start_method=start_method,
|
||||||
debug_mode=debug_mode,
|
debug_mode=debug_mode,
|
||||||
|
|
|
@ -26,7 +26,6 @@ from contextlib import (
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import inspect
|
import inspect
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
from types import ModuleType
|
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Callable,
|
Callable,
|
||||||
|
@ -268,7 +267,10 @@ async def _errors_relayed_via_ipc(
|
||||||
entered_debug = await _debug._maybe_enter_pm(err)
|
entered_debug = await _debug._maybe_enter_pm(err)
|
||||||
|
|
||||||
if not entered_debug:
|
if not entered_debug:
|
||||||
log.exception('Actor crashed:\n')
|
log.exception(
|
||||||
|
'RPC task crashed\n'
|
||||||
|
f'|_{ctx}'
|
||||||
|
)
|
||||||
|
|
||||||
# always (try to) ship RPC errors back to caller
|
# always (try to) ship RPC errors back to caller
|
||||||
if is_rpc:
|
if is_rpc:
|
||||||
|
@ -329,27 +331,6 @@ async def _errors_relayed_via_ipc(
|
||||||
actor._ongoing_rpc_tasks.set()
|
actor._ongoing_rpc_tasks.set()
|
||||||
|
|
||||||
|
|
||||||
_gb_mod: ModuleType|None|False = None
|
|
||||||
|
|
||||||
|
|
||||||
async def maybe_import_gb():
|
|
||||||
global _gb_mod
|
|
||||||
if _gb_mod is False:
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
import greenback
|
|
||||||
_gb_mod = greenback
|
|
||||||
await greenback.ensure_portal()
|
|
||||||
|
|
||||||
except ModuleNotFoundError:
|
|
||||||
log.debug(
|
|
||||||
'`greenback` is not installed.\n'
|
|
||||||
'No sync debug support!\n'
|
|
||||||
)
|
|
||||||
_gb_mod = False
|
|
||||||
|
|
||||||
|
|
||||||
async def _invoke(
|
async def _invoke(
|
||||||
|
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
|
@ -377,7 +358,9 @@ async def _invoke(
|
||||||
treat_as_gen: bool = False
|
treat_as_gen: bool = False
|
||||||
|
|
||||||
if _state.debug_mode():
|
if _state.debug_mode():
|
||||||
await maybe_import_gb()
|
# XXX for .pause_from_sync()` usage we need to make sure
|
||||||
|
# `greenback` is boostrapped in the subactor!
|
||||||
|
await _debug.maybe_init_greenback()
|
||||||
|
|
||||||
# TODO: possibly a specially formatted traceback
|
# TODO: possibly a specially formatted traceback
|
||||||
# (not sure what typing is for this..)?
|
# (not sure what typing is for this..)?
|
||||||
|
@ -608,7 +591,8 @@ async def _invoke(
|
||||||
# other side.
|
# other side.
|
||||||
ctxc = ContextCancelled(
|
ctxc = ContextCancelled(
|
||||||
msg,
|
msg,
|
||||||
suberror_type=trio.Cancelled,
|
boxed_type=trio.Cancelled,
|
||||||
|
# boxed_type_str='Cancelled',
|
||||||
canceller=canceller,
|
canceller=canceller,
|
||||||
)
|
)
|
||||||
# assign local error so that the `.outcome`
|
# assign local error so that the `.outcome`
|
||||||
|
@ -666,7 +650,7 @@ async def _invoke(
|
||||||
f'`{repr(ctx.outcome)}`',
|
f'`{repr(ctx.outcome)}`',
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
log.cancel(
|
log.runtime(
|
||||||
f'IPC context terminated with a final {res_type_str}\n\n'
|
f'IPC context terminated with a final {res_type_str}\n\n'
|
||||||
f'{ctx}\n'
|
f'{ctx}\n'
|
||||||
)
|
)
|
||||||
|
@ -699,12 +683,6 @@ async def try_ship_error_to_remote(
|
||||||
# TODO: special tb fmting for ctxc cases?
|
# TODO: special tb fmting for ctxc cases?
|
||||||
# tb=tb,
|
# tb=tb,
|
||||||
)
|
)
|
||||||
# NOTE: the src actor should always be packed into the
|
|
||||||
# error.. but how should we verify this?
|
|
||||||
# actor: Actor = _state.current_actor()
|
|
||||||
# assert err_msg['src_actor_uid']
|
|
||||||
# if not err_msg['error'].get('src_actor_uid'):
|
|
||||||
# import pdbp; pdbp.set_trace()
|
|
||||||
await channel.send(msg)
|
await channel.send(msg)
|
||||||
|
|
||||||
# XXX NOTE XXX in SC terms this is one of the worst things
|
# XXX NOTE XXX in SC terms this is one of the worst things
|
||||||
|
|
|
@ -45,6 +45,7 @@ from functools import partial
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
import importlib
|
import importlib
|
||||||
import importlib.util
|
import importlib.util
|
||||||
|
import os
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
@ -55,7 +56,7 @@ from typing import (
|
||||||
)
|
)
|
||||||
import uuid
|
import uuid
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
import os
|
import warnings
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
from trio import (
|
from trio import (
|
||||||
|
@ -77,8 +78,8 @@ from ._exceptions import (
|
||||||
ContextCancelled,
|
ContextCancelled,
|
||||||
TransportClosed,
|
TransportClosed,
|
||||||
)
|
)
|
||||||
from ._discovery import get_arbiter
|
|
||||||
from .devx import _debug
|
from .devx import _debug
|
||||||
|
from ._discovery import get_registry
|
||||||
from ._portal import Portal
|
from ._portal import Portal
|
||||||
from . import _state
|
from . import _state
|
||||||
from . import _mp_fixup_main
|
from . import _mp_fixup_main
|
||||||
|
@ -127,19 +128,24 @@ class Actor:
|
||||||
# ugh, we need to get rid of this and replace with a "registry" sys
|
# ugh, we need to get rid of this and replace with a "registry" sys
|
||||||
# https://github.com/goodboy/tractor/issues/216
|
# https://github.com/goodboy/tractor/issues/216
|
||||||
is_arbiter: bool = False
|
is_arbiter: bool = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_registrar(self) -> bool:
|
||||||
|
return self.is_arbiter
|
||||||
|
|
||||||
msg_buffer_size: int = 2**6
|
msg_buffer_size: int = 2**6
|
||||||
|
|
||||||
# nursery placeholders filled in by `async_main()` after fork
|
# nursery placeholders filled in by `async_main()` after fork
|
||||||
_root_n: Nursery | None = None
|
_root_n: Nursery|None = None
|
||||||
_service_n: Nursery | None = None
|
_service_n: Nursery|None = None
|
||||||
_server_n: Nursery | None = None
|
_server_n: Nursery|None = None
|
||||||
|
|
||||||
# Information about `__main__` from parent
|
# Information about `__main__` from parent
|
||||||
_parent_main_data: dict[str, str]
|
_parent_main_data: dict[str, str]
|
||||||
_parent_chan_cs: CancelScope | None = None
|
_parent_chan_cs: CancelScope|None = None
|
||||||
|
|
||||||
# syncs for setup/teardown sequences
|
# syncs for setup/teardown sequences
|
||||||
_server_down: trio.Event | None = None
|
_server_down: trio.Event|None = None
|
||||||
|
|
||||||
# user toggled crash handling (including monkey-patched in
|
# user toggled crash handling (including monkey-patched in
|
||||||
# `trio.open_nursery()` via `.trionics._supervisor` B)
|
# `trio.open_nursery()` via `.trionics._supervisor` B)
|
||||||
|
@ -162,10 +168,14 @@ class Actor:
|
||||||
name: str,
|
name: str,
|
||||||
*,
|
*,
|
||||||
enable_modules: list[str] = [],
|
enable_modules: list[str] = [],
|
||||||
uid: str | None = None,
|
uid: str|None = None,
|
||||||
loglevel: str | None = None,
|
loglevel: str|None = None,
|
||||||
arbiter_addr: tuple[str, int] | None = None,
|
registry_addrs: list[tuple[str, int]]|None = None,
|
||||||
spawn_method: str | None = None
|
spawn_method: str|None = None,
|
||||||
|
|
||||||
|
# TODO: remove!
|
||||||
|
arbiter_addr: tuple[str, int]|None = None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
This constructor is called in the parent actor **before** the spawning
|
This constructor is called in the parent actor **before** the spawning
|
||||||
|
@ -179,7 +189,7 @@ class Actor:
|
||||||
)
|
)
|
||||||
|
|
||||||
self._cancel_complete = trio.Event()
|
self._cancel_complete = trio.Event()
|
||||||
self._cancel_called_by_remote: tuple[str, tuple] | None = None
|
self._cancel_called_by_remote: tuple[str, tuple]|None = None
|
||||||
self._cancel_called: bool = False
|
self._cancel_called: bool = False
|
||||||
|
|
||||||
# retreive and store parent `__main__` data which
|
# retreive and store parent `__main__` data which
|
||||||
|
@ -189,27 +199,30 @@ class Actor:
|
||||||
# always include debugging tools module
|
# always include debugging tools module
|
||||||
enable_modules.append('tractor.devx._debug')
|
enable_modules.append('tractor.devx._debug')
|
||||||
|
|
||||||
mods = {}
|
self.enable_modules: dict[str, str] = {}
|
||||||
for name in enable_modules:
|
for name in enable_modules:
|
||||||
mod = importlib.import_module(name)
|
mod: ModuleType = importlib.import_module(name)
|
||||||
mods[name] = _get_mod_abspath(mod)
|
self.enable_modules[name] = _get_mod_abspath(mod)
|
||||||
|
|
||||||
self.enable_modules = mods
|
|
||||||
self._mods: dict[str, ModuleType] = {}
|
self._mods: dict[str, ModuleType] = {}
|
||||||
self.loglevel = loglevel
|
self.loglevel: str = loglevel
|
||||||
|
|
||||||
self._arb_addr: tuple[str, int] | None = (
|
if arbiter_addr is not None:
|
||||||
str(arbiter_addr[0]),
|
warnings.warn(
|
||||||
int(arbiter_addr[1])
|
'`Actor(arbiter_addr=<blah>)` is now deprecated.\n'
|
||||||
) if arbiter_addr else None
|
'Use `registry_addrs: list[tuple]` instead.',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
registry_addrs: list[tuple[str, int]] = [arbiter_addr]
|
||||||
|
|
||||||
# marked by the process spawning backend at startup
|
# marked by the process spawning backend at startup
|
||||||
# will be None for the parent most process started manually
|
# will be None for the parent most process started manually
|
||||||
# by the user (currently called the "arbiter")
|
# by the user (currently called the "arbiter")
|
||||||
self._spawn_method = spawn_method
|
self._spawn_method: str = spawn_method
|
||||||
|
|
||||||
self._peers: defaultdict = defaultdict(list)
|
self._peers: defaultdict = defaultdict(list)
|
||||||
self._peer_connected: dict = {}
|
self._peer_connected: dict[tuple[str, str], trio.Event] = {}
|
||||||
self._no_more_peers = trio.Event()
|
self._no_more_peers = trio.Event()
|
||||||
self._no_more_peers.set()
|
self._no_more_peers.set()
|
||||||
self._ongoing_rpc_tasks = trio.Event()
|
self._ongoing_rpc_tasks = trio.Event()
|
||||||
|
@ -232,13 +245,52 @@ class Actor:
|
||||||
] = {}
|
] = {}
|
||||||
|
|
||||||
self._listeners: list[trio.abc.Listener] = []
|
self._listeners: list[trio.abc.Listener] = []
|
||||||
self._parent_chan: Channel | None = None
|
self._parent_chan: Channel|None = None
|
||||||
self._forkserver_info: tuple | None = None
|
self._forkserver_info: tuple|None = None
|
||||||
self._actoruid2nursery: dict[
|
self._actoruid2nursery: dict[
|
||||||
tuple[str, str],
|
tuple[str, str],
|
||||||
ActorNursery | None,
|
ActorNursery|None,
|
||||||
] = {} # type: ignore # noqa
|
] = {} # type: ignore # noqa
|
||||||
|
|
||||||
|
# when provided, init the registry addresses property from
|
||||||
|
# input via the validator.
|
||||||
|
self._reg_addrs: list[tuple[str, int]] = []
|
||||||
|
if registry_addrs:
|
||||||
|
self.reg_addrs: list[tuple[str, int]] = registry_addrs
|
||||||
|
_state._runtime_vars['_registry_addrs'] = registry_addrs
|
||||||
|
|
||||||
|
@property
|
||||||
|
def reg_addrs(self) -> list[tuple[str, int]]:
|
||||||
|
'''
|
||||||
|
List of (socket) addresses for all known (and contactable)
|
||||||
|
registry actors.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self._reg_addrs
|
||||||
|
|
||||||
|
@reg_addrs.setter
|
||||||
|
def reg_addrs(
|
||||||
|
self,
|
||||||
|
addrs: list[tuple[str, int]],
|
||||||
|
) -> None:
|
||||||
|
if not addrs:
|
||||||
|
log.warning(
|
||||||
|
'Empty registry address list is invalid:\n'
|
||||||
|
f'{addrs}'
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# always sanity check the input list since it's critical
|
||||||
|
# that addrs are correct for discovery sys operation.
|
||||||
|
for addr in addrs:
|
||||||
|
if not isinstance(addr, tuple):
|
||||||
|
raise ValueError(
|
||||||
|
'Expected `Actor.reg_addrs: list[tuple[str, int]]`\n'
|
||||||
|
f'Got {addrs}'
|
||||||
|
)
|
||||||
|
|
||||||
|
self._reg_addrs = addrs
|
||||||
|
|
||||||
async def wait_for_peer(
|
async def wait_for_peer(
|
||||||
self, uid: tuple[str, str]
|
self, uid: tuple[str, str]
|
||||||
) -> tuple[trio.Event, Channel]:
|
) -> tuple[trio.Event, Channel]:
|
||||||
|
@ -336,6 +388,12 @@ class Actor:
|
||||||
self._no_more_peers = trio.Event() # unset by making new
|
self._no_more_peers = trio.Event() # unset by making new
|
||||||
chan = Channel.from_stream(stream)
|
chan = Channel.from_stream(stream)
|
||||||
their_uid: tuple[str, str]|None = chan.uid
|
their_uid: tuple[str, str]|None = chan.uid
|
||||||
|
if their_uid:
|
||||||
|
log.warning(
|
||||||
|
f'Re-connection from already known {their_uid}'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
log.runtime(f'New connection to us @{chan.raddr}')
|
||||||
|
|
||||||
con_msg: str = ''
|
con_msg: str = ''
|
||||||
if their_uid:
|
if their_uid:
|
||||||
|
@ -517,16 +575,19 @@ class Actor:
|
||||||
|
|
||||||
if disconnected:
|
if disconnected:
|
||||||
# if the transport died and this actor is still
|
# if the transport died and this actor is still
|
||||||
# registered within a local nursery, we report that the
|
# registered within a local nursery, we report
|
||||||
# IPC layer may have failed unexpectedly since it may be
|
# that the IPC layer may have failed
|
||||||
# the cause of other downstream errors.
|
# unexpectedly since it may be the cause of
|
||||||
|
# other downstream errors.
|
||||||
entry = local_nursery._children.get(uid)
|
entry = local_nursery._children.get(uid)
|
||||||
if entry:
|
if entry:
|
||||||
proc: trio.Process
|
proc: trio.Process
|
||||||
_, proc, _ = entry
|
_, proc, _ = entry
|
||||||
|
|
||||||
poll = getattr(proc, 'poll', None)
|
if (
|
||||||
if poll and poll() is None:
|
(poll := getattr(proc, 'poll', None))
|
||||||
|
and poll() is None
|
||||||
|
):
|
||||||
log.cancel(
|
log.cancel(
|
||||||
f'Peer IPC broke but subproc is alive?\n\n'
|
f'Peer IPC broke but subproc is alive?\n\n'
|
||||||
|
|
||||||
|
@ -720,7 +781,7 @@ class Actor:
|
||||||
#
|
#
|
||||||
# side: str|None = None,
|
# side: str|None = None,
|
||||||
|
|
||||||
msg_buffer_size: int | None = None,
|
msg_buffer_size: int|None = None,
|
||||||
allow_overruns: bool = False,
|
allow_overruns: bool = False,
|
||||||
|
|
||||||
) -> Context:
|
) -> Context:
|
||||||
|
@ -785,7 +846,7 @@ class Actor:
|
||||||
kwargs: dict,
|
kwargs: dict,
|
||||||
|
|
||||||
# IPC channel config
|
# IPC channel config
|
||||||
msg_buffer_size: int | None = None,
|
msg_buffer_size: int|None = None,
|
||||||
allow_overruns: bool = False,
|
allow_overruns: bool = False,
|
||||||
load_nsf: bool = False,
|
load_nsf: bool = False,
|
||||||
|
|
||||||
|
@ -859,11 +920,11 @@ class Actor:
|
||||||
|
|
||||||
async def _from_parent(
|
async def _from_parent(
|
||||||
self,
|
self,
|
||||||
parent_addr: tuple[str, int] | None,
|
parent_addr: tuple[str, int]|None,
|
||||||
|
|
||||||
) -> tuple[
|
) -> tuple[
|
||||||
Channel,
|
Channel,
|
||||||
list[tuple[str, int]] | None,
|
list[tuple[str, int]]|None,
|
||||||
]:
|
]:
|
||||||
'''
|
'''
|
||||||
Bootstrap this local actor's runtime config from its parent by
|
Bootstrap this local actor's runtime config from its parent by
|
||||||
|
@ -880,11 +941,11 @@ class Actor:
|
||||||
)
|
)
|
||||||
await chan.connect()
|
await chan.connect()
|
||||||
|
|
||||||
|
# TODO: move this into a `Channel.handshake()`?
|
||||||
# Initial handshake: swap names.
|
# Initial handshake: swap names.
|
||||||
await self._do_handshake(chan)
|
await self._do_handshake(chan)
|
||||||
|
|
||||||
accept_addr: tuple[str, int] | None = None
|
accept_addrs: list[tuple[str, int]]|None = None
|
||||||
|
|
||||||
if self._spawn_method == "trio":
|
if self._spawn_method == "trio":
|
||||||
# Receive runtime state from our parent
|
# Receive runtime state from our parent
|
||||||
parent_data: dict[str, Any]
|
parent_data: dict[str, Any]
|
||||||
|
@ -897,10 +958,7 @@ class Actor:
|
||||||
# if "trace"/"util" mode is enabled?
|
# if "trace"/"util" mode is enabled?
|
||||||
f'{pformat(parent_data)}\n'
|
f'{pformat(parent_data)}\n'
|
||||||
)
|
)
|
||||||
accept_addr = (
|
accept_addrs: list[tuple[str, int]] = parent_data.pop('bind_addrs')
|
||||||
parent_data.pop('bind_host'),
|
|
||||||
parent_data.pop('bind_port'),
|
|
||||||
)
|
|
||||||
rvs = parent_data.pop('_runtime_vars')
|
rvs = parent_data.pop('_runtime_vars')
|
||||||
|
|
||||||
if rvs['_debug_mode']:
|
if rvs['_debug_mode']:
|
||||||
|
@ -918,18 +976,23 @@ class Actor:
|
||||||
_state._runtime_vars.update(rvs)
|
_state._runtime_vars.update(rvs)
|
||||||
|
|
||||||
for attr, value in parent_data.items():
|
for attr, value in parent_data.items():
|
||||||
|
if (
|
||||||
if attr == '_arb_addr':
|
attr == 'reg_addrs'
|
||||||
|
and value
|
||||||
|
):
|
||||||
# XXX: ``msgspec`` doesn't support serializing tuples
|
# XXX: ``msgspec`` doesn't support serializing tuples
|
||||||
# so just cash manually here since it's what our
|
# so just cash manually here since it's what our
|
||||||
# internals expect.
|
# internals expect.
|
||||||
value = tuple(value) if value else None
|
# TODO: we don't really NEED these as
|
||||||
self._arb_addr = value
|
# tuples so we can probably drop this
|
||||||
|
# casting since apparently in python lists
|
||||||
|
# are "more efficient"?
|
||||||
|
self.reg_addrs = [tuple(val) for val in value]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
setattr(self, attr, value)
|
setattr(self, attr, value)
|
||||||
|
|
||||||
return chan, accept_addr
|
return chan, accept_addrs
|
||||||
|
|
||||||
except OSError: # failed to connect
|
except OSError: # failed to connect
|
||||||
log.warning(
|
log.warning(
|
||||||
|
@ -946,9 +1009,9 @@ class Actor:
|
||||||
handler_nursery: Nursery,
|
handler_nursery: Nursery,
|
||||||
*,
|
*,
|
||||||
# (host, port) to bind for channel server
|
# (host, port) to bind for channel server
|
||||||
accept_host: tuple[str, int] | None = None,
|
listen_sockaddrs: list[tuple[str, int]]|None = None,
|
||||||
accept_port: int = 0,
|
|
||||||
task_status: TaskStatus[trio.Nursery] = trio.TASK_STATUS_IGNORED,
|
task_status: TaskStatus[Nursery] = trio.TASK_STATUS_IGNORED,
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Start the IPC transport server, begin listening for new connections.
|
Start the IPC transport server, begin listening for new connections.
|
||||||
|
@ -958,30 +1021,40 @@ class Actor:
|
||||||
`.cancel_server()` is called.
|
`.cancel_server()` is called.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
if listen_sockaddrs is None:
|
||||||
|
listen_sockaddrs = [(None, 0)]
|
||||||
|
|
||||||
self._server_down = trio.Event()
|
self._server_down = trio.Event()
|
||||||
try:
|
try:
|
||||||
async with trio.open_nursery() as server_n:
|
async with trio.open_nursery() as server_n:
|
||||||
listeners: list[trio.abc.Listener] = await server_n.start(
|
|
||||||
partial(
|
for host, port in listen_sockaddrs:
|
||||||
trio.serve_tcp,
|
listeners: list[trio.abc.Listener] = await server_n.start(
|
||||||
self._stream_handler,
|
partial(
|
||||||
# new connections will stay alive even if this server
|
trio.serve_tcp,
|
||||||
# is cancelled
|
|
||||||
handler_nursery=handler_nursery,
|
handler=self._stream_handler,
|
||||||
port=accept_port,
|
port=port,
|
||||||
host=accept_host,
|
host=host,
|
||||||
|
|
||||||
|
# NOTE: configured such that new
|
||||||
|
# connections will stay alive even if
|
||||||
|
# this server is cancelled!
|
||||||
|
handler_nursery=handler_nursery,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
sockets: list[trio.socket] = [
|
||||||
sockets: list[trio.socket] = [
|
getattr(listener, 'socket', 'unknown socket')
|
||||||
getattr(listener, 'socket', 'unknown socket')
|
for listener in listeners
|
||||||
for listener in listeners
|
]
|
||||||
]
|
log.runtime(
|
||||||
log.runtime(
|
'Started TCP server(s)\n'
|
||||||
'Started TCP server(s)\n'
|
f'|_{sockets}\n'
|
||||||
f'|_{sockets}\n'
|
)
|
||||||
)
|
self._listeners.extend(listeners)
|
||||||
self._listeners.extend(listeners)
|
|
||||||
task_status.started(server_n)
|
task_status.started(server_n)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# signal the server is down since nursery above terminated
|
# signal the server is down since nursery above terminated
|
||||||
self._server_down.set()
|
self._server_down.set()
|
||||||
|
@ -1318,6 +1391,19 @@ class Actor:
|
||||||
log.runtime("Shutting down channel server")
|
log.runtime("Shutting down channel server")
|
||||||
self._server_n.cancel_scope.cancel()
|
self._server_n.cancel_scope.cancel()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def accept_addrs(self) -> list[tuple[str, int]]:
|
||||||
|
'''
|
||||||
|
All addresses to which the transport-channel server binds
|
||||||
|
and listens for new connections.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# throws OSError on failure
|
||||||
|
return [
|
||||||
|
listener.socket.getsockname()
|
||||||
|
for listener in self._listeners
|
||||||
|
] # type: ignore
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def accept_addr(self) -> tuple[str, int]:
|
def accept_addr(self) -> tuple[str, int]:
|
||||||
'''
|
'''
|
||||||
|
@ -1326,7 +1412,7 @@ class Actor:
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# throws OSError on failure
|
# throws OSError on failure
|
||||||
return self._listeners[0].socket.getsockname() # type: ignore
|
return self.accept_addrs[0]
|
||||||
|
|
||||||
def get_parent(self) -> Portal:
|
def get_parent(self) -> Portal:
|
||||||
'''
|
'''
|
||||||
|
@ -1343,6 +1429,7 @@ class Actor:
|
||||||
'''
|
'''
|
||||||
return self._peers[uid]
|
return self._peers[uid]
|
||||||
|
|
||||||
|
# TODO: move to `Channel.handshake(uid)`
|
||||||
async def _do_handshake(
|
async def _do_handshake(
|
||||||
self,
|
self,
|
||||||
chan: Channel
|
chan: Channel
|
||||||
|
@ -1379,7 +1466,7 @@ class Actor:
|
||||||
|
|
||||||
async def async_main(
|
async def async_main(
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
accept_addr: tuple[str, int] | None = None,
|
accept_addrs: tuple[str, int]|None = None,
|
||||||
|
|
||||||
# XXX: currently ``parent_addr`` is only needed for the
|
# XXX: currently ``parent_addr`` is only needed for the
|
||||||
# ``multiprocessing`` backend (which pickles state sent to
|
# ``multiprocessing`` backend (which pickles state sent to
|
||||||
|
@ -1388,7 +1475,7 @@ async def async_main(
|
||||||
# change this to a simple ``is_subactor: bool`` which will
|
# change this to a simple ``is_subactor: bool`` which will
|
||||||
# be False when running as root actor and True when as
|
# be False when running as root actor and True when as
|
||||||
# a subactor.
|
# a subactor.
|
||||||
parent_addr: tuple[str, int] | None = None,
|
parent_addr: tuple[str, int]|None = None,
|
||||||
task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED,
|
task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -1407,20 +1494,25 @@ async def async_main(
|
||||||
# on our debugger lock state.
|
# on our debugger lock state.
|
||||||
_debug.Lock._trio_handler = signal.getsignal(signal.SIGINT)
|
_debug.Lock._trio_handler = signal.getsignal(signal.SIGINT)
|
||||||
|
|
||||||
registered_with_arbiter = False
|
is_registered: bool = False
|
||||||
try:
|
try:
|
||||||
|
|
||||||
# establish primary connection with immediate parent
|
# establish primary connection with immediate parent
|
||||||
actor._parent_chan = None
|
actor._parent_chan: Channel|None = None
|
||||||
if parent_addr is not None:
|
if parent_addr is not None:
|
||||||
|
|
||||||
actor._parent_chan, accept_addr_rent = await actor._from_parent(
|
(
|
||||||
parent_addr)
|
actor._parent_chan,
|
||||||
|
set_accept_addr_says_rent,
|
||||||
|
) = await actor._from_parent(parent_addr)
|
||||||
|
|
||||||
# either it's passed in because we're not a child
|
# either it's passed in because we're not a child or
|
||||||
# or because we're running in mp mode
|
# because we're running in mp mode
|
||||||
if accept_addr_rent is not None:
|
if (
|
||||||
accept_addr = accept_addr_rent
|
set_accept_addr_says_rent
|
||||||
|
and set_accept_addr_says_rent is not None
|
||||||
|
):
|
||||||
|
accept_addrs = set_accept_addr_says_rent
|
||||||
|
|
||||||
# The "root" nursery ensures the channel with the immediate
|
# The "root" nursery ensures the channel with the immediate
|
||||||
# parent is kept alive as a resilient service until
|
# parent is kept alive as a resilient service until
|
||||||
|
@ -1460,34 +1552,72 @@ async def async_main(
|
||||||
# - subactor: the bind address is sent by our parent
|
# - subactor: the bind address is sent by our parent
|
||||||
# over our established channel
|
# over our established channel
|
||||||
# - root actor: the ``accept_addr`` passed to this method
|
# - root actor: the ``accept_addr`` passed to this method
|
||||||
assert accept_addr
|
assert accept_addrs
|
||||||
host, port = accept_addr
|
|
||||||
|
|
||||||
actor._server_n = await service_nursery.start(
|
try:
|
||||||
partial(
|
actor._server_n = await service_nursery.start(
|
||||||
actor._serve_forever,
|
partial(
|
||||||
service_nursery,
|
actor._serve_forever,
|
||||||
accept_host=host,
|
service_nursery,
|
||||||
accept_port=port
|
listen_sockaddrs=accept_addrs,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
except OSError as oserr:
|
||||||
accept_addr = actor.accept_addr
|
# NOTE: always allow runtime hackers to debug
|
||||||
|
# tranport address bind errors - normally it's
|
||||||
|
# something silly like the wrong socket-address
|
||||||
|
# passed via a config or CLI Bo
|
||||||
|
entered_debug: bool = await _debug._maybe_enter_pm(oserr)
|
||||||
|
if not entered_debug:
|
||||||
|
log.exception('Failed to init IPC channel server !?\n')
|
||||||
|
raise
|
||||||
|
|
||||||
|
accept_addrs: list[tuple[str, int]] = actor.accept_addrs
|
||||||
|
|
||||||
|
# NOTE: only set the loopback addr for the
|
||||||
|
# process-tree-global "root" mailbox since
|
||||||
|
# all sub-actors should be able to speak to
|
||||||
|
# their root actor over that channel.
|
||||||
if _state._runtime_vars['_is_root']:
|
if _state._runtime_vars['_is_root']:
|
||||||
_state._runtime_vars['_root_mailbox'] = accept_addr
|
for addr in accept_addrs:
|
||||||
|
host, _ = addr
|
||||||
|
# TODO: generic 'lo' detector predicate
|
||||||
|
if '127.0.0.1' in host:
|
||||||
|
_state._runtime_vars['_root_mailbox'] = addr
|
||||||
|
|
||||||
# Register with the arbiter if we're told its addr
|
# Register with the arbiter if we're told its addr
|
||||||
log.runtime(f"Registering {actor} for role `{actor.name}`")
|
log.runtime(
|
||||||
assert isinstance(actor._arb_addr, tuple)
|
f'Registering `{actor.name}` ->\n'
|
||||||
|
f'{pformat(accept_addrs)}'
|
||||||
|
)
|
||||||
|
|
||||||
async with get_arbiter(*actor._arb_addr) as arb_portal:
|
# TODO: ideally we don't fan out to all registrars
|
||||||
await arb_portal.run_from_ns(
|
# if addresses point to the same actor..
|
||||||
'self',
|
# So we need a way to detect that? maybe iterate
|
||||||
'register_actor',
|
# only on unique actor uids?
|
||||||
uid=actor.uid,
|
for addr in actor.reg_addrs:
|
||||||
sockaddr=accept_addr,
|
try:
|
||||||
)
|
assert isinstance(addr, tuple)
|
||||||
|
assert addr[1] # non-zero after bind
|
||||||
|
except AssertionError:
|
||||||
|
await _debug.pause()
|
||||||
|
|
||||||
registered_with_arbiter = True
|
async with get_registry(*addr) as reg_portal:
|
||||||
|
for accept_addr in accept_addrs:
|
||||||
|
|
||||||
|
if not accept_addr[1]:
|
||||||
|
await _debug.pause()
|
||||||
|
|
||||||
|
assert accept_addr[1]
|
||||||
|
|
||||||
|
await reg_portal.run_from_ns(
|
||||||
|
'self',
|
||||||
|
'register_actor',
|
||||||
|
uid=actor.uid,
|
||||||
|
sockaddr=accept_addr,
|
||||||
|
)
|
||||||
|
|
||||||
|
is_registered: bool = True
|
||||||
|
|
||||||
# init steps complete
|
# init steps complete
|
||||||
task_status.started()
|
task_status.started()
|
||||||
|
@ -1520,18 +1650,20 @@ async def async_main(
|
||||||
log.runtime("Closing all actor lifetime contexts")
|
log.runtime("Closing all actor lifetime contexts")
|
||||||
actor.lifetime_stack.close()
|
actor.lifetime_stack.close()
|
||||||
|
|
||||||
if not registered_with_arbiter:
|
if not is_registered:
|
||||||
# TODO: I guess we could try to connect back
|
# TODO: I guess we could try to connect back
|
||||||
# to the parent through a channel and engage a debugger
|
# to the parent through a channel and engage a debugger
|
||||||
# once we have that all working with std streams locking?
|
# once we have that all working with std streams locking?
|
||||||
log.exception(
|
log.exception(
|
||||||
f"Actor errored and failed to register with arbiter "
|
f"Actor errored and failed to register with arbiter "
|
||||||
f"@ {actor._arb_addr}?")
|
f"@ {actor.reg_addrs[0]}?")
|
||||||
log.error(
|
log.error(
|
||||||
"\n\n\t^^^ THIS IS PROBABLY A TRACTOR BUGGGGG!!! ^^^\n"
|
"\n\n\t^^^ THIS IS PROBABLY AN INTERNAL `tractor` BUG! ^^^\n\n"
|
||||||
"\tCALMLY CALL THE AUTHORITIES AND HIDE YOUR CHILDREN.\n\n"
|
"\t>> CALMLY CALL THE AUTHORITIES AND HIDE YOUR CHILDREN <<\n\n"
|
||||||
"\tYOUR PARENT CODE IS GOING TO KEEP WORKING FINE!!!\n"
|
"\tIf this is a sub-actor hopefully its parent will keep running "
|
||||||
"\tTHIS IS HOW RELIABlE SYSTEMS ARE SUPPOSED TO WORK!?!?\n"
|
"correctly presuming this error was safely ignored..\n\n"
|
||||||
|
"\tPLEASE REPORT THIS TRACEBACK IN A BUG REPORT: "
|
||||||
|
"https://github.com/goodboy/tractor/issues\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
if actor._parent_chan:
|
if actor._parent_chan:
|
||||||
|
@ -1571,27 +1703,33 @@ async def async_main(
|
||||||
|
|
||||||
# Unregister actor from the registry-sys / registrar.
|
# Unregister actor from the registry-sys / registrar.
|
||||||
if (
|
if (
|
||||||
registered_with_arbiter
|
is_registered
|
||||||
and not actor.is_arbiter
|
and not actor.is_registrar
|
||||||
):
|
):
|
||||||
failed = False
|
failed: bool = False
|
||||||
assert isinstance(actor._arb_addr, tuple)
|
for addr in actor.reg_addrs:
|
||||||
with trio.move_on_after(0.5) as cs:
|
assert isinstance(addr, tuple)
|
||||||
cs.shield = True
|
with trio.move_on_after(0.5) as cs:
|
||||||
try:
|
cs.shield = True
|
||||||
async with get_arbiter(*actor._arb_addr) as arb_portal:
|
try:
|
||||||
await arb_portal.run_from_ns(
|
async with get_registry(
|
||||||
'self',
|
*addr,
|
||||||
'unregister_actor',
|
) as reg_portal:
|
||||||
uid=actor.uid
|
await reg_portal.run_from_ns(
|
||||||
)
|
'self',
|
||||||
except OSError:
|
'unregister_actor',
|
||||||
|
uid=actor.uid
|
||||||
|
)
|
||||||
|
except OSError:
|
||||||
|
failed = True
|
||||||
|
if cs.cancelled_caught:
|
||||||
failed = True
|
failed = True
|
||||||
if cs.cancelled_caught:
|
|
||||||
failed = True
|
if failed:
|
||||||
if failed:
|
log.warning(
|
||||||
log.warning(
|
f'Failed to unregister {actor.name} from '
|
||||||
f"Failed to unregister {actor.name} from arbiter")
|
f'registar @ {addr}'
|
||||||
|
)
|
||||||
|
|
||||||
# Ensure all peers (actors connected to us as clients) are finished
|
# Ensure all peers (actors connected to us as clients) are finished
|
||||||
if not actor._no_more_peers.is_set():
|
if not actor._no_more_peers.is_set():
|
||||||
|
@ -1610,18 +1748,36 @@ async def async_main(
|
||||||
# TODO: rename to `Registry` and move to `._discovery`!
|
# TODO: rename to `Registry` and move to `._discovery`!
|
||||||
class Arbiter(Actor):
|
class Arbiter(Actor):
|
||||||
'''
|
'''
|
||||||
A special actor who knows all the other actors and always has
|
A special registrar actor who can contact all other actors
|
||||||
access to a top level nursery.
|
within its immediate process tree and possibly keeps a registry
|
||||||
|
of others meant to be discoverable in a distributed
|
||||||
|
application. Normally the registrar is also the "root actor"
|
||||||
|
and thus always has access to the top-most-level actor
|
||||||
|
(process) nursery.
|
||||||
|
|
||||||
The arbiter is by default the first actor spawned on each host
|
By default, the registrar is always initialized when and if no
|
||||||
and is responsible for keeping track of all other actors for
|
other registrar socket addrs have been specified to runtime
|
||||||
coordination purposes. If a new main process is launched and an
|
init entry-points (such as `open_root_actor()` or
|
||||||
arbiter is already running that arbiter will be used.
|
`open_nursery()`). Any time a new main process is launched (and
|
||||||
|
thus thus a new root actor created) and, no existing registrar
|
||||||
|
can be contacted at the provided `registry_addr`, then a new
|
||||||
|
one is always created; however, if one can be reached it is
|
||||||
|
used.
|
||||||
|
|
||||||
|
Normally a distributed app requires at least registrar per
|
||||||
|
logical host where for that given "host space" (aka localhost
|
||||||
|
IPC domain of addresses) it is responsible for making all other
|
||||||
|
host (local address) bound actors *discoverable* to external
|
||||||
|
actor trees running on remote hosts.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
is_arbiter = True
|
is_arbiter = True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs) -> None:
|
def __init__(
|
||||||
|
self,
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
self._registry: dict[
|
self._registry: dict[
|
||||||
tuple[str, str],
|
tuple[str, str],
|
||||||
|
@ -1641,7 +1797,7 @@ class Arbiter(Actor):
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
|
|
||||||
) -> tuple[str, int] | None:
|
) -> tuple[str, int]|None:
|
||||||
|
|
||||||
for uid, sockaddr in self._registry.items():
|
for uid, sockaddr in self._registry.items():
|
||||||
if name in uid:
|
if name in uid:
|
||||||
|
@ -1663,7 +1819,10 @@ class Arbiter(Actor):
|
||||||
# unpacker since we have tuples as keys (not this makes the
|
# unpacker since we have tuples as keys (not this makes the
|
||||||
# arbiter suscetible to hashdos):
|
# arbiter suscetible to hashdos):
|
||||||
# https://github.com/msgpack/msgpack-python#major-breaking-changes-in-msgpack-10
|
# https://github.com/msgpack/msgpack-python#major-breaking-changes-in-msgpack-10
|
||||||
return {'.'.join(key): val for key, val in self._registry.items()}
|
return {
|
||||||
|
'.'.join(key): val
|
||||||
|
for key, val in self._registry.items()
|
||||||
|
}
|
||||||
|
|
||||||
async def wait_for_actor(
|
async def wait_for_actor(
|
||||||
self,
|
self,
|
||||||
|
@ -1706,8 +1865,15 @@ class Arbiter(Actor):
|
||||||
sockaddr: tuple[str, int]
|
sockaddr: tuple[str, int]
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
uid = name, _ = (str(uid[0]), str(uid[1]))
|
uid = name, hash = (str(uid[0]), str(uid[1]))
|
||||||
self._registry[uid] = (str(sockaddr[0]), int(sockaddr[1]))
|
addr = (host, port) = (
|
||||||
|
str(sockaddr[0]),
|
||||||
|
int(sockaddr[1]),
|
||||||
|
)
|
||||||
|
if port == 0:
|
||||||
|
await _debug.pause()
|
||||||
|
assert port # should never be 0-dynamic-os-alloc
|
||||||
|
self._registry[uid] = addr
|
||||||
|
|
||||||
# pop and signal all waiter events
|
# pop and signal all waiter events
|
||||||
events = self._waiters.pop(name, [])
|
events = self._waiters.pop(name, [])
|
||||||
|
|
|
@ -220,6 +220,10 @@ async def hard_kill(
|
||||||
# whilst also hacking on it XD
|
# whilst also hacking on it XD
|
||||||
# terminate_after: int = 99999,
|
# terminate_after: int = 99999,
|
||||||
|
|
||||||
|
# NOTE: for mucking with `.pause()`-ing inside the runtime
|
||||||
|
# whilst also hacking on it XD
|
||||||
|
# terminate_after: int = 99999,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Un-gracefully terminate an OS level `trio.Process` after timeout.
|
Un-gracefully terminate an OS level `trio.Process` after timeout.
|
||||||
|
@ -365,7 +369,7 @@ async def new_proc(
|
||||||
errors: dict[tuple[str, str], Exception],
|
errors: dict[tuple[str, str], Exception],
|
||||||
|
|
||||||
# passed through to actor main
|
# passed through to actor main
|
||||||
bind_addr: tuple[str, int],
|
bind_addrs: list[tuple[str, int]],
|
||||||
parent_addr: tuple[str, int],
|
parent_addr: tuple[str, int],
|
||||||
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
||||||
|
|
||||||
|
@ -387,7 +391,7 @@ async def new_proc(
|
||||||
actor_nursery,
|
actor_nursery,
|
||||||
subactor,
|
subactor,
|
||||||
errors,
|
errors,
|
||||||
bind_addr,
|
bind_addrs,
|
||||||
parent_addr,
|
parent_addr,
|
||||||
_runtime_vars, # run time vars
|
_runtime_vars, # run time vars
|
||||||
infect_asyncio=infect_asyncio,
|
infect_asyncio=infect_asyncio,
|
||||||
|
@ -402,7 +406,7 @@ async def trio_proc(
|
||||||
errors: dict[tuple[str, str], Exception],
|
errors: dict[tuple[str, str], Exception],
|
||||||
|
|
||||||
# passed through to actor main
|
# passed through to actor main
|
||||||
bind_addr: tuple[str, int],
|
bind_addrs: list[tuple[str, int]],
|
||||||
parent_addr: tuple[str, int],
|
parent_addr: tuple[str, int],
|
||||||
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
||||||
*,
|
*,
|
||||||
|
@ -491,16 +495,15 @@ async def trio_proc(
|
||||||
|
|
||||||
# send additional init params
|
# send additional init params
|
||||||
await chan.send({
|
await chan.send({
|
||||||
"_parent_main_data": subactor._parent_main_data,
|
'_parent_main_data': subactor._parent_main_data,
|
||||||
"enable_modules": subactor.enable_modules,
|
'enable_modules': subactor.enable_modules,
|
||||||
"_arb_addr": subactor._arb_addr,
|
'reg_addrs': subactor.reg_addrs,
|
||||||
"bind_host": bind_addr[0],
|
'bind_addrs': bind_addrs,
|
||||||
"bind_port": bind_addr[1],
|
'_runtime_vars': _runtime_vars,
|
||||||
"_runtime_vars": _runtime_vars,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
# track subactor in current nursery
|
# track subactor in current nursery
|
||||||
curr_actor = current_actor()
|
curr_actor: Actor = current_actor()
|
||||||
curr_actor._actoruid2nursery[subactor.uid] = actor_nursery
|
curr_actor._actoruid2nursery[subactor.uid] = actor_nursery
|
||||||
|
|
||||||
# resume caller at next checkpoint now that child is up
|
# resume caller at next checkpoint now that child is up
|
||||||
|
@ -602,7 +605,7 @@ async def mp_proc(
|
||||||
subactor: Actor,
|
subactor: Actor,
|
||||||
errors: dict[tuple[str, str], Exception],
|
errors: dict[tuple[str, str], Exception],
|
||||||
# passed through to actor main
|
# passed through to actor main
|
||||||
bind_addr: tuple[str, int],
|
bind_addrs: list[tuple[str, int]],
|
||||||
parent_addr: tuple[str, int],
|
parent_addr: tuple[str, int],
|
||||||
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
||||||
*,
|
*,
|
||||||
|
@ -660,7 +663,7 @@ async def mp_proc(
|
||||||
target=_mp_main,
|
target=_mp_main,
|
||||||
args=(
|
args=(
|
||||||
subactor,
|
subactor,
|
||||||
bind_addr,
|
bind_addrs,
|
||||||
fs_info,
|
fs_info,
|
||||||
_spawn_method,
|
_spawn_method,
|
||||||
parent_addr,
|
parent_addr,
|
||||||
|
|
|
@ -30,10 +30,16 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
_current_actor: Actor|None = None # type: ignore # noqa
|
_current_actor: Actor|None = None # type: ignore # noqa
|
||||||
_last_actor_terminated: Actor|None = None
|
_last_actor_terminated: Actor|None = None
|
||||||
|
|
||||||
|
# TODO: mk this a `msgspec.Struct`!
|
||||||
_runtime_vars: dict[str, Any] = {
|
_runtime_vars: dict[str, Any] = {
|
||||||
'_debug_mode': False,
|
'_debug_mode': False,
|
||||||
'_is_root': False,
|
'_is_root': False,
|
||||||
'_root_mailbox': (None, None)
|
'_root_mailbox': (None, None),
|
||||||
|
'_registry_addrs': [],
|
||||||
|
|
||||||
|
# for `breakpoint()` support
|
||||||
|
'use_greenback': False,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -22,10 +22,7 @@ from contextlib import asynccontextmanager as acm
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import inspect
|
import inspect
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
from typing import (
|
from typing import TYPE_CHECKING
|
||||||
Optional,
|
|
||||||
TYPE_CHECKING,
|
|
||||||
)
|
|
||||||
import typing
|
import typing
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
@ -97,7 +94,7 @@ class ActorNursery:
|
||||||
tuple[
|
tuple[
|
||||||
Actor,
|
Actor,
|
||||||
trio.Process | mp.Process,
|
trio.Process | mp.Process,
|
||||||
Optional[Portal],
|
Portal | None,
|
||||||
]
|
]
|
||||||
] = {}
|
] = {}
|
||||||
# portals spawned with ``run_in_actor()`` are
|
# portals spawned with ``run_in_actor()`` are
|
||||||
|
@ -121,12 +118,12 @@ class ActorNursery:
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
*,
|
*,
|
||||||
bind_addr: tuple[str, int] = _default_bind_addr,
|
bind_addrs: list[tuple[str, int]] = [_default_bind_addr],
|
||||||
rpc_module_paths: list[str] | None = None,
|
rpc_module_paths: list[str]|None = None,
|
||||||
enable_modules: list[str] | None = None,
|
enable_modules: list[str]|None = None,
|
||||||
loglevel: str | None = None, # set log level per subactor
|
loglevel: str|None = None, # set log level per subactor
|
||||||
nursery: trio.Nursery | None = None,
|
nursery: trio.Nursery|None = None,
|
||||||
debug_mode: Optional[bool] | None = None,
|
debug_mode: bool|None = None,
|
||||||
infect_asyncio: bool = False,
|
infect_asyncio: bool = False,
|
||||||
) -> Portal:
|
) -> Portal:
|
||||||
'''
|
'''
|
||||||
|
@ -161,7 +158,9 @@ class ActorNursery:
|
||||||
# modules allowed to invoked funcs from
|
# modules allowed to invoked funcs from
|
||||||
enable_modules=enable_modules,
|
enable_modules=enable_modules,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
arbiter_addr=current_actor()._arb_addr,
|
|
||||||
|
# verbatim relay this actor's registrar addresses
|
||||||
|
registry_addrs=current_actor().reg_addrs,
|
||||||
)
|
)
|
||||||
parent_addr = self._actor.accept_addr
|
parent_addr = self._actor.accept_addr
|
||||||
assert parent_addr
|
assert parent_addr
|
||||||
|
@ -178,7 +177,7 @@ class ActorNursery:
|
||||||
self,
|
self,
|
||||||
subactor,
|
subactor,
|
||||||
self.errors,
|
self.errors,
|
||||||
bind_addr,
|
bind_addrs,
|
||||||
parent_addr,
|
parent_addr,
|
||||||
_rtv, # run time vars
|
_rtv, # run time vars
|
||||||
infect_asyncio=infect_asyncio,
|
infect_asyncio=infect_asyncio,
|
||||||
|
@ -191,8 +190,8 @@ class ActorNursery:
|
||||||
fn: typing.Callable,
|
fn: typing.Callable,
|
||||||
*,
|
*,
|
||||||
|
|
||||||
name: Optional[str] = None,
|
name: str | None = None,
|
||||||
bind_addr: tuple[str, int] = _default_bind_addr,
|
bind_addrs: tuple[str, int] = [_default_bind_addr],
|
||||||
rpc_module_paths: list[str] | None = None,
|
rpc_module_paths: list[str] | None = None,
|
||||||
enable_modules: list[str] | None = None,
|
enable_modules: list[str] | None = None,
|
||||||
loglevel: str | None = None, # set log level per subactor
|
loglevel: str | None = None, # set log level per subactor
|
||||||
|
@ -221,7 +220,7 @@ class ActorNursery:
|
||||||
enable_modules=[mod_path] + (
|
enable_modules=[mod_path] + (
|
||||||
enable_modules or rpc_module_paths or []
|
enable_modules or rpc_module_paths or []
|
||||||
),
|
),
|
||||||
bind_addr=bind_addr,
|
bind_addrs=bind_addrs,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
# use the run_in_actor nursery
|
# use the run_in_actor nursery
|
||||||
nursery=self._ria_nursery,
|
nursery=self._ria_nursery,
|
||||||
|
@ -584,7 +583,7 @@ async def open_nursery(
|
||||||
finally:
|
finally:
|
||||||
msg: str = (
|
msg: str = (
|
||||||
'Actor-nursery exited\n'
|
'Actor-nursery exited\n'
|
||||||
f'|_{an}\n\n'
|
f'|_{an}\n'
|
||||||
)
|
)
|
||||||
|
|
||||||
# shutdown runtime if it was started
|
# shutdown runtime if it was started
|
||||||
|
|
|
@ -0,0 +1,177 @@
|
||||||
|
# tractor: structured concurrent "actors".
|
||||||
|
# Copyright 2018-eternity Tyler Goodlet.
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
Tools for code-object annotation, introspection and mutation
|
||||||
|
as it pertains to improving the grok-ability of our runtime!
|
||||||
|
|
||||||
|
'''
|
||||||
|
from __future__ import annotations
|
||||||
|
import inspect
|
||||||
|
# import msgspec
|
||||||
|
# from pprint import pformat
|
||||||
|
from types import (
|
||||||
|
FrameType,
|
||||||
|
FunctionType,
|
||||||
|
MethodType,
|
||||||
|
# CodeType,
|
||||||
|
)
|
||||||
|
from typing import (
|
||||||
|
# Any,
|
||||||
|
Callable,
|
||||||
|
# TYPE_CHECKING,
|
||||||
|
Type,
|
||||||
|
)
|
||||||
|
|
||||||
|
from tractor.msg import (
|
||||||
|
pretty_struct,
|
||||||
|
NamespacePath,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: yeah, i don't love this and we should prolly just
|
||||||
|
# write a decorator that actually keeps a stupid ref to the func
|
||||||
|
# obj..
|
||||||
|
def get_class_from_frame(fr: FrameType) -> (
|
||||||
|
FunctionType
|
||||||
|
|MethodType
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Attempt to get the function (or method) reference
|
||||||
|
from a given `FrameType`.
|
||||||
|
|
||||||
|
Verbatim from an SO:
|
||||||
|
https://stackoverflow.com/a/2220759
|
||||||
|
|
||||||
|
'''
|
||||||
|
args, _, _, value_dict = inspect.getargvalues(fr)
|
||||||
|
|
||||||
|
# we check the first parameter for the frame function is
|
||||||
|
# named 'self'
|
||||||
|
if (
|
||||||
|
len(args)
|
||||||
|
and
|
||||||
|
# TODO: other cases for `@classmethod` etc..?)
|
||||||
|
args[0] == 'self'
|
||||||
|
):
|
||||||
|
# in that case, 'self' will be referenced in value_dict
|
||||||
|
instance: object = value_dict.get('self')
|
||||||
|
if instance:
|
||||||
|
# return its class
|
||||||
|
return getattr(
|
||||||
|
instance,
|
||||||
|
'__class__',
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
|
||||||
|
# return None otherwise
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def func_ref_from_frame(
|
||||||
|
frame: FrameType,
|
||||||
|
) -> Callable:
|
||||||
|
func_name: str = frame.f_code.co_name
|
||||||
|
try:
|
||||||
|
return frame.f_globals[func_name]
|
||||||
|
except KeyError:
|
||||||
|
cls: Type|None = get_class_from_frame(frame)
|
||||||
|
if cls:
|
||||||
|
return getattr(
|
||||||
|
cls,
|
||||||
|
func_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: move all this into new `.devx._code`!
|
||||||
|
# -[ ] prolly create a `@runtime_api` dec?
|
||||||
|
# -[ ] ^- make it capture and/or accept buncha optional
|
||||||
|
# meta-data like a fancier version of `@pdbp.hideframe`.
|
||||||
|
#
|
||||||
|
class CallerInfo(pretty_struct.Struct):
|
||||||
|
rt_fi: inspect.FrameInfo
|
||||||
|
call_frame: FrameType
|
||||||
|
|
||||||
|
@property
|
||||||
|
def api_func_ref(self) -> Callable|None:
|
||||||
|
return func_ref_from_frame(self.rt_fi.frame)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def api_nsp(self) -> NamespacePath|None:
|
||||||
|
func: FunctionType = self.api_func_ref
|
||||||
|
if func:
|
||||||
|
return NamespacePath.from_ref(func)
|
||||||
|
|
||||||
|
return '<unknown>'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def caller_func_ref(self) -> Callable|None:
|
||||||
|
return func_ref_from_frame(self.call_frame)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def caller_nsp(self) -> NamespacePath|None:
|
||||||
|
func: FunctionType = self.caller_func_ref
|
||||||
|
if func:
|
||||||
|
return NamespacePath.from_ref(func)
|
||||||
|
|
||||||
|
return '<unknown>'
|
||||||
|
|
||||||
|
|
||||||
|
def find_caller_info(
|
||||||
|
dunder_var: str = '__runtimeframe__',
|
||||||
|
iframes:int = 1,
|
||||||
|
check_frame_depth: bool = True,
|
||||||
|
|
||||||
|
) -> CallerInfo|None:
|
||||||
|
'''
|
||||||
|
Scan up the callstack for a frame with a `dunder_var: str` variable
|
||||||
|
and return the `iframes` frames above it.
|
||||||
|
|
||||||
|
By default we scan for a `__runtimeframe__` scope var which
|
||||||
|
denotes a `tractor` API above which (one frame up) is "user
|
||||||
|
app code" which "called into" the `tractor` method or func.
|
||||||
|
|
||||||
|
TODO: ex with `Portal.open_context()`
|
||||||
|
|
||||||
|
'''
|
||||||
|
# TODO: use this instead?
|
||||||
|
# https://docs.python.org/3/library/inspect.html#inspect.getouterframes
|
||||||
|
frames: list[inspect.FrameInfo] = inspect.stack()
|
||||||
|
for fi in frames:
|
||||||
|
assert (
|
||||||
|
fi.function
|
||||||
|
==
|
||||||
|
fi.frame.f_code.co_name
|
||||||
|
)
|
||||||
|
this_frame: FrameType = fi.frame
|
||||||
|
dunder_val: int|None = this_frame.f_locals.get(dunder_var)
|
||||||
|
if dunder_val:
|
||||||
|
go_up_iframes: int = (
|
||||||
|
dunder_val # could be 0 or `True` i guess?
|
||||||
|
or
|
||||||
|
iframes
|
||||||
|
)
|
||||||
|
rt_frame: FrameType = fi.frame
|
||||||
|
call_frame = rt_frame
|
||||||
|
for i in range(go_up_iframes):
|
||||||
|
call_frame = call_frame.f_back
|
||||||
|
|
||||||
|
return CallerInfo(
|
||||||
|
rt_fi=fi,
|
||||||
|
call_frame=call_frame,
|
||||||
|
)
|
||||||
|
|
||||||
|
return None
|
|
@ -33,35 +33,46 @@ from functools import (
|
||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
import threading
|
||||||
import traceback
|
import traceback
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Callable,
|
Callable,
|
||||||
AsyncIterator,
|
AsyncIterator,
|
||||||
AsyncGenerator,
|
AsyncGenerator,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
|
from types import (
|
||||||
|
FrameType,
|
||||||
|
ModuleType,
|
||||||
)
|
)
|
||||||
from types import FrameType
|
|
||||||
|
|
||||||
import pdbp
|
import pdbp
|
||||||
|
import sniffio
|
||||||
import tractor
|
import tractor
|
||||||
import trio
|
import trio
|
||||||
from trio.lowlevel import current_task
|
from trio.lowlevel import current_task
|
||||||
from trio_typing import (
|
from trio import (
|
||||||
TaskStatus,
|
TaskStatus,
|
||||||
# Task,
|
# Task,
|
||||||
)
|
)
|
||||||
|
|
||||||
from ..log import get_logger
|
from tractor.log import get_logger
|
||||||
from .._state import (
|
from tractor._state import (
|
||||||
current_actor,
|
current_actor,
|
||||||
is_root_process,
|
is_root_process,
|
||||||
debug_mode,
|
debug_mode,
|
||||||
)
|
)
|
||||||
from .._exceptions import (
|
from tractor._exceptions import (
|
||||||
is_multi_cancelled,
|
is_multi_cancelled,
|
||||||
ContextCancelled,
|
ContextCancelled,
|
||||||
)
|
)
|
||||||
from .._ipc import Channel
|
from tractor._ipc import Channel
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from tractor._runtime import (
|
||||||
|
Actor,
|
||||||
|
)
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
@ -116,10 +127,36 @@ class Lock:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def shield_sigint(cls):
|
def shield_sigint(cls):
|
||||||
cls._orig_sigint_handler = signal.signal(
|
'''
|
||||||
signal.SIGINT,
|
Shield out SIGINT handling (which by default triggers
|
||||||
shield_sigint_handler,
|
`trio.Task` cancellation) in subactors when the `pdb` REPL
|
||||||
)
|
is active.
|
||||||
|
|
||||||
|
Avoids cancellation of the current actor (task) when the
|
||||||
|
user mistakenly sends ctl-c or a signal is received from
|
||||||
|
an external request; explicit runtime cancel requests are
|
||||||
|
allowed until the use exits the REPL session using
|
||||||
|
'continue' or 'quit', at which point the orig SIGINT
|
||||||
|
handler is restored.
|
||||||
|
|
||||||
|
'''
|
||||||
|
#
|
||||||
|
# XXX detect whether we're running from a non-main thread
|
||||||
|
# in which case schedule the SIGINT shielding override
|
||||||
|
# to in the main thread.
|
||||||
|
# https://docs.python.org/3/library/signal.html#signals-and-threads
|
||||||
|
if not cls.is_main_trio_thread():
|
||||||
|
cls._orig_sigint_handler: Callable = trio.from_thread.run_sync(
|
||||||
|
signal.signal,
|
||||||
|
signal.SIGINT,
|
||||||
|
shield_sigint_handler,
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
cls._orig_sigint_handler = signal.signal(
|
||||||
|
signal.SIGINT,
|
||||||
|
shield_sigint_handler,
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@pdbp.hideframe # XXX NOTE XXX see below in `.pause_from_sync()`
|
@pdbp.hideframe # XXX NOTE XXX see below in `.pause_from_sync()`
|
||||||
|
@ -127,13 +164,60 @@ class Lock:
|
||||||
# always restore ``trio``'s sigint handler. see notes below in
|
# always restore ``trio``'s sigint handler. see notes below in
|
||||||
# the pdb factory about the nightmare that is that code swapping
|
# the pdb factory about the nightmare that is that code swapping
|
||||||
# out the handler when the repl activates...
|
# out the handler when the repl activates...
|
||||||
signal.signal(signal.SIGINT, cls._trio_handler)
|
if not cls.is_main_trio_thread():
|
||||||
|
trio.from_thread.run_sync(
|
||||||
|
signal.signal,
|
||||||
|
signal.SIGINT,
|
||||||
|
cls._trio_handler,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
signal.signal(
|
||||||
|
signal.SIGINT,
|
||||||
|
cls._trio_handler,
|
||||||
|
)
|
||||||
|
|
||||||
cls._orig_sigint_handler = None
|
cls._orig_sigint_handler = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def is_main_trio_thread(cls) -> bool:
|
||||||
|
'''
|
||||||
|
Check if we're the "main" thread (as in the first one
|
||||||
|
started by cpython) AND that it is ALSO the thread that
|
||||||
|
called `trio.run()` and not some thread spawned with
|
||||||
|
`trio.to_thread.run_sync()`.
|
||||||
|
|
||||||
|
'''
|
||||||
|
is_trio_main = (
|
||||||
|
# TODO: since this is private, @oremanj says
|
||||||
|
# we should just copy the impl for now..
|
||||||
|
(is_main_thread := trio._util.is_main_thread())
|
||||||
|
and
|
||||||
|
(async_lib := sniffio.current_async_library()) == 'trio'
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
not is_trio_main
|
||||||
|
and is_main_thread
|
||||||
|
):
|
||||||
|
log.warning(
|
||||||
|
f'Current async-lib detected by `sniffio`: {async_lib}\n'
|
||||||
|
)
|
||||||
|
return is_trio_main
|
||||||
|
# XXX apparently unreliable..see ^
|
||||||
|
# (
|
||||||
|
# threading.current_thread()
|
||||||
|
# is not threading.main_thread()
|
||||||
|
# )
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def release(cls):
|
def release(cls):
|
||||||
try:
|
try:
|
||||||
cls._debug_lock.release()
|
if not cls.is_main_trio_thread():
|
||||||
|
trio.from_thread.run_sync(
|
||||||
|
cls._debug_lock.release
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
cls._debug_lock.release()
|
||||||
|
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
# uhhh makes no sense but been seeing the non-owner
|
# uhhh makes no sense but been seeing the non-owner
|
||||||
# release error even though this is definitely the task
|
# release error even though this is definitely the task
|
||||||
|
@ -400,7 +484,6 @@ async def wait_for_parent_stdin_hijack(
|
||||||
|
|
||||||
# this syncs to child's ``Context.started()`` call.
|
# this syncs to child's ``Context.started()`` call.
|
||||||
async with portal.open_context(
|
async with portal.open_context(
|
||||||
|
|
||||||
lock_tty_for_child,
|
lock_tty_for_child,
|
||||||
subactor_uid=actor_uid,
|
subactor_uid=actor_uid,
|
||||||
|
|
||||||
|
@ -438,11 +521,31 @@ async def wait_for_parent_stdin_hijack(
|
||||||
log.debug('Exiting debugger from child')
|
log.debug('Exiting debugger from child')
|
||||||
|
|
||||||
|
|
||||||
def mk_mpdb() -> tuple[MultiActorPdb, Callable]:
|
def mk_mpdb() -> MultiActorPdb:
|
||||||
|
'''
|
||||||
|
Deliver a new `MultiActorPdb`: a multi-process safe `pdbp`
|
||||||
|
REPL using the magic of SC!
|
||||||
|
|
||||||
|
Our `pdb.Pdb` subtype accomplishes multi-process safe debugging
|
||||||
|
by:
|
||||||
|
|
||||||
|
- mutexing access to the root process' TTY & stdstreams
|
||||||
|
via an IPC managed `Lock` singleton per process tree.
|
||||||
|
|
||||||
|
- temporarily overriding any subactor's SIGINT handler to shield during
|
||||||
|
live REPL sessions in sub-actors such that cancellation is
|
||||||
|
never (mistakenly) triggered by a ctrl-c and instead only
|
||||||
|
by either explicit requests in the runtime or
|
||||||
|
|
||||||
|
'''
|
||||||
pdb = MultiActorPdb()
|
pdb = MultiActorPdb()
|
||||||
# signal.signal = pdbp.hideframe(signal.signal)
|
|
||||||
|
|
||||||
|
# Always shield out SIGINTs for subactors when REPL is active.
|
||||||
|
#
|
||||||
|
# XXX detect whether we're running from a non-main thread
|
||||||
|
# in which case schedule the SIGINT shielding override
|
||||||
|
# to in the main thread.
|
||||||
|
# https://docs.python.org/3/library/signal.html#signals-and-threads
|
||||||
Lock.shield_sigint()
|
Lock.shield_sigint()
|
||||||
|
|
||||||
# XXX: These are the important flags mentioned in
|
# XXX: These are the important flags mentioned in
|
||||||
|
@ -451,7 +554,7 @@ def mk_mpdb() -> tuple[MultiActorPdb, Callable]:
|
||||||
pdb.allow_kbdint = True
|
pdb.allow_kbdint = True
|
||||||
pdb.nosigint = True
|
pdb.nosigint = True
|
||||||
|
|
||||||
return pdb, Lock.unshield_sigint
|
return pdb
|
||||||
|
|
||||||
|
|
||||||
def shield_sigint_handler(
|
def shield_sigint_handler(
|
||||||
|
@ -464,17 +567,16 @@ def shield_sigint_handler(
|
||||||
'''
|
'''
|
||||||
Specialized, debugger-aware SIGINT handler.
|
Specialized, debugger-aware SIGINT handler.
|
||||||
|
|
||||||
In childred we always ignore to avoid deadlocks since cancellation
|
In childred we always ignore/shield for SIGINT to avoid
|
||||||
should always be managed by the parent supervising actor. The root
|
deadlocks since cancellation should always be managed by the
|
||||||
is always cancelled on ctrl-c.
|
supervising parent actor. The root actor-proces is always
|
||||||
|
cancelled on ctrl-c.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
__tracebackhide__ = True
|
__tracebackhide__: bool = True
|
||||||
|
uid_in_debug: tuple[str, str]|None = Lock.global_actor_in_debug
|
||||||
|
|
||||||
uid_in_debug: tuple[str, str] | None = Lock.global_actor_in_debug
|
actor: Actor = current_actor()
|
||||||
|
|
||||||
actor = current_actor()
|
|
||||||
# print(f'{actor.uid} in HANDLER with ')
|
|
||||||
|
|
||||||
def do_cancel():
|
def do_cancel():
|
||||||
# If we haven't tried to cancel the runtime then do that instead
|
# If we haven't tried to cancel the runtime then do that instead
|
||||||
|
@ -509,7 +611,7 @@ def shield_sigint_handler(
|
||||||
return do_cancel()
|
return do_cancel()
|
||||||
|
|
||||||
# only set in the actor actually running the REPL
|
# only set in the actor actually running the REPL
|
||||||
pdb_obj: MultiActorPdb | None = Lock.repl
|
pdb_obj: MultiActorPdb|None = Lock.repl
|
||||||
|
|
||||||
# root actor branch that reports whether or not a child
|
# root actor branch that reports whether or not a child
|
||||||
# has locked debugger.
|
# has locked debugger.
|
||||||
|
@ -616,14 +718,20 @@ _pause_msg: str = 'Attaching to pdb REPL in actor'
|
||||||
|
|
||||||
|
|
||||||
def _set_trace(
|
def _set_trace(
|
||||||
actor: tractor.Actor | None = None,
|
actor: tractor.Actor|None = None,
|
||||||
pdb: MultiActorPdb | None = None,
|
pdb: MultiActorPdb|None = None,
|
||||||
shield: bool = False,
|
shield: bool = False,
|
||||||
|
|
||||||
extra_frames_up_when_async: int = 1,
|
extra_frames_up_when_async: int = 1,
|
||||||
|
hide_tb: bool = True,
|
||||||
):
|
):
|
||||||
__tracebackhide__: bool = True
|
__tracebackhide__: bool = hide_tb
|
||||||
actor: tractor.Actor = actor or current_actor()
|
|
||||||
|
actor: tractor.Actor = (
|
||||||
|
actor
|
||||||
|
or
|
||||||
|
current_actor()
|
||||||
|
)
|
||||||
|
|
||||||
# always start 1 level up from THIS in user code.
|
# always start 1 level up from THIS in user code.
|
||||||
frame: FrameType|None
|
frame: FrameType|None
|
||||||
|
@ -669,20 +777,17 @@ def _set_trace(
|
||||||
f'Going up frame {i} -> {frame}\n'
|
f'Going up frame {i} -> {frame}\n'
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
# engage ze REPL
|
||||||
pdb, undo_sigint = mk_mpdb()
|
# B~()
|
||||||
|
|
||||||
# we entered the global ``breakpoint()`` built-in from sync
|
|
||||||
# code?
|
|
||||||
Lock.local_task_in_debug = 'sync'
|
|
||||||
|
|
||||||
pdb.set_trace(frame=frame)
|
pdb.set_trace(frame=frame)
|
||||||
|
|
||||||
|
|
||||||
async def _pause(
|
async def _pause(
|
||||||
|
|
||||||
debug_func: Callable = _set_trace,
|
debug_func: Callable = _set_trace,
|
||||||
release_lock_signal: trio.Event | None = None,
|
|
||||||
|
# NOTE: must be passed in the `.pause_from_sync()` case!
|
||||||
|
pdb: MultiActorPdb|None = None,
|
||||||
|
|
||||||
# TODO: allow caller to pause despite task cancellation,
|
# TODO: allow caller to pause despite task cancellation,
|
||||||
# exactly the same as wrapping with:
|
# exactly the same as wrapping with:
|
||||||
|
@ -691,9 +796,9 @@ async def _pause(
|
||||||
# => the REMAINING ISSUE is that the scope's .__exit__() frame
|
# => the REMAINING ISSUE is that the scope's .__exit__() frame
|
||||||
# is always show in the debugger on entry.. and there seems to
|
# is always show in the debugger on entry.. and there seems to
|
||||||
# be no way to override it?..
|
# be no way to override it?..
|
||||||
# shield: bool = False,
|
#
|
||||||
|
|
||||||
shield: bool = False,
|
shield: bool = False,
|
||||||
|
hide_tb: bool = True,
|
||||||
task_status: TaskStatus[trio.Event] = trio.TASK_STATUS_IGNORED
|
task_status: TaskStatus[trio.Event] = trio.TASK_STATUS_IGNORED
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -705,10 +810,16 @@ async def _pause(
|
||||||
Hopefully we won't need this in the long run.
|
Hopefully we won't need this in the long run.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
__tracebackhide__: bool = True
|
__tracebackhide__: bool = hide_tb
|
||||||
actor = current_actor()
|
actor: Actor = current_actor()
|
||||||
pdb, undo_sigint = mk_mpdb()
|
try:
|
||||||
task_name: str = trio.lowlevel.current_task().name
|
task_name: str = trio.lowlevel.current_task().name
|
||||||
|
except RuntimeError as rte:
|
||||||
|
if actor.is_infected_aio():
|
||||||
|
raise RuntimeError(
|
||||||
|
'`tractor.pause[_from_sync]()` not yet supported '
|
||||||
|
'for infected `asyncio` mode!'
|
||||||
|
) from rte
|
||||||
|
|
||||||
if (
|
if (
|
||||||
not Lock.local_pdb_complete
|
not Lock.local_pdb_complete
|
||||||
|
@ -716,9 +827,13 @@ async def _pause(
|
||||||
):
|
):
|
||||||
Lock.local_pdb_complete = trio.Event()
|
Lock.local_pdb_complete = trio.Event()
|
||||||
|
|
||||||
debug_func = partial(
|
if debug_func is not None:
|
||||||
debug_func,
|
debug_func = partial(
|
||||||
)
|
debug_func,
|
||||||
|
)
|
||||||
|
|
||||||
|
if pdb is None:
|
||||||
|
pdb: MultiActorPdb = mk_mpdb()
|
||||||
|
|
||||||
# TODO: need a more robust check for the "root" actor
|
# TODO: need a more robust check for the "root" actor
|
||||||
if (
|
if (
|
||||||
|
@ -767,6 +882,7 @@ async def _pause(
|
||||||
actor.uid,
|
actor.uid,
|
||||||
)
|
)
|
||||||
Lock.repl = pdb
|
Lock.repl = pdb
|
||||||
|
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
Lock.release()
|
Lock.release()
|
||||||
|
|
||||||
|
@ -811,32 +927,26 @@ async def _pause(
|
||||||
# TODO: do we want to support using this **just** for the
|
# TODO: do we want to support using this **just** for the
|
||||||
# locking / common code (prolly to help address #320)?
|
# locking / common code (prolly to help address #320)?
|
||||||
#
|
#
|
||||||
# if debug_func is None:
|
if debug_func is None:
|
||||||
# assert release_lock_signal, (
|
task_status.started(Lock)
|
||||||
# 'Must pass `release_lock_signal: trio.Event` if no '
|
|
||||||
# 'trace func provided!'
|
|
||||||
# )
|
|
||||||
# print(f"{actor.uid} ENTERING WAIT")
|
|
||||||
# with trio.CancelScope(shield=True):
|
|
||||||
# await release_lock_signal.wait()
|
|
||||||
|
|
||||||
# else:
|
else:
|
||||||
# block here one (at the appropriate frame *up*) where
|
# block here one (at the appropriate frame *up*) where
|
||||||
# ``breakpoint()`` was awaited and begin handling stdio.
|
# ``breakpoint()`` was awaited and begin handling stdio.
|
||||||
log.debug('Entering sync world of the `pdb` REPL..')
|
log.debug('Entering sync world of the `pdb` REPL..')
|
||||||
try:
|
try:
|
||||||
debug_func(
|
debug_func(
|
||||||
actor,
|
actor,
|
||||||
pdb,
|
pdb,
|
||||||
extra_frames_up_when_async=2,
|
extra_frames_up_when_async=2,
|
||||||
shield=shield,
|
shield=shield,
|
||||||
)
|
)
|
||||||
except BaseException:
|
except BaseException:
|
||||||
log.exception(
|
log.exception(
|
||||||
'Failed to invoke internal `debug_func = '
|
'Failed to invoke internal `debug_func = '
|
||||||
f'{debug_func.func.__name__}`\n'
|
f'{debug_func.func.__name__}`\n'
|
||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
except bdb.BdbQuit:
|
except bdb.BdbQuit:
|
||||||
Lock.release()
|
Lock.release()
|
||||||
|
@ -862,8 +972,7 @@ async def _pause(
|
||||||
|
|
||||||
async def pause(
|
async def pause(
|
||||||
|
|
||||||
debug_func: Callable = _set_trace,
|
debug_func: Callable|None = _set_trace,
|
||||||
release_lock_signal: trio.Event | None = None,
|
|
||||||
|
|
||||||
# TODO: allow caller to pause despite task cancellation,
|
# TODO: allow caller to pause despite task cancellation,
|
||||||
# exactly the same as wrapping with:
|
# exactly the same as wrapping with:
|
||||||
|
@ -872,10 +981,11 @@ async def pause(
|
||||||
# => the REMAINING ISSUE is that the scope's .__exit__() frame
|
# => the REMAINING ISSUE is that the scope's .__exit__() frame
|
||||||
# is always show in the debugger on entry.. and there seems to
|
# is always show in the debugger on entry.. and there seems to
|
||||||
# be no way to override it?..
|
# be no way to override it?..
|
||||||
# shield: bool = False,
|
#
|
||||||
|
|
||||||
shield: bool = False,
|
shield: bool = False,
|
||||||
task_status: TaskStatus[trio.Event] = trio.TASK_STATUS_IGNORED
|
task_status: TaskStatus[trio.Event] = trio.TASK_STATUS_IGNORED,
|
||||||
|
|
||||||
|
**_pause_kwargs,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
|
@ -920,89 +1030,166 @@ async def pause(
|
||||||
task_status.started(cs)
|
task_status.started(cs)
|
||||||
return await _pause(
|
return await _pause(
|
||||||
debug_func=debug_func,
|
debug_func=debug_func,
|
||||||
release_lock_signal=release_lock_signal,
|
|
||||||
shield=True,
|
shield=True,
|
||||||
task_status=task_status,
|
task_status=task_status,
|
||||||
|
**_pause_kwargs
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return await _pause(
|
return await _pause(
|
||||||
debug_func=debug_func,
|
debug_func=debug_func,
|
||||||
release_lock_signal=release_lock_signal,
|
|
||||||
shield=False,
|
shield=False,
|
||||||
task_status=task_status,
|
task_status=task_status,
|
||||||
|
**_pause_kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_gb_mod: None|ModuleType|False = None
|
||||||
|
|
||||||
|
|
||||||
|
def maybe_import_greenback(
|
||||||
|
raise_not_found: bool = True,
|
||||||
|
force_reload: bool = False,
|
||||||
|
|
||||||
|
) -> ModuleType|False:
|
||||||
|
# be cached-fast on module-already-inited
|
||||||
|
global _gb_mod
|
||||||
|
|
||||||
|
if _gb_mod is False:
|
||||||
|
return False
|
||||||
|
|
||||||
|
elif (
|
||||||
|
_gb_mod is not None
|
||||||
|
and not force_reload
|
||||||
|
):
|
||||||
|
return _gb_mod
|
||||||
|
|
||||||
|
try:
|
||||||
|
import greenback
|
||||||
|
_gb_mod = greenback
|
||||||
|
return greenback
|
||||||
|
|
||||||
|
except ModuleNotFoundError as mnf:
|
||||||
|
log.debug(
|
||||||
|
'`greenback` is not installed.\n'
|
||||||
|
'No sync debug support!\n'
|
||||||
|
)
|
||||||
|
_gb_mod = False
|
||||||
|
|
||||||
|
if raise_not_found:
|
||||||
|
raise RuntimeError(
|
||||||
|
'The `greenback` lib is required to use `tractor.pause_from_sync()`!\n'
|
||||||
|
'https://github.com/oremanj/greenback\n'
|
||||||
|
) from mnf
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def maybe_init_greenback(
|
||||||
|
**kwargs,
|
||||||
|
) -> None|ModuleType:
|
||||||
|
|
||||||
|
if mod := maybe_import_greenback(**kwargs):
|
||||||
|
await mod.ensure_portal()
|
||||||
|
log.info(
|
||||||
|
'`greenback` portal opened!\n'
|
||||||
|
'Sync debug support activated!\n'
|
||||||
|
)
|
||||||
|
return mod
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
# TODO: allow pausing from sync code.
|
# TODO: allow pausing from sync code.
|
||||||
# normally by remapping python's builtin breakpoint() hook to this
|
# normally by remapping python's builtin breakpoint() hook to this
|
||||||
# runtime aware version which takes care of all .
|
# runtime aware version which takes care of all .
|
||||||
def pause_from_sync() -> None:
|
def pause_from_sync(
|
||||||
print("ENTER SYNC PAUSE")
|
hide_tb: bool = False,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
__tracebackhide__: bool = hide_tb
|
||||||
actor: tractor.Actor = current_actor(
|
actor: tractor.Actor = current_actor(
|
||||||
err_on_no_runtime=False,
|
err_on_no_runtime=False,
|
||||||
)
|
)
|
||||||
if actor:
|
log.debug(
|
||||||
try:
|
f'{actor.uid}: JUST ENTERED `tractor.pause_from_sync()`'
|
||||||
import greenback
|
f'|_{actor}\n'
|
||||||
# __tracebackhide__ = True
|
)
|
||||||
|
if not actor:
|
||||||
|
raise RuntimeError(
|
||||||
|
'Not inside the `tractor`-runtime?\n'
|
||||||
|
'`tractor.pause_from_sync()` is not functional without a wrapping\n'
|
||||||
|
'- `async with tractor.open_nursery()` or,\n'
|
||||||
|
'- `async with tractor.open_root_actor()`\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
# NOTE: once supported, remove this AND the one
|
||||||
|
# inside `._pause()`!
|
||||||
|
if actor.is_infected_aio():
|
||||||
|
raise RuntimeError(
|
||||||
|
'`tractor.pause[_from_sync]()` not yet supported '
|
||||||
|
'for infected `asyncio` mode!'
|
||||||
|
)
|
||||||
|
|
||||||
# task_can_release_tty_lock = trio.Event()
|
# raises on not-found by default
|
||||||
|
greenback: ModuleType = maybe_import_greenback()
|
||||||
|
mdb: MultiActorPdb = mk_mpdb()
|
||||||
|
|
||||||
# spawn bg task which will lock out the TTY, we poll
|
# run async task which will lock out the root proc's TTY.
|
||||||
# just below until the release event is reporting that task as
|
if not Lock.is_main_trio_thread():
|
||||||
# waiting.. not the most ideal but works for now ;)
|
|
||||||
greenback.await_(
|
# TODO: we could also check for a non-`.to_thread` context
|
||||||
actor._service_n.start(partial(
|
# using `trio.from_thread.check_cancelled()` (says
|
||||||
pause,
|
# oremanj) wherein we get the following outputs:
|
||||||
debug_func=None,
|
#
|
||||||
# release_lock_signal=task_can_release_tty_lock,
|
# `RuntimeError`: non-`.to_thread` spawned thread
|
||||||
))
|
# noop: non-cancelled `.to_thread`
|
||||||
|
# `trio.Cancelled`: cancelled `.to_thread`
|
||||||
|
#
|
||||||
|
trio.from_thread.run(
|
||||||
|
partial(
|
||||||
|
pause,
|
||||||
|
debug_func=None,
|
||||||
|
pdb=mdb,
|
||||||
|
hide_tb=hide_tb,
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
# TODO: maybe the `trio.current_task()` id/name if avail?
|
||||||
|
Lock.local_task_in_debug: str = str(threading.current_thread().name)
|
||||||
|
|
||||||
except ModuleNotFoundError:
|
else: # we are presumably the `trio.run()` + main thread
|
||||||
log.warning('NO GREENBACK FOUND')
|
greenback.await_(
|
||||||
else:
|
pause(
|
||||||
log.warning('Not inside actor-runtime')
|
debug_func=None,
|
||||||
|
pdb=mdb,
|
||||||
|
hide_tb=hide_tb,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
Lock.local_task_in_debug: str = current_task().name
|
||||||
|
|
||||||
db, undo_sigint = mk_mpdb()
|
# TODO: ensure we aggressively make the user aware about
|
||||||
Lock.local_task_in_debug = 'sync'
|
# entering the global ``breakpoint()`` built-in from sync
|
||||||
# db.config.enable_hidden_frames = True
|
|
||||||
|
|
||||||
# we entered the global ``breakpoint()`` built-in from sync
|
|
||||||
# code?
|
# code?
|
||||||
frame: FrameType | None = sys._getframe()
|
_set_trace(
|
||||||
# print(f'FRAME: {str(frame)}')
|
actor=actor,
|
||||||
# assert not db._is_hidden(frame)
|
pdb=mdb,
|
||||||
|
hide_tb=hide_tb,
|
||||||
|
extra_frames_up_when_async=1,
|
||||||
|
|
||||||
frame: FrameType = frame.f_back # type: ignore
|
# TODO? will we ever need it?
|
||||||
# print(f'FRAME: {str(frame)}')
|
# -> the gb._await() won't be affected by cancellation?
|
||||||
# if not db._is_hidden(frame):
|
# shield=shield,
|
||||||
# pdbp.set_trace()
|
)
|
||||||
# db._hidden_frames.append(
|
# LEGACY NOTE on next LOC's frame showing weirdness..
|
||||||
# (frame, frame.f_lineno)
|
#
|
||||||
# )
|
# XXX NOTE XXX no other LOC can be here without it
|
||||||
db.set_trace(frame=frame)
|
# showing up in the REPL's last stack frame !?!
|
||||||
# NOTE XXX: see the `@pdbp.hideframe` decoration
|
# -[ ] tried to use `@pdbp.hideframe` decoration but
|
||||||
# on `Lock.unshield_sigint()`.. I have NO CLUE why
|
# still doesn't work
|
||||||
# the next instruction's def frame is being shown
|
|
||||||
# in the tb but it seems to be something wonky with
|
|
||||||
# the way `pdb` core works?
|
|
||||||
# undo_sigint()
|
|
||||||
|
|
||||||
# Lock.global_actor_in_debug = actor.uid
|
|
||||||
# Lock.release()
|
|
||||||
# task_can_release_tty_lock.set()
|
|
||||||
|
|
||||||
|
|
||||||
# using the "pause" semantics instead since
|
|
||||||
# that better covers actually somewhat "pausing the runtime"
|
|
||||||
# for this particular paralell task to do debugging B)
|
|
||||||
# pp = pause # short-hand for "pause point"
|
|
||||||
|
|
||||||
|
|
||||||
|
# NOTE prefer a new "pause" semantic since it better describes
|
||||||
|
# "pausing the actor's runtime" for this particular
|
||||||
|
# paralell task to do debugging in a REPL.
|
||||||
async def breakpoint(**kwargs):
|
async def breakpoint(**kwargs):
|
||||||
log.warning(
|
log.warning(
|
||||||
'`tractor.breakpoint()` is deprecated!\n'
|
'`tractor.breakpoint()` is deprecated!\n'
|
||||||
|
|
|
@ -23,12 +23,31 @@ into each ``trio.Nursery`` except it links the lifetimes of memory space
|
||||||
disjoint, parallel executing tasks in separate actors.
|
disjoint, parallel executing tasks in separate actors.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
from __future__ import annotations
|
||||||
|
import multiprocessing as mp
|
||||||
from signal import (
|
from signal import (
|
||||||
signal,
|
signal,
|
||||||
SIGUSR1,
|
SIGUSR1,
|
||||||
)
|
)
|
||||||
|
import traceback
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
|
from tractor import (
|
||||||
|
_state,
|
||||||
|
log as logmod,
|
||||||
|
)
|
||||||
|
|
||||||
|
log = logmod.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from tractor._spawn import ProcessType
|
||||||
|
from tractor import (
|
||||||
|
Actor,
|
||||||
|
ActorNursery,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@trio.lowlevel.disable_ki_protection
|
@trio.lowlevel.disable_ki_protection
|
||||||
def dump_task_tree() -> None:
|
def dump_task_tree() -> None:
|
||||||
|
@ -41,9 +60,15 @@ def dump_task_tree() -> None:
|
||||||
recurse_child_tasks=True
|
recurse_child_tasks=True
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
log = get_console_log('cancel')
|
log = get_console_log(
|
||||||
log.pdb(
|
name=__name__,
|
||||||
f'Dumping `stackscope` tree:\n\n'
|
level='cancel',
|
||||||
|
)
|
||||||
|
actor: Actor = _state.current_actor()
|
||||||
|
log.devx(
|
||||||
|
f'Dumping `stackscope` tree for actor\n'
|
||||||
|
f'{actor.name}: {actor}\n'
|
||||||
|
f' |_{mp.current_process()}\n\n'
|
||||||
f'{tree_str}\n'
|
f'{tree_str}\n'
|
||||||
)
|
)
|
||||||
# import logging
|
# import logging
|
||||||
|
@ -56,8 +81,13 @@ def dump_task_tree() -> None:
|
||||||
# ).exception("Error printing task tree")
|
# ).exception("Error printing task tree")
|
||||||
|
|
||||||
|
|
||||||
def signal_handler(sig: int, frame: object) -> None:
|
def signal_handler(
|
||||||
import traceback
|
sig: int,
|
||||||
|
frame: object,
|
||||||
|
|
||||||
|
relay_to_subs: bool = True,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
try:
|
try:
|
||||||
trio.lowlevel.current_trio_token(
|
trio.lowlevel.current_trio_token(
|
||||||
).run_sync_soon(dump_task_tree)
|
).run_sync_soon(dump_task_tree)
|
||||||
|
@ -65,6 +95,26 @@ def signal_handler(sig: int, frame: object) -> None:
|
||||||
# not in async context -- print a normal traceback
|
# not in async context -- print a normal traceback
|
||||||
traceback.print_stack()
|
traceback.print_stack()
|
||||||
|
|
||||||
|
if not relay_to_subs:
|
||||||
|
return
|
||||||
|
|
||||||
|
an: ActorNursery
|
||||||
|
for an in _state.current_actor()._actoruid2nursery.values():
|
||||||
|
|
||||||
|
subproc: ProcessType
|
||||||
|
subactor: Actor
|
||||||
|
for subactor, subproc, _ in an._children.values():
|
||||||
|
log.devx(
|
||||||
|
f'Relaying `SIGUSR1`[{sig}] to sub-actor\n'
|
||||||
|
f'{subactor}\n'
|
||||||
|
f' |_{subproc}\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
if isinstance(subproc, trio.Process):
|
||||||
|
subproc.send_signal(sig)
|
||||||
|
|
||||||
|
elif isinstance(subproc, mp.Process):
|
||||||
|
subproc._send_signal(sig)
|
||||||
|
|
||||||
|
|
||||||
def enable_stack_on_sig(
|
def enable_stack_on_sig(
|
||||||
|
@ -82,3 +132,6 @@ def enable_stack_on_sig(
|
||||||
# NOTE: not the above can be triggered from
|
# NOTE: not the above can be triggered from
|
||||||
# a (xonsh) shell using:
|
# a (xonsh) shell using:
|
||||||
# kill -SIGUSR1 @$(pgrep -f '<cmd>')
|
# kill -SIGUSR1 @$(pgrep -f '<cmd>')
|
||||||
|
#
|
||||||
|
# for example if you were looking to trace a `pytest` run
|
||||||
|
# kill -SIGUSR1 @$(pgrep -f 'pytest')
|
||||||
|
|
|
@ -21,6 +21,11 @@ Log like a forester!
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
|
from logging import (
|
||||||
|
LoggerAdapter,
|
||||||
|
Logger,
|
||||||
|
StreamHandler,
|
||||||
|
)
|
||||||
import colorlog # type: ignore
|
import colorlog # type: ignore
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
|
@ -48,20 +53,19 @@ LOG_FORMAT = (
|
||||||
|
|
||||||
DATE_FORMAT = '%b %d %H:%M:%S'
|
DATE_FORMAT = '%b %d %H:%M:%S'
|
||||||
|
|
||||||
LEVELS: dict[str, int] = {
|
# FYI, ERROR is 40
|
||||||
|
CUSTOM_LEVELS: dict[str, int] = {
|
||||||
'TRANSPORT': 5,
|
'TRANSPORT': 5,
|
||||||
'RUNTIME': 15,
|
'RUNTIME': 15,
|
||||||
'CANCEL': 16,
|
'DEVX': 17,
|
||||||
|
'CANCEL': 18,
|
||||||
'PDB': 500,
|
'PDB': 500,
|
||||||
}
|
}
|
||||||
# _custom_levels: set[str] = {
|
|
||||||
# lvlname.lower for lvlname in LEVELS.keys()
|
|
||||||
# }
|
|
||||||
|
|
||||||
STD_PALETTE = {
|
STD_PALETTE = {
|
||||||
'CRITICAL': 'red',
|
'CRITICAL': 'red',
|
||||||
'ERROR': 'red',
|
'ERROR': 'red',
|
||||||
'PDB': 'white',
|
'PDB': 'white',
|
||||||
|
'DEVX': 'cyan',
|
||||||
'WARNING': 'yellow',
|
'WARNING': 'yellow',
|
||||||
'INFO': 'green',
|
'INFO': 'green',
|
||||||
'CANCEL': 'yellow',
|
'CANCEL': 'yellow',
|
||||||
|
@ -78,7 +82,7 @@ BOLD_PALETTE = {
|
||||||
|
|
||||||
# TODO: this isn't showing the correct '{filename}'
|
# TODO: this isn't showing the correct '{filename}'
|
||||||
# as it did before..
|
# as it did before..
|
||||||
class StackLevelAdapter(logging.LoggerAdapter):
|
class StackLevelAdapter(LoggerAdapter):
|
||||||
|
|
||||||
def transport(
|
def transport(
|
||||||
self,
|
self,
|
||||||
|
@ -86,7 +90,8 @@ class StackLevelAdapter(logging.LoggerAdapter):
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
IPC level msg-ing.
|
IPC transport level msg IO; generally anything below
|
||||||
|
`._ipc.Channel` and friends.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
return self.log(5, msg)
|
return self.log(5, msg)
|
||||||
|
@ -102,11 +107,11 @@ class StackLevelAdapter(logging.LoggerAdapter):
|
||||||
msg: str,
|
msg: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Cancellation logging, mostly for runtime reporting.
|
Cancellation sequencing, mostly for runtime reporting.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
return self.log(
|
return self.log(
|
||||||
level=16,
|
level=22,
|
||||||
msg=msg,
|
msg=msg,
|
||||||
# stacklevel=4,
|
# stacklevel=4,
|
||||||
)
|
)
|
||||||
|
@ -116,11 +121,21 @@ class StackLevelAdapter(logging.LoggerAdapter):
|
||||||
msg: str,
|
msg: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Debugger logging.
|
`pdb`-REPL (debugger) related statuses.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
return self.log(500, msg)
|
return self.log(500, msg)
|
||||||
|
|
||||||
|
def devx(
|
||||||
|
self,
|
||||||
|
msg: str,
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
"Developer experience" sub-sys statuses.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self.log(17, msg)
|
||||||
|
|
||||||
def log(
|
def log(
|
||||||
self,
|
self,
|
||||||
level,
|
level,
|
||||||
|
@ -136,8 +151,7 @@ class StackLevelAdapter(logging.LoggerAdapter):
|
||||||
if self.isEnabledFor(level):
|
if self.isEnabledFor(level):
|
||||||
stacklevel: int = 3
|
stacklevel: int = 3
|
||||||
if (
|
if (
|
||||||
level in LEVELS.values()
|
level in CUSTOM_LEVELS.values()
|
||||||
# or level in _custom_levels
|
|
||||||
):
|
):
|
||||||
stacklevel: int = 4
|
stacklevel: int = 4
|
||||||
|
|
||||||
|
@ -184,8 +198,30 @@ class StackLevelAdapter(logging.LoggerAdapter):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO IDEAs:
|
||||||
|
# -[ ] move to `.devx.pformat`?
|
||||||
|
# -[ ] do per task-name and actor-name color coding
|
||||||
|
# -[ ] unique color per task-id and actor-uuid
|
||||||
|
def pformat_task_uid(
|
||||||
|
id_part: str = 'tail'
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Return `str`-ified unique for a `trio.Task` via a combo of its
|
||||||
|
`.name: str` and `id()` truncated output.
|
||||||
|
|
||||||
|
'''
|
||||||
|
task: trio.Task = trio.lowlevel.current_task()
|
||||||
|
tid: str = str(id(task))
|
||||||
|
if id_part == 'tail':
|
||||||
|
tid_part: str = tid[-6:]
|
||||||
|
else:
|
||||||
|
tid_part: str = tid[:6]
|
||||||
|
|
||||||
|
return f'{task.name}[{tid_part}]'
|
||||||
|
|
||||||
|
|
||||||
_conc_name_getters = {
|
_conc_name_getters = {
|
||||||
'task': lambda: trio.lowlevel.current_task().name,
|
'task': pformat_task_uid,
|
||||||
'actor': lambda: current_actor(),
|
'actor': lambda: current_actor(),
|
||||||
'actor_name': lambda: current_actor().name,
|
'actor_name': lambda: current_actor().name,
|
||||||
'actor_uid': lambda: current_actor().uid[1][:6],
|
'actor_uid': lambda: current_actor().uid[1][:6],
|
||||||
|
@ -193,7 +229,10 @@ _conc_name_getters = {
|
||||||
|
|
||||||
|
|
||||||
class ActorContextInfo(Mapping):
|
class ActorContextInfo(Mapping):
|
||||||
"Dyanmic lookup for local actor and task names"
|
'''
|
||||||
|
Dyanmic lookup for local actor and task names.
|
||||||
|
|
||||||
|
'''
|
||||||
_context_keys = (
|
_context_keys = (
|
||||||
'task',
|
'task',
|
||||||
'actor',
|
'actor',
|
||||||
|
@ -224,6 +263,7 @@ def get_logger(
|
||||||
'''Return the package log or a sub-logger for ``name`` if provided.
|
'''Return the package log or a sub-logger for ``name`` if provided.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
log: Logger
|
||||||
log = rlog = logging.getLogger(_root_name)
|
log = rlog = logging.getLogger(_root_name)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -266,7 +306,7 @@ def get_logger(
|
||||||
logger = StackLevelAdapter(log, ActorContextInfo())
|
logger = StackLevelAdapter(log, ActorContextInfo())
|
||||||
|
|
||||||
# additional levels
|
# additional levels
|
||||||
for name, val in LEVELS.items():
|
for name, val in CUSTOM_LEVELS.items():
|
||||||
logging.addLevelName(val, name)
|
logging.addLevelName(val, name)
|
||||||
|
|
||||||
# ensure customs levels exist as methods
|
# ensure customs levels exist as methods
|
||||||
|
@ -278,7 +318,7 @@ def get_logger(
|
||||||
def get_console_log(
|
def get_console_log(
|
||||||
level: str | None = None,
|
level: str | None = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> logging.LoggerAdapter:
|
) -> LoggerAdapter:
|
||||||
'''Get the package logger and enable a handler which writes to stderr.
|
'''Get the package logger and enable a handler which writes to stderr.
|
||||||
|
|
||||||
Yeah yeah, i know we can use ``DictConfig``. You do it.
|
Yeah yeah, i know we can use ``DictConfig``. You do it.
|
||||||
|
@ -303,7 +343,7 @@ def get_console_log(
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
handler = logging.StreamHandler()
|
handler = StreamHandler()
|
||||||
formatter = colorlog.ColoredFormatter(
|
formatter = colorlog.ColoredFormatter(
|
||||||
LOG_FORMAT,
|
LOG_FORMAT,
|
||||||
datefmt=DATE_FORMAT,
|
datefmt=DATE_FORMAT,
|
||||||
|
@ -323,3 +363,19 @@ def get_loglevel() -> str:
|
||||||
|
|
||||||
# global module logger for tractor itself
|
# global module logger for tractor itself
|
||||||
log = get_logger('tractor')
|
log = get_logger('tractor')
|
||||||
|
|
||||||
|
|
||||||
|
def at_least_level(
|
||||||
|
log: Logger|LoggerAdapter,
|
||||||
|
level: int|str,
|
||||||
|
) -> bool:
|
||||||
|
'''
|
||||||
|
Predicate to test if a given level is active.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if isinstance(level, str):
|
||||||
|
level: int = CUSTOM_LEVELS[level.upper()]
|
||||||
|
|
||||||
|
if log.getEffectiveLevel() <= level:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
|
@ -33,10 +33,14 @@ from typing import (
|
||||||
import trio
|
import trio
|
||||||
from outcome import Error
|
from outcome import Error
|
||||||
|
|
||||||
from .log import get_logger
|
from tractor.log import get_logger
|
||||||
from ._state import current_actor
|
from tractor._state import (
|
||||||
from ._exceptions import AsyncioCancelled
|
current_actor,
|
||||||
from .trionics._broadcast import (
|
debug_mode,
|
||||||
|
)
|
||||||
|
from tractor.devx import _debug
|
||||||
|
from tractor._exceptions import AsyncioCancelled
|
||||||
|
from tractor.trionics._broadcast import (
|
||||||
broadcast_receiver,
|
broadcast_receiver,
|
||||||
BroadcastReceiver,
|
BroadcastReceiver,
|
||||||
)
|
)
|
||||||
|
@ -64,9 +68,9 @@ class LinkedTaskChannel(trio.abc.Channel):
|
||||||
_trio_exited: bool = False
|
_trio_exited: bool = False
|
||||||
|
|
||||||
# set after ``asyncio.create_task()``
|
# set after ``asyncio.create_task()``
|
||||||
_aio_task: asyncio.Task | None = None
|
_aio_task: asyncio.Task|None = None
|
||||||
_aio_err: BaseException | None = None
|
_aio_err: BaseException|None = None
|
||||||
_broadcaster: BroadcastReceiver | None = None
|
_broadcaster: BroadcastReceiver|None = None
|
||||||
|
|
||||||
async def aclose(self) -> None:
|
async def aclose(self) -> None:
|
||||||
await self._from_aio.aclose()
|
await self._from_aio.aclose()
|
||||||
|
@ -158,7 +162,9 @@ def _run_asyncio_task(
|
||||||
'''
|
'''
|
||||||
__tracebackhide__ = True
|
__tracebackhide__ = True
|
||||||
if not current_actor().is_infected_aio():
|
if not current_actor().is_infected_aio():
|
||||||
raise RuntimeError("`infect_asyncio` mode is not enabled!?")
|
raise RuntimeError(
|
||||||
|
"`infect_asyncio` mode is not enabled!?"
|
||||||
|
)
|
||||||
|
|
||||||
# ITC (inter task comms), these channel/queue names are mostly from
|
# ITC (inter task comms), these channel/queue names are mostly from
|
||||||
# ``asyncio``'s perspective.
|
# ``asyncio``'s perspective.
|
||||||
|
@ -187,7 +193,7 @@ def _run_asyncio_task(
|
||||||
|
|
||||||
cancel_scope = trio.CancelScope()
|
cancel_scope = trio.CancelScope()
|
||||||
aio_task_complete = trio.Event()
|
aio_task_complete = trio.Event()
|
||||||
aio_err: BaseException | None = None
|
aio_err: BaseException|None = None
|
||||||
|
|
||||||
chan = LinkedTaskChannel(
|
chan = LinkedTaskChannel(
|
||||||
aio_q, # asyncio.Queue
|
aio_q, # asyncio.Queue
|
||||||
|
@ -253,7 +259,7 @@ def _run_asyncio_task(
|
||||||
if not inspect.isawaitable(coro):
|
if not inspect.isawaitable(coro):
|
||||||
raise TypeError(f"No support for invoking {coro}")
|
raise TypeError(f"No support for invoking {coro}")
|
||||||
|
|
||||||
task = asyncio.create_task(
|
task: asyncio.Task = asyncio.create_task(
|
||||||
wait_on_coro_final_result(
|
wait_on_coro_final_result(
|
||||||
to_trio,
|
to_trio,
|
||||||
coro,
|
coro,
|
||||||
|
@ -262,6 +268,18 @@ def _run_asyncio_task(
|
||||||
)
|
)
|
||||||
chan._aio_task = task
|
chan._aio_task = task
|
||||||
|
|
||||||
|
# XXX TODO XXX get this actually workin.. XD
|
||||||
|
# maybe setup `greenback` for `asyncio`-side task REPLing
|
||||||
|
if (
|
||||||
|
debug_mode()
|
||||||
|
and
|
||||||
|
(greenback := _debug.maybe_import_greenback(
|
||||||
|
force_reload=True,
|
||||||
|
raise_not_found=False,
|
||||||
|
))
|
||||||
|
):
|
||||||
|
greenback.bestow_portal(task)
|
||||||
|
|
||||||
def cancel_trio(task: asyncio.Task) -> None:
|
def cancel_trio(task: asyncio.Task) -> None:
|
||||||
'''
|
'''
|
||||||
Cancel the calling ``trio`` task on error.
|
Cancel the calling ``trio`` task on error.
|
||||||
|
@ -269,7 +287,7 @@ def _run_asyncio_task(
|
||||||
'''
|
'''
|
||||||
nonlocal chan
|
nonlocal chan
|
||||||
aio_err = chan._aio_err
|
aio_err = chan._aio_err
|
||||||
task_err: BaseException | None = None
|
task_err: BaseException|None = None
|
||||||
|
|
||||||
# only to avoid ``asyncio`` complaining about uncaptured
|
# only to avoid ``asyncio`` complaining about uncaptured
|
||||||
# task exceptions
|
# task exceptions
|
||||||
|
@ -349,11 +367,11 @@ async def translate_aio_errors(
|
||||||
'''
|
'''
|
||||||
trio_task = trio.lowlevel.current_task()
|
trio_task = trio.lowlevel.current_task()
|
||||||
|
|
||||||
aio_err: BaseException | None = None
|
aio_err: BaseException|None = None
|
||||||
|
|
||||||
# TODO: make thisi a channel method?
|
# TODO: make thisi a channel method?
|
||||||
def maybe_raise_aio_err(
|
def maybe_raise_aio_err(
|
||||||
err: Exception | None = None
|
err: Exception|None = None
|
||||||
) -> None:
|
) -> None:
|
||||||
aio_err = chan._aio_err
|
aio_err = chan._aio_err
|
||||||
if (
|
if (
|
||||||
|
@ -531,6 +549,16 @@ def run_as_asyncio_guest(
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
trio_done_fut = asyncio.Future()
|
trio_done_fut = asyncio.Future()
|
||||||
|
|
||||||
|
if debug_mode():
|
||||||
|
# XXX make it obvi we know this isn't supported yet!
|
||||||
|
log.error(
|
||||||
|
'Attempting to enter unsupported `greenback` init '
|
||||||
|
'from `asyncio` task..'
|
||||||
|
)
|
||||||
|
await _debug.maybe_init_greenback(
|
||||||
|
force_reload=True,
|
||||||
|
)
|
||||||
|
|
||||||
def trio_done_callback(main_outcome):
|
def trio_done_callback(main_outcome):
|
||||||
|
|
||||||
if isinstance(main_outcome, Error):
|
if isinstance(main_outcome, Error):
|
||||||
|
|
Loading…
Reference in New Issue