2020-07-23 17:23:55 +00:00
|
|
|
"""
|
|
|
|
Multi-core debugging for da peeps!
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
from functools import partial
|
2020-07-30 14:42:22 +00:00
|
|
|
from typing import Awaitable, Tuple, Optional, Callable
|
2020-07-23 17:23:55 +00:00
|
|
|
|
|
|
|
from async_generator import aclosing
|
|
|
|
import tractor
|
|
|
|
import trio
|
|
|
|
|
|
|
|
from .log import get_logger
|
|
|
|
|
2020-07-26 21:46:55 +00:00
|
|
|
try:
|
|
|
|
# wtf: only exported when installed in dev mode?
|
|
|
|
import pdbpp
|
|
|
|
except ImportError:
|
|
|
|
# pdbpp is installed in regular mode...
|
|
|
|
import pdb
|
|
|
|
assert pdb.xpm, "pdbpp is not installed?"
|
|
|
|
pdbpp = pdb
|
|
|
|
|
|
|
|
|
2020-07-23 17:23:55 +00:00
|
|
|
log = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['breakpoint', 'post_mortem']
|
|
|
|
|
|
|
|
|
2020-07-30 14:42:22 +00:00
|
|
|
# placeholder for function to set a ``trio.Event``
|
|
|
|
_pdb_release_hook: Optional[Callable] = None
|
2020-07-23 17:23:55 +00:00
|
|
|
|
|
|
|
|
2020-07-30 14:42:22 +00:00
|
|
|
class TractorConfig(pdbpp.DefaultConfig):
|
|
|
|
"""Custom ``pdbpp`` goodness.
|
|
|
|
"""
|
|
|
|
sticky_by_default = True
|
|
|
|
|
|
|
|
def teardown(self, _pdb):
|
|
|
|
_pdb_release_hook(_pdb)
|
2020-07-26 21:46:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PdbwTeardown(pdbpp.Pdb):
|
|
|
|
"""Add teardown hooks to the regular ``pdbpp.Pdb``.
|
|
|
|
"""
|
2020-07-30 14:42:22 +00:00
|
|
|
# override the pdbpp config with our coolio one
|
|
|
|
DefaultConfig = TractorConfig
|
|
|
|
|
2020-07-26 21:46:55 +00:00
|
|
|
# TODO: figure out how to dissallow recursive .set_trace() entry
|
|
|
|
# since that'll cause deadlock for us.
|
|
|
|
def set_continue(self):
|
|
|
|
super().set_continue()
|
|
|
|
self.config.teardown(self)
|
|
|
|
|
|
|
|
def set_quit(self):
|
|
|
|
super().set_quit()
|
|
|
|
self.config.teardown(self)
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: will be needed whenever we get to true remote debugging.
|
|
|
|
# XXX see https://github.com/goodboy/tractor/issues/130
|
|
|
|
|
2020-07-30 14:42:22 +00:00
|
|
|
# # TODO: is there some way to determine this programatically?
|
|
|
|
# _pdb_exit_patterns = tuple(
|
|
|
|
# str.encode(patt + "\n") for patt in (
|
|
|
|
# 'c', 'cont', 'continue', 'q', 'quit')
|
|
|
|
# )
|
|
|
|
|
2020-07-26 21:46:55 +00:00
|
|
|
# def subactoruid2proc(
|
|
|
|
# actor: 'Actor', # noqa
|
|
|
|
# uid: Tuple[str, str]
|
|
|
|
# ) -> trio.Process:
|
|
|
|
# n = actor._actoruid2nursery[uid]
|
|
|
|
# _, proc, _ = n._children[uid]
|
|
|
|
# return proc
|
|
|
|
|
|
|
|
# async def hijack_stdin():
|
|
|
|
# log.info(f"Hijacking stdin from {actor.uid}")
|
|
|
|
|
|
|
|
# trap std in and relay to subproc
|
|
|
|
# async_stdin = trio.wrap_file(sys.stdin)
|
|
|
|
|
|
|
|
# async with aclosing(async_stdin):
|
|
|
|
# async for msg in async_stdin:
|
|
|
|
# log.trace(f"Stdin input:\n{msg}")
|
|
|
|
# # encode to bytes
|
|
|
|
# bmsg = str.encode(msg)
|
|
|
|
|
|
|
|
# # relay bytes to subproc over pipe
|
|
|
|
# # await proc.stdin.send_all(bmsg)
|
|
|
|
|
|
|
|
# if bmsg in _pdb_exit_patterns:
|
|
|
|
# log.info("Closing stdin hijack")
|
|
|
|
# break
|
2020-07-23 17:23:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def _hijack_stdin_relay_to_child(
|
|
|
|
subactor_uid: Tuple[str, str]
|
|
|
|
) -> None:
|
|
|
|
actor = tractor.current_actor()
|
2020-07-26 21:46:55 +00:00
|
|
|
debug_lock = actor.statespace.setdefault(
|
|
|
|
'_debug_lock', trio.StrictFIFOLock()
|
|
|
|
)
|
2020-07-23 17:23:55 +00:00
|
|
|
|
2020-07-26 21:46:55 +00:00
|
|
|
log.debug(f"Actor {subactor_uid} is waiting on stdin hijack lock")
|
|
|
|
await debug_lock.acquire()
|
|
|
|
log.warning(f"Actor {subactor_uid} acquired stdin hijack lock")
|
2020-07-23 17:23:55 +00:00
|
|
|
|
2020-07-26 21:46:55 +00:00
|
|
|
# TODO: when we get to true remote debugging
|
|
|
|
# this will deliver stdin data
|
|
|
|
try:
|
|
|
|
# indicate to child that we've locked stdio
|
|
|
|
yield 'Locked'
|
2020-07-23 17:23:55 +00:00
|
|
|
|
2020-07-26 21:46:55 +00:00
|
|
|
# wait for cancellation of stream by child
|
|
|
|
await trio.sleep_forever()
|
2020-07-23 17:23:55 +00:00
|
|
|
|
2020-07-26 21:46:55 +00:00
|
|
|
# TODO: for remote debugging schedule hijacking in root scope
|
|
|
|
# (see above)
|
|
|
|
# actor._root_nursery.start_soon(hijack_stdin)
|
2020-07-23 17:23:55 +00:00
|
|
|
|
2020-07-26 21:46:55 +00:00
|
|
|
finally:
|
|
|
|
if debug_lock.locked():
|
|
|
|
debug_lock.release()
|
|
|
|
log.debug(f"Actor {subactor_uid} released stdin hijack lock")
|
2020-07-23 17:23:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
# XXX: We only make this sync in case someone wants to
|
|
|
|
# overload the ``breakpoint()`` built-in.
|
|
|
|
def _breakpoint(debug_func) -> Awaitable[None]:
|
|
|
|
"""``tractor`` breakpoint entry for engaging pdb machinery
|
|
|
|
in subactors.
|
|
|
|
"""
|
|
|
|
actor = tractor.current_actor()
|
2020-07-26 21:46:55 +00:00
|
|
|
do_unlock = trio.Event()
|
2020-07-23 17:23:55 +00:00
|
|
|
|
2020-07-26 21:46:55 +00:00
|
|
|
async def wait_for_parent_stdin_hijack(
|
|
|
|
task_status=trio.TASK_STATUS_IGNORED
|
|
|
|
):
|
2020-07-23 17:23:55 +00:00
|
|
|
|
|
|
|
# TODO: need a more robust check for the "root" actor
|
|
|
|
if actor._parent_chan:
|
|
|
|
async with tractor._portal.open_portal(
|
|
|
|
actor._parent_chan,
|
|
|
|
start_msg_loop=False,
|
2020-07-26 21:46:55 +00:00
|
|
|
shield=True,
|
2020-07-23 17:23:55 +00:00
|
|
|
) as portal:
|
|
|
|
# with trio.fail_after(1):
|
2020-07-26 21:46:55 +00:00
|
|
|
agen = await portal.run(
|
2020-07-23 17:23:55 +00:00
|
|
|
'tractor._debug',
|
|
|
|
'_hijack_stdin_relay_to_child',
|
|
|
|
subactor_uid=actor.uid,
|
|
|
|
)
|
2020-07-26 21:46:55 +00:00
|
|
|
async with aclosing(agen):
|
|
|
|
async for val in agen:
|
|
|
|
assert val == 'Locked'
|
|
|
|
task_status.started()
|
|
|
|
with trio.CancelScope(shield=True):
|
|
|
|
await do_unlock.wait()
|
|
|
|
|
|
|
|
# trigger cancellation of remote stream
|
|
|
|
break
|
|
|
|
|
|
|
|
log.debug(f"Child {actor} released parent stdio lock")
|
|
|
|
|
|
|
|
def unlock(_pdb):
|
|
|
|
do_unlock.set()
|
|
|
|
|
|
|
|
global _pdb_release_hook
|
|
|
|
_pdb_release_hook = unlock
|
|
|
|
|
|
|
|
async def _bp():
|
2020-07-30 14:42:22 +00:00
|
|
|
# this **must** be awaited by the caller and is done using the
|
|
|
|
# root nursery so that the debugger can continue to run without
|
|
|
|
# being restricted by the scope of a new task nursery.
|
|
|
|
await actor._root_nursery.start(wait_for_parent_stdin_hijack)
|
2020-07-23 17:23:55 +00:00
|
|
|
|
2020-07-30 14:42:22 +00:00
|
|
|
# block here one (at the appropriate frame *up* where
|
|
|
|
# ``breakpoint()`` was awaited and begin handling stdio
|
2020-07-23 17:23:55 +00:00
|
|
|
debug_func(actor)
|
|
|
|
|
2020-07-30 14:42:22 +00:00
|
|
|
return _bp() # user code **must** await this!
|
2020-07-23 17:23:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _set_trace(actor):
|
2020-07-26 21:46:55 +00:00
|
|
|
log.critical(f"\nAttaching pdb to actor: {actor.uid}\n")
|
|
|
|
PdbwTeardown().set_trace(
|
|
|
|
# start 2 levels up in user code
|
2020-07-23 17:23:55 +00:00
|
|
|
frame=sys._getframe().f_back.f_back,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
breakpoint = partial(
|
|
|
|
_breakpoint,
|
|
|
|
_set_trace,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _post_mortem(actor):
|
|
|
|
log.error(f"\nAttaching to pdb in crashed actor: {actor.uid}\n")
|
2020-07-26 21:46:55 +00:00
|
|
|
pdbpp.xpm(Pdb=PdbwTeardown)
|
2020-07-23 17:23:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
post_mortem = partial(
|
|
|
|
_breakpoint,
|
|
|
|
_post_mortem,
|
|
|
|
)
|