8000 fix: Deal with out-of-order scope teardown in Flask tests · etherscan-io/sentry-python@6bdbbe4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bdbbe4

Browse files
committed
fix: Deal with out-of-order scope teardown in Flask tests
Fix getsentry#222
1 parent 0fa120e commit 6bdbbe4

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

sentry_sdk/integrations/flask.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,17 @@ def _push_appctx(*args, **kwargs):
6161
if hub.get_integration(FlaskIntegration) is not None:
6262
# always want to push scope regardless of whether WSGI app might already
6363
# have (not the case for CLI for example)
64-
hub.push_scope()
64+
scope_manager = hub.push_scope()
65+
scope_manager.__enter__()
66+
_app_ctx_stack.top.sentry_sdk_scope_manager = scope_manager
6567
with hub.configure_scope() as scope:
6668
scope._name = "flask"
6769

6870

6971
def _pop_appctx(*args, **kwargs):
70-
hub = Hub.current
71-
if hub.get_integration(FlaskIntegration) is not None:
72-
hub.pop_scope_unsafe()
72+
scope_manager = getattr(_app_ctx_stack.top, "sentry_sdk_scope_manager", None)
73+
if scope_manager is not None:
74+
scope_manager.__exit__(None, None, None)
7375

7476

7577
def _request_started(sender, **kwargs):

tests/integrations/flask/test_flask.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,15 @@ def generate():
495495

496496
with configure_scope() as scope:
497497
assert not scope._tags["request_data"]
498+
499+
500+
def test_scoped_test_client(sentry_init, app):
501+
sentry_init(integrations=[flask_sentry.FlaskIntegration()])
502+
503+
@app.route("/")
504+
def index():
505+
return "ok"
506+
507+
with app.test_client() as client:
508+
response = client.get("/")
509+
assert response.status_code == 200

0 commit comments

Comments
 (0)
0