Skip to content

Commit 3608b8d

Browse files
committed
Instantiate new scopes in threading when propagate is false
1 parent a964676 commit 3608b8d

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

sentry_sdk/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from sentry_sdk.scope import Scope
1+
# TODO-neel scope switch
2+
# TODO-neel avoid duplication between api and __init__
3+
from sentry_sdk.opentelemetry.scope import PotelScope as Scope
24
from sentry_sdk.transport import Transport, HttpTransport
35
from sentry_sdk.client import Client
46

sentry_sdk/integrations/threading.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from threading import Thread, current_thread
55

66
import sentry_sdk
7+
from sentry_sdk import Scope
8+
from sentry_sdk.scope import ScopeType
79
from sentry_sdk.integrations import Integration
810
from sentry_sdk.utils import (
911
event_from_exception,
@@ -75,8 +77,8 @@ def sentry_start(self, *a, **kw):
7577
isolation_scope = sentry_sdk.get_isolation_scope().fork()
7678
current_scope = sentry_sdk.get_current_scope().fork()
7779
else:
78-
isolation_scope = None
79-
current_scope = None
80+
isolation_scope = Scope(ty=ScopeType.ISOLATION)
81+
current_scope = Scope(ty=ScopeType.CURRENT)
8082

8183
# Patching instance methods in `start()` creates a reference cycle if
8284
# done in a naive way. See
@@ -98,7 +100,7 @@ def sentry_start(self, *a, **kw):
98100

99101

100102
def _wrap_run(isolation_scope_to_use, current_scope_to_use, old_run_func):
101-
# type: (Optional[sentry_sdk.Scope], Optional[sentry_sdk.Scope], F) -> F
103+
# type: (sentry_sdk.Scope, sentry_sdk.Scope, F) -> F
102104
@wraps(old_run_func)
103105
def run(*a, **kw):
104106
# type: (*Any, **Any) -> Any
@@ -110,13 +112,8 @@ def _run_old_run_func():
110112
except Exception:
111113
reraise(*_capture_exception())
112114

113-
if isolation_scope_to_use is not None and current_scope_to_use is not None:
114-
with sentry_sdk.use_isolation_scope(isolation_scope_to_use):
115-
with sentry_sdk.use_scope(current_scope_to_use):
116-
return _run_old_run_func()
117-
else:
118-
with sentry_sdk.isolation_scope() as scope:
119-
scope.clear()
115+
with sentry_sdk.use_isolation_scope(isolation_scope_to_use):
116+
with sentry_sdk.use_scope(current_scope_to_use):
120117
return _run_old_run_func()
121118

122119
return run # type: ignore

0 commit comments

Comments
 (0)