Skip to content

Commit 6ebc29a

Browse files
use same version gating as Thread.start
1 parent f51b4f0 commit 6ebc29a

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

sentry_sdk/integrations/threading.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ def setup_once():
6161
django_version = None
6262
channels_version = None
6363

64+
is_async_emulated_with_threads = (
65+
sys.version_info < (3, 9)
66+
and channels_version is not None
67+
and channels_version < "4.0.0"
68+
and django_version is not None
69+
and django_version >= (3, 0)
70+
and django_version < (4, 0)
71+
)
72+
6473
@wraps(old_start)
6574
def sentry_start(self, *a, **kw):
6675
# type: (Thread, *Any, **Any) -> Any
@@ -69,14 +78,7 @@ def sentry_start(self, *a, **kw):
6978
return old_start(self, *a, **kw)
7079

7180
if integration.propagate_scope:
72-
if (
73-
sys.version_info < (3, 9)
74-
and channels_version is not None
75-
and channels_version < "4.0.0"
76-
and django_version is not None
77-
and django_version >= (3, 0)
78-
and django_version < (4, 0)
79-
):
81+
if is_async_emulated_with_threads:
8082
warnings.warn(
8183
"There is a known issue with Django channels 2.x and 3.x when using Python 3.8 or older. "
8284
"(Async support is emulated using threads and some Sentry data may be leaked between those threads.) "
@@ -111,7 +113,9 @@ def sentry_start(self, *a, **kw):
111113
return old_start(self, *a, **kw)
112114

113115
Thread.start = sentry_start # type: ignore
114-
ThreadPoolExecutor.submit = _wrap_threadpool_executor_submit(ThreadPoolExecutor.submit) # type: ignore
116+
ThreadPoolExecutor.submit = _wrap_threadpool_executor_submit(
117+
ThreadPoolExecutor.submit, is_async_emulated_with_threads
118+
) # type: ignore
115119

116120

117121
def _wrap_run(isolation_scope_to_use, current_scope_to_use, old_run_func):
@@ -137,8 +141,8 @@ def _run_old_run_func():
137141
return run # type: ignore
138142

139143

140-
def _wrap_threadpool_executor_submit(func):
141-
# type: (Callable[..., Future[T]]) -> Callable[..., Future[T]]
144+
def _wrap_threadpool_executor_submit(func, is_async_emulated_with_threads):
145+
# type: (Callable[..., Future[T]], bool) -> Callable[..., Future[T]]
142146
"""
143147
Wrap submit call to propagate scopes on task submission.
144148
"""
@@ -150,7 +154,10 @@ def sentry_submit(self, fn, *args, **kwargs):
150154
if integration is None:
151155
return func(self, fn, *args, **kwargs)
152156

153-
if integration.propagate_scope:
157+
if integration.propagate_scope and is_async_emulated_with_threads:
158+
isolation_scope = sentry_sdk.get_isolation_scope()
159+
current_scope = sentry_sdk.get_current_scope()
160+
elif integration.propagate_scope:
154161
isolation_scope = sentry_sdk.get_isolation_scope().fork()
155162
current_scope = sentry_sdk.get_current_scope().fork()
156163
else:

0 commit comments

Comments
 (0)