Use `Context.[peer_]side` in ctxc messages

runtime_to_msgspec
Tyler Goodlet 2024-04-25 16:19:39 -04:00
parent 4bab998ff9
commit adba454d1d
1 changed files with 14 additions and 9 deletions

View File

@ -592,7 +592,7 @@ async def _invoke(
if cs.cancel_called: if cs.cancel_called:
canceller: tuple = ctx.canceller canceller: tuple = ctx.canceller
msg: str = 'actor was cancelled by ' explain: str = f'{ctx.side!r}-side task was cancelled by '
# NOTE / TODO: if we end up having # NOTE / TODO: if we end up having
# ``Actor._cancel_task()`` call # ``Actor._cancel_task()`` call
@ -602,24 +602,28 @@ async def _invoke(
if ctx._cancel_called: if ctx._cancel_called:
# TODO: test for this!!!!! # TODO: test for this!!!!!
canceller: tuple = our_uid canceller: tuple = our_uid
msg += 'itself ' explain += 'itself '
# if the channel which spawned the ctx is the # if the channel which spawned the ctx is the
# one that cancelled it then we report that, vs. # one that cancelled it then we report that, vs.
# it being some other random actor that for ex. # it being some other random actor that for ex.
# some actor who calls `Portal.cancel_actor()` # some actor who calls `Portal.cancel_actor()`
# and by side-effect cancels this ctx. # and by side-effect cancels this ctx.
#
# TODO: determine if the ctx peer task was the
# exact task which cancelled, vs. some other
# task in the same actor.
elif canceller == ctx.chan.uid: elif canceller == ctx.chan.uid:
msg += 'its caller' explain += f'its {ctx.peer_side!r}-side peer'
else: else:
msg += 'a remote peer' explain += 'a remote peer'
# TODO: move this "div centering" into # TODO: move this "div centering" into
# a helper for use elsewhere! # a helper for use elsewhere!
div_chars: str = '------ - ------' div_chars: str = '------ - ------'
div_offset: int = ( div_offset: int = (
round(len(msg)/2)+1 round(len(explain)/2)+1
+ +
round(len(div_chars)/2)+1 round(len(div_chars)/2)+1
) )
@ -630,11 +634,12 @@ async def _invoke(
+ +
f'{div_chars}\n' f'{div_chars}\n'
) )
msg += ( explain += (
div_str + div_str +
f'<= canceller: {canceller}\n' f'<= canceller: {canceller}\n'
f'=> uid: {our_uid}\n' f'=> cancellee: {our_uid}\n'
f' |_{ctx._task}()' # TODO: better repr for ctx tasks..
f' |_{ctx.side!r} {ctx._task}'
# TODO: instead just show the # TODO: instead just show the
# ctx.__str__() here? # ctx.__str__() here?
@ -653,7 +658,7 @@ async def _invoke(
# task, so relay this cancel signal to the # task, so relay this cancel signal to the
# other side. # other side.
ctxc = ContextCancelled( ctxc = ContextCancelled(
message=msg, message=explain,
boxed_type=trio.Cancelled, boxed_type=trio.Cancelled,
canceller=canceller, canceller=canceller,
) )