2021-12-13 18:08:32 +00:00
|
|
|
# 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/>.
|
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
"""
|
|
|
|
Our classy exception set.
|
2021-12-13 18:08:32 +00:00
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
"""
|
2022-09-15 20:56:50 +00:00
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
Optional,
|
|
|
|
Type,
|
|
|
|
)
|
2019-01-01 20:58:38 +00:00
|
|
|
import importlib
|
2018-11-19 08:51:12 +00:00
|
|
|
import builtins
|
|
|
|
import traceback
|
|
|
|
|
2022-10-09 17:12:50 +00:00
|
|
|
import exceptiongroup as eg
|
2019-10-30 04:32:10 +00:00
|
|
|
import trio
|
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
|
2019-01-01 20:58:38 +00:00
|
|
|
_this_mod = importlib.import_module(__name__)
|
|
|
|
|
|
|
|
|
2021-07-31 17:56:26 +00:00
|
|
|
class ActorFailure(Exception):
|
|
|
|
"General actor failure"
|
|
|
|
|
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
class RemoteActorError(Exception):
|
|
|
|
# TODO: local recontruction of remote exception deats
|
|
|
|
"Remote actor exception bundled locally"
|
2021-06-13 22:01:49 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
message: str,
|
2021-06-14 00:19:52 +00:00
|
|
|
suberror_type: Optional[Type[BaseException]] = None,
|
2021-06-13 22:01:49 +00:00
|
|
|
**msgdata
|
|
|
|
|
|
|
|
) -> None:
|
2018-11-19 08:51:12 +00:00
|
|
|
super().__init__(message)
|
2019-01-01 20:58:38 +00:00
|
|
|
|
2021-06-13 22:01:49 +00:00
|
|
|
self.type = suberror_type
|
2018-11-19 08:51:12 +00:00
|
|
|
self.msgdata = msgdata
|
|
|
|
|
|
|
|
|
|
|
|
class InternalActorError(RemoteActorError):
|
|
|
|
"""Remote internal ``tractor`` error indicating
|
|
|
|
failure of some primitive or machinery.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
class TransportClosed(trio.ClosedResourceError):
|
|
|
|
"Underlying channel transport was closed prior to use"
|
|
|
|
|
2021-07-31 17:56:26 +00:00
|
|
|
|
2021-06-13 22:01:49 +00:00
|
|
|
class ContextCancelled(RemoteActorError):
|
|
|
|
"Inter-actor task context cancelled itself on the callee side."
|
|
|
|
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
class NoResult(RuntimeError):
|
|
|
|
"No final result is expected for this actor"
|
|
|
|
|
|
|
|
|
2019-01-12 22:55:28 +00:00
|
|
|
class ModuleNotExposed(ModuleNotFoundError):
|
2019-01-01 20:58:38 +00:00
|
|
|
"The requested module is not exposed for RPC"
|
|
|
|
|
|
|
|
|
2021-04-27 15:07:53 +00:00
|
|
|
class NoRuntime(RuntimeError):
|
|
|
|
"The root actor has not been initialized yet"
|
|
|
|
|
|
|
|
|
2021-12-05 23:28:02 +00:00
|
|
|
class StreamOverrun(trio.TooSlowError):
|
|
|
|
"This stream was overrun by sender"
|
|
|
|
|
|
|
|
|
2021-11-23 21:19:19 +00:00
|
|
|
class AsyncioCancelled(Exception):
|
|
|
|
'''
|
|
|
|
Asyncio cancelled translation (non-base) error
|
|
|
|
for use with the ``to_asyncio`` module
|
|
|
|
to be raised in the ``trio`` side task
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
2021-06-27 15:37:35 +00:00
|
|
|
def pack_error(
|
|
|
|
exc: BaseException,
|
2021-07-31 17:56:26 +00:00
|
|
|
tb=None,
|
2021-06-27 15:37:35 +00:00
|
|
|
|
2022-09-15 20:56:50 +00:00
|
|
|
) -> dict[str, Any]:
|
2018-11-19 08:51:12 +00:00
|
|
|
"""Create an "error message" for tranmission over
|
|
|
|
a channel (aka the wire).
|
|
|
|
"""
|
2021-06-27 15:37:35 +00:00
|
|
|
if tb:
|
|
|
|
tb_str = ''.join(traceback.format_tb(tb))
|
|
|
|
else:
|
|
|
|
tb_str = traceback.format_exc()
|
|
|
|
|
2018-11-19 08:51:12 +00:00
|
|
|
return {
|
|
|
|
'error': {
|
2021-06-27 15:37:35 +00:00
|
|
|
'tb_str': tb_str,
|
2018-11-19 08:51:12 +00:00
|
|
|
'type_str': type(exc).__name__,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-25 20:07:36 +00:00
|
|
|
def unpack_error(
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2022-09-15 20:56:50 +00:00
|
|
|
msg: dict[str, Any],
|
2020-12-25 20:07:36 +00:00
|
|
|
chan=None,
|
|
|
|
err_type=RemoteActorError
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2020-12-25 20:07:36 +00:00
|
|
|
) -> Exception:
|
2022-10-12 21:41:01 +00:00
|
|
|
'''
|
|
|
|
Unpack an 'error' message from the wire
|
2018-11-19 08:51:12 +00:00
|
|
|
into a local ``RemoteActorError``.
|
2021-06-24 22:49:51 +00:00
|
|
|
|
2022-10-12 21:41:01 +00:00
|
|
|
'''
|
|
|
|
__tracebackhide__ = True
|
2021-06-13 22:01:49 +00:00
|
|
|
error = msg['error']
|
|
|
|
|
|
|
|
tb_str = error.get('tb_str', '')
|
|
|
|
message = f"{chan.uid}\n" + tb_str
|
|
|
|
type_name = error['type_str']
|
2021-06-14 00:19:52 +00:00
|
|
|
suberror_type: Type[BaseException] = Exception
|
2021-06-13 22:01:49 +00:00
|
|
|
|
|
|
|
if type_name == 'ContextCancelled':
|
|
|
|
err_type = ContextCancelled
|
|
|
|
suberror_type = trio.Cancelled
|
|
|
|
|
|
|
|
else: # try to lookup a suitable local error type
|
2022-10-09 17:12:50 +00:00
|
|
|
for ns in [
|
|
|
|
builtins,
|
|
|
|
_this_mod,
|
|
|
|
eg,
|
|
|
|
trio,
|
|
|
|
]:
|
2021-06-13 22:01:49 +00:00
|
|
|
try:
|
|
|
|
suberror_type = getattr(ns, type_name)
|
|
|
|
break
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
exc = err_type(
|
|
|
|
message,
|
|
|
|
suberror_type=suberror_type,
|
|
|
|
|
|
|
|
# unpack other fields into error type init
|
2018-11-19 08:51:12 +00:00
|
|
|
**msg['error'],
|
|
|
|
)
|
2020-12-25 20:07:36 +00:00
|
|
|
|
2021-06-13 22:01:49 +00:00
|
|
|
return exc
|
|
|
|
|
2020-12-25 20:07:36 +00:00
|
|
|
|
|
|
|
def is_multi_cancelled(exc: BaseException) -> bool:
|
2022-10-09 17:12:50 +00:00
|
|
|
'''
|
|
|
|
Predicate to determine if a possible ``eg.BaseExceptionGroup`` contains
|
|
|
|
only ``trio.Cancelled`` sub-exceptions (and is likely the result of
|
2020-12-25 20:07:36 +00:00
|
|
|
cancelling a collection of subtasks.
|
|
|
|
|
2022-10-09 17:12:50 +00:00
|
|
|
'''
|
|
|
|
if isinstance(exc, eg.BaseExceptionGroup):
|
|
|
|
return exc.subgroup(
|
|
|
|
lambda exc: isinstance(exc, trio.Cancelled)
|
|
|
|
) is not None
|
|
|
|
|
|
|
|
return False
|