Move maybe-raise-error-msg logic into context
A context method handling all this logic makes the most sense since it contains all the state related to whether the error should be raised in a nursery scope or is expected to be raised by a consumer task which reads and processes the msg directly (via a `Portal` API call). This also makes it easy to always process remote errors even when there is no (stream) overrun condition.stricter_context_starting
parent
1f8e1cccbb
commit
c9132de7dc
|
@ -650,62 +650,38 @@ class Actor:
|
||||||
return
|
return
|
||||||
|
|
||||||
send_chan = ctx._send_chan
|
send_chan = ctx._send_chan
|
||||||
assert send_chan
|
|
||||||
|
|
||||||
error = msg.get('error')
|
|
||||||
if error:
|
|
||||||
# If this is an error message from a context opened by
|
|
||||||
# ``Portal.open_context()`` we want to interrupt any ongoing
|
|
||||||
# (child) tasks within that context to be notified of the remote
|
|
||||||
# error relayed here.
|
|
||||||
#
|
|
||||||
# The reason we may want to raise the remote error immediately
|
|
||||||
# is that there is no guarantee the associated local task(s)
|
|
||||||
# will attempt to read from any locally opened stream any time
|
|
||||||
# soon.
|
|
||||||
#
|
|
||||||
# NOTE: this only applies when
|
|
||||||
# ``Portal.open_context()`` has been called since it is assumed
|
|
||||||
# (currently) that other portal APIs (``Portal.run()``,
|
|
||||||
# ``.run_in_actor()``) do their own error checking at the point
|
|
||||||
# of the call and result processing.
|
|
||||||
log.warning(f'Remote context for {chan.uid}:{cid} errored {msg}')
|
|
||||||
ctx._maybe_error_from_remote_msg(msg)
|
|
||||||
|
|
||||||
try:
|
|
||||||
log.runtime(f"Delivering {msg} from {chan.uid} to caller {cid}")
|
log.runtime(f"Delivering {msg} from {chan.uid} to caller {cid}")
|
||||||
|
|
||||||
# XXX: we do **not** maintain backpressure and instead
|
# XXX: we do **not** maintain backpressure and instead
|
||||||
# opt to relay stream overrun errors to the sender.
|
# opt to relay stream overrun errors to the sender.
|
||||||
try:
|
try:
|
||||||
send_chan.send_nowait(msg)
|
send_chan.send_nowait(msg)
|
||||||
except trio.WouldBlock:
|
# if an error is deteced we should always
|
||||||
|
# expect it to be raised by any context (stream)
|
||||||
|
# consumer task
|
||||||
|
await ctx._maybe_raise_from_remote_msg(msg)
|
||||||
|
|
||||||
# XXX: do we need this?
|
except trio.WouldBlock:
|
||||||
# if we're trying to push an error but we're in
|
# XXX: always push an error even if the local
|
||||||
# an overrun state we'll just get stuck sending
|
# receiver is in overrun state.
|
||||||
# the error that was sent to us back to it's sender
|
await ctx._maybe_raise_from_remote_msg(msg)
|
||||||
# instead of it actually being raises in the target
|
|
||||||
# task..
|
|
||||||
# if error:
|
|
||||||
# raise unpack_error(msg, chan) from None
|
|
||||||
|
|
||||||
uid = chan.uid
|
uid = chan.uid
|
||||||
|
|
||||||
lines = [
|
lines = [
|
||||||
'Task context stream was overrun',
|
'Task context stream was overrun',
|
||||||
f'local task: {cid} @ {self.uid}',
|
f'local task: {cid} @ {self.uid}',
|
||||||
f'remote sender: {chan.uid}',
|
f'remote sender: {uid}',
|
||||||
]
|
]
|
||||||
if not ctx._stream_opened:
|
if not ctx._stream_opened:
|
||||||
lines.insert(
|
lines.insert(
|
||||||
1,
|
1,
|
||||||
f'\n*** No stream open on `{self.uid[0]}` side! ***\n'
|
f'\n*** No stream open on `{self.uid[0]}` side! ***\n'
|
||||||
)
|
)
|
||||||
|
|
||||||
text = '\n'.join(lines)
|
text = '\n'.join(lines)
|
||||||
log.warning(text)
|
|
||||||
if ctx._backpressure:
|
if ctx._backpressure:
|
||||||
|
log.warning(text)
|
||||||
await send_chan.send(msg)
|
await send_chan.send(msg)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
|
@ -713,15 +689,14 @@ class Actor:
|
||||||
except StreamOverrun as err:
|
except StreamOverrun as err:
|
||||||
err_msg = pack_error(err)
|
err_msg = pack_error(err)
|
||||||
err_msg['cid'] = cid
|
err_msg['cid'] = cid
|
||||||
await chan.send(err_msg)
|
try:
|
||||||
|
|
||||||
except trio.BrokenResourceError:
|
|
||||||
# TODO: what is the right way to handle the case where the
|
# TODO: what is the right way to handle the case where the
|
||||||
# local task has already sent a 'stop' / StopAsyncInteration
|
# local task has already sent a 'stop' / StopAsyncInteration
|
||||||
# to the other side but and possibly has closed the local
|
# to the other side but and possibly has closed the local
|
||||||
# feeder mem chan? Do we wait for some kind of ack or just
|
# feeder mem chan? Do we wait for some kind of ack or just
|
||||||
# let this fail silently and bubble up (currently)?
|
# let this fail silently and bubble up (currently)?
|
||||||
|
await chan.send(err_msg)
|
||||||
|
except trio.BrokenResourceError:
|
||||||
# XXX: local consumer has closed their side
|
# XXX: local consumer has closed their side
|
||||||
# so cancel the far end streaming task
|
# so cancel the far end streaming task
|
||||||
log.warning(f"{send_chan} consumer is already closed")
|
log.warning(f"{send_chan} consumer is already closed")
|
||||||
|
|
|
@ -364,19 +364,41 @@ class Context:
|
||||||
async def send_stop(self) -> None:
|
async def send_stop(self) -> None:
|
||||||
await self.chan.send({'stop': True, 'cid': self.cid})
|
await self.chan.send({'stop': True, 'cid': self.cid})
|
||||||
|
|
||||||
def _maybe_error_from_remote_msg(
|
async def _maybe_raise_from_remote_msg(
|
||||||
self,
|
self,
|
||||||
msg: Dict[str, Any],
|
msg: Dict[str, Any],
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Unpack and raise a msg error into the local scope
|
(Maybe) unpack and raise a msg error into the local scope
|
||||||
nursery for this context.
|
nursery for this context.
|
||||||
|
|
||||||
Acts as a form of "relay" for a remote error raised
|
Acts as a form of "relay" for a remote error raised
|
||||||
in the corresponding remote callee task.
|
in the corresponding remote callee task.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
error = msg.get('error')
|
||||||
|
if error:
|
||||||
|
# If this is an error message from a context opened by
|
||||||
|
# ``Portal.open_context()`` we want to interrupt any ongoing
|
||||||
|
# (child) tasks within that context to be notified of the remote
|
||||||
|
# error relayed here.
|
||||||
|
#
|
||||||
|
# The reason we may want to raise the remote error immediately
|
||||||
|
# is that there is no guarantee the associated local task(s)
|
||||||
|
# will attempt to read from any locally opened stream any time
|
||||||
|
# soon.
|
||||||
|
#
|
||||||
|
# NOTE: this only applies when
|
||||||
|
# ``Portal.open_context()`` has been called since it is assumed
|
||||||
|
# (currently) that other portal APIs (``Portal.run()``,
|
||||||
|
# ``.run_in_actor()``) do their own error checking at the point
|
||||||
|
# of the call and result processing.
|
||||||
|
log.error(
|
||||||
|
f'Remote context error for {self.chan.uid}:{self.cid}:\n'
|
||||||
|
f'{msg["error"]["tb_str"]}'
|
||||||
|
)
|
||||||
|
# await ctx._maybe_error_from_remote_msg(msg)
|
||||||
self._error = unpack_error(msg, self.chan)
|
self._error = unpack_error(msg, self.chan)
|
||||||
|
|
||||||
# TODO: tempted to **not** do this by-reraising in a
|
# TODO: tempted to **not** do this by-reraising in a
|
||||||
|
|
Loading…
Reference in New Issue