diff --git a/tractor/devx/_stackscope.py b/tractor/devx/_stackscope.py index aab313eb..8e1605fd 100644 --- a/tractor/devx/_stackscope.py +++ b/tractor/devx/_stackscope.py @@ -381,11 +381,13 @@ def enable_stack_on_sig( message=r"coroutine method '(asend|athrow)' .* was never awaited", ) import stackscope + _state._runtime_vars['use_stackscope'] = True except ImportError: log.warning( 'The `stackscope` lib is not installed!\n' '`Ignoring enable_stack_on_sig() call!\n' ) + assert not _state._runtime_vars['use_stackscope'] return None # Capture the trio token if we're inside `trio.run` diff --git a/tractor/runtime/_runtime.py b/tractor/runtime/_runtime.py index 8b6780cd..1111d30f 100644 --- a/tractor/runtime/_runtime.py +++ b/tractor/runtime/_runtime.py @@ -940,32 +940,30 @@ class Actor: # `tractor._testing.pytest`'s `--enable-stackscope` # CLI flag — env var propagates via fork-inherited # environ). - import os - if ( - rvs['_debug_mode'] - or - os.environ.get('TRACTOR_ENABLE_STACKSCOPE') - ): - from ..devx import ( - enable_stack_on_sig, - maybe_init_greenback, - ) - try: - # TODO: maybe return some status msgs upward - # to that we can emit them in `con_status` - # instead? - log.devx( - 'Enabling `stackscope` traces on SIGUSR1' - ) - enable_stack_on_sig() + if rvs['_debug_mode']: + if ( + rvs.get('use_stackscope') + or + os.environ.get('TRACTOR_ENABLE_STACKSCOPE') + ): + from ..devx import enable_stack_on_sig + try: + # TODO: maybe return some status msgs upward + # to that we can emit them in `con_status` + # instead? + log.devx( + 'Enabling `stackscope` traces on SIGUSR1' + ) + enable_stack_on_sig() - except ImportError: - log.warning( - '`stackscope` not installed for use in ' - 'debug mode / `--enable-stackscope`!' - ) + except ImportError: + log.warning( + '`stackscope` not installed for use in ' + 'debug mode / `--enable-stackscope`!' + ) if rvs.get('use_greenback', False): + from ..devx import maybe_init_greenback maybe_mod: ModuleType|None = await maybe_init_greenback() if maybe_mod: log.devx( diff --git a/tractor/runtime/_state.py b/tractor/runtime/_state.py index b9316448..11e0c0fd 100644 --- a/tractor/runtime/_state.py +++ b/tractor/runtime/_state.py @@ -93,6 +93,7 @@ class RuntimeVars(Struct): repl_fixture: bool|Callable = False # |AbstractContextManager[bool] # for `tractor.pause_from_sync()` & `breakpoint()` support use_greenback: bool = False + use_stackscope: bool = False # infected-`asyncio`-mode: `trio` running as guest. _is_infected_aio: bool = False @@ -139,8 +140,9 @@ _RUNTIME_VARS_DEFAULTS: dict[str, Any] = { # `debug_mode: bool` settings '_debug_mode': False, # bool 'repl_fixture': False, # |AbstractContextManager[bool] - # for `tractor.pause_from_sync()` & `breakpoint()` support - 'use_greenback': False, + + 'use_greenback': False, # `.pause_from_sync()`/`breakpoint()` + 'use_stackscope': False, # trio-task-stack dumps on SIGUSR1 # infected-`asyncio`-mode: `trio` running as guest. '_is_infected_aio': False,