From aef306465db09d0df5f52de93aba1abe6f111fbf Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 28 Jul 2025 12:50:06 -0400 Subject: [PATCH] Add `never_warn_on: dict` support to unmasker Such that key->value pairs can be defined which should *never be* unmasked where values of - the keys are exc-types which might be masked, and - the values are exc-types which masked the equivalent key. For example, the default includes: - KBI->taskc: a kbi should never be unmasked from its masking `trio.Cancelled`. For the impl, a new `do_warn: bool` in the fn-body determines the primary guard for whether a warning or re-raising is necessary. --- tractor/trionics/_taskc.py | 52 +++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/tractor/trionics/_taskc.py b/tractor/trionics/_taskc.py index 4e336b59..b2503434 100644 --- a/tractor/trionics/_taskc.py +++ b/tractor/trionics/_taskc.py @@ -22,7 +22,10 @@ from __future__ import annotations from contextlib import ( asynccontextmanager as acm, ) -from typing import TYPE_CHECKING +from typing import ( + Type, + TYPE_CHECKING, +) import trio from tractor.log import get_logger @@ -80,9 +83,19 @@ async def maybe_raise_from_masking_exc( # ^TODO? other cases? ), - always_warn_on: tuple[BaseException] = ( + always_warn_on: tuple[Type[BaseException]] = ( trio.Cancelled, ), + + # don't ever unmask or warn on any masking pair, + # { -> } + never_warn_on: dict[ + Type[BaseException], + Type[BaseException], + ] = { + KeyboardInterrupt: trio.Cancelled, + trio.Cancelled: trio.Cancelled, + }, # ^XXX, special case(s) where we warn-log bc likely # there will be no operational diff since the exc # is always expected to be consumed. @@ -144,7 +157,10 @@ async def maybe_raise_from_masking_exc( maybe_masker=exc_match, unmask_from=set(unmask_from), ): - masked.append((exc_ctx, exc_match)) + masked.append(( + exc_ctx, + exc_match, + )) boxed_maybe_exc.value = exc_match note: str = ( f'\n' @@ -156,18 +172,36 @@ async def maybe_raise_from_masking_exc( f'\n' f'{extra_note}\n' ) - exc_ctx.add_note(note) - if type(exc_match) in always_warn_on: + do_warn: bool = ( + never_warn_on.get( + type(exc_ctx) # masking type + ) + is not + type(exc_match) # masked type + ) + + if do_warn: + exc_ctx.add_note(note) + + if ( + do_warn + and + type(exc_match) in always_warn_on + ): log.warning(note) - if raise_unmasked: - + if ( + do_warn + and + raise_unmasked + ): if len(masked) < 2: raise exc_ctx from exc_match + + # ??TODO, see above but, possibly unmasking sub-exc + # entries if there are > 1 # else: - # # ?TODO, see above but, possibly unmasking sub-exc - # # entries if there are > 1 # await pause(shield=True) else: raise