Skip to content

Commit 93e57ad

Browse files
committed
chore: remove unnecessary tls
1 parent c70be85 commit 93e57ad

File tree

3 files changed

+20
-39
lines changed

3 files changed

+20
-39
lines changed

sentry_sdk/hub.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ def __exit__(self, exc_type, exc_value, tb):
3939

4040
class _ScopeManager(object):
4141

42-
def __init__(self, hub):
42+
def __init__(self, hub, layer):
4343
self._hub = hub
44+
self._layer = layer
4445

4546
def __enter__(self):
4647
return self
4748

4849
def __exit__(self, exc_type, exc_value, tb):
49-
self._hub._stack.pop()
50+
assert self._hub._stack.pop() == self._layer, 'popped wrong scope'
5051

5152

5253
class Hub(with_metaclass(HubMeta)):
@@ -162,11 +163,18 @@ def add_event_processor(self, factory):
162163
self._pending_processors.append(factory)
163164

164165
def push_scope(self):
165-
"""Pushes a new layer on the scope stack."""
166+
"""Pushes a new layer on the scope stack. Returns a context manager
167+
that should be used to pop the scope again."""
166168
self._flush_event_processors()
167169
client, scope = self._stack[-1]
168-
self._stack.append((client, copy.copy(scope)))
169-
return _ScopeManager(self)
170+
new_layer = (client, copy.copy(scope))
171+
self._stack.append(new_layer)
172+
return _ScopeManager(self, new_layer)
173+
174+
def pop_scope_unsafe(self):
175+
"""Pops a scope layer from the stack. Try to use the context manager
176+
`push_scope()` instead."""
177+
self._stack.pop()
170178

171179
@contextmanager
172180
def configure_scope(self):

sentry_sdk/integrations/django/__init__.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@
2424
def _get_transaction_from_request(request):
2525
return resolve(request.path).func.__name__
2626

27-
_request_scope = ContextVar('sentry_django_request_scope')
28-
29-
3027
# request_started (or any other signal) cannot be used because the request is
3128
# not yet available
3229
class SentryMiddleware(MiddlewareMixin):
3330
def process_request(self, request):
3431
try:
35-
assert _request_scope.get(None) is None, 'race condition'
36-
_request_scope.set(get_current_hub().push_scope().__enter__())
32+
get_current_hub().push_scope()
3733

3834
with configure_scope() as scope:
3935
scope.transaction = _get_transaction_from_request(request)
@@ -42,13 +38,7 @@ def process_request(self, request):
4238

4339

4440
def _request_finished(*args, **kwargs):
45-
try:
46-
val = _request_scope.get(None)
47-
assert val is not None, 'race condition'
48-
val.__exit__(None, None, None)
49-
_request_scope.set(None)
50-
except Exception:
51-
get_current_hub().capture_internal_exception()
41+
get_current_hub().pop_scope_unsafe()
5242

5343

5444
def _got_request_exception(request=None, **kwargs):

sentry_sdk/integrations/flask.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,12 @@ def init_app(self, app):
3131

3232

3333
def _push_appctx(*args, **kwargs):
34-
try:
35-
assert getattr(
36-
_app_ctx_stack.top,
37-
'_sentry_app_scope_manager',
38-
None
39-
) is None, 'race condition'
40-
_app_ctx_stack.top._sentry_app_scope_manager = \
41-
get_current_hub().push_scope().__enter__()
42-
except Exception:
43-
get_current_hub().capture_internal_exception()
34+
get_current_hub().push_scope()
35+
_app_ctx_stack.top._sentry_app_scope_pushed = True
4436

4537

4638
def _pop_appctx(exception):
47-
try:
48-
assert getattr(
49-
_app_ctx_stack.top,
50-
'_sentry_app_scope_manager',
51-
None
52-
) is not None, 'race condition'
53-
_app_ctx_stack.top._sentry_app_scope_manager.__exit__(None, None, None)
54-
_app_ctx_stack.top._sentry_app_scope_manager = None
55-
except Exception:
56-
get_current_hub().capture_internal_exception()
39+
get_current_hub().pop_scope_unsafe()
5740

5841

5942
def _capture_exception(sender, exception, **kwargs):
@@ -64,9 +47,9 @@ def _before_request(*args, **kwargs):
6447
try:
6548
assert getattr(
6649
_app_ctx_stack.top,
67-
'_sentry_app_scope_manager',
50+
'_sentry_app_scope_pushed',
6851
None
69-
) is not None, 'scope push failed'
52+
), 'scope push failed'
7053

7154
with configure_scope() as scope:
7255
if request.url_rule:

0 commit comments

Comments
 (0)