From 5aabcbaa62edae0384adf40e9968758c806db6b3 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Mon, 22 Jun 2026 14:40:20 +0200 Subject: [PATCH 1/2] fix: Stop unconditionally importing contextvars --- sentry_sdk/utils.py | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 875d28c0d0..cfd1113b8e 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -84,29 +84,6 @@ _installed_modules = None -_is_sentry_internal_task = contextvars.ContextVar( - "is_sentry_internal_task", default=False -) - -# These exceptions won't set the span status to error if they occur. Use -# register_control_flow_exception to add to this list -_control_flow_exception_classes: "set[type]" = set() - - -def is_internal_task() -> bool: - return _is_sentry_internal_task.get() - - -@contextmanager -def mark_sentry_task_internal() -> "Generator[None, None, None]": - """Context manager to mark a task as Sentry internal.""" - token = _is_sentry_internal_task.set(True) - try: - yield - finally: - _is_sentry_internal_task.reset(token) - - BASE64_ALPHABET = re.compile(r"^[a-zA-Z0-9/+=]*$") FALSY_ENV_VALUES = frozenset(("false", "f", "n", "no", "off", "0")) @@ -1468,6 +1445,28 @@ def _get_contextvars() -> "Tuple[bool, type]": Please refer to https://docs.sentry.io/platforms/python/contextvars/ for more information. """ +_is_sentry_internal_task = contextvars.ContextVar( + "is_sentry_internal_task", default=False +) + +# These exceptions won't set the span status to error if they occur. Use +# register_control_flow_exception to add to this list +_control_flow_exception_classes: "set[type]" = set() + + +def is_internal_task() -> bool: + return _is_sentry_internal_task.get() + + +@contextmanager +def mark_sentry_task_internal() -> "Generator[None, None, None]": + """Context manager to mark a task as Sentry internal.""" + token = _is_sentry_internal_task.set(True) + try: + yield + finally: + _is_sentry_internal_task.reset(token) + def qualname_from_function(func: "Callable[..., Any]") -> "Optional[str]": """Return the qualified name of func. Works with regular function, lambda, partial and partialmethod.""" From c4cf8eb261ee02e4f22c87df96c3ba3f2f54ddcf Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Mon, 22 Jun 2026 14:41:06 +0200 Subject: [PATCH 2/2] . --- sentry_sdk/utils.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index cfd1113b8e..4e362cae54 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1,5 +1,4 @@ import base64 -import contextvars import copy import json import linecache @@ -1445,9 +1444,7 @@ def _get_contextvars() -> "Tuple[bool, type]": Please refer to https://docs.sentry.io/platforms/python/contextvars/ for more information. """ -_is_sentry_internal_task = contextvars.ContextVar( - "is_sentry_internal_task", default=False -) +_is_sentry_internal_task = ContextVar("is_sentry_internal_task", default=False) # These exceptions won't set the span status to error if they occur. Use # register_control_flow_exception to add to this list