Skip to content

Fix propagate_scope=False in ThreadingIntegration #4310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: potel-base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sentry_sdk/integrations/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def _run_old_run_func():
with sentry_sdk.use_scope(current_scope_to_use):
return _run_old_run_func()
else:
with sentry_sdk.isolation_scope():
with sentry_sdk.isolation_scope() as scope:
scope.clear()
return _run_old_run_func()

return run # type: ignore
Expand Down
40 changes: 26 additions & 14 deletions tests/integrations/threading/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def do_some_work(number):
threads = []

with sentry_sdk.start_span(op="outer-trx"):
for number in range(5):
for number in range(2):
with sentry_sdk.start_span(
op=f"outer-submit-{number}", name="Thread: main"
):
Expand All @@ -243,32 +243,44 @@ def do_some_work(number):
for t in threads:
t.join()

(event,) = events
if propagate_scope:
# The children spans from the threads become parts of the existing span
# tree since we propagated the scope
assert len(events) == 1
(event,) = events

assert render_span_tree(event) == dedent(
"""\
- op="outer-trx": description=null
- op="outer-submit-0": description="Thread: main"
- op="inner-run-0": description="Thread: child-0"
- op="outer-submit-1": description="Thread: main"
- op="inner-run-1": description="Thread: child-1"
- op="outer-submit-2": description="Thread: main"
- op="inner-run-2": description="Thread: child-2"
- op="outer-submit-3": description="Thread: main"
- op="inner-run-3": description="Thread: child-3"
- op="outer-submit-4": description="Thread: main"
- op="inner-run-4": description="Thread: child-4"\
- op="inner-run-1": description="Thread: child-1"\
"""
)

elif not propagate_scope:
assert render_span_tree(event) == dedent(
# The spans from the threads become their own root spans/transactions
# as the connection to the parent span was severed when the scope was
# cleared
assert len(events) == 3
(event1, event2, event3) = sorted(events, key=render_span_tree)

assert render_span_tree(event1) == dedent(
"""\
- op="inner-run-0": description=null\
"""
)
assert render_span_tree(event2) == dedent(
"""\
- op="inner-run-1": description=null\
"""
)

assert render_span_tree(event3) == dedent(
"""\
- op="outer-trx": description=null
- op="outer-submit-0": description="Thread: main"
- op="outer-submit-1": description="Thread: main"
- op="outer-submit-2": description="Thread: main"
- op="outer-submit-3": description="Thread: main"
- op="outer-submit-4": description="Thread: main"\
- op="outer-submit-1": description="Thread: main"\
"""
)
Loading