Skip to content

fix(tests): Fix scope level pollution from span consumer tests#109105

Merged
joshuarli merged 3 commits intomasterfrom
fix/test-pollution-flusher-scope-level-109064
Feb 26, 2026
Merged

fix(tests): Fix scope level pollution from span consumer tests#109105
joshuarli merged 3 commits intomasterfrom
fix/test-pollution-flusher-scope-level-109064

Conversation

@joshuarli
Copy link
Member

@joshuarli joshuarli commented Feb 23, 2026

Summary

  • ProcessSpansStrategyFactory.create_with_partitions() and SpanFlusher.main() set sentry_sdk.get_isolation_scope().level = "warning", which pollutes the shared isolation scope and persists across tests
  • This is a test-only problem: in production the flusher runs as a separate process (multiprocessing.Process), so the scope is naturally isolated. The threading.Thread path is only used in tests (when produce_to_pipe is set)
  • Revert the production code changes from 32636e6 that added sentry_sdk.isolation_scope() wrapping and _main_loop extraction in SpanFlusher
  • Add a conftest fixture that resets the isolation scope level after each span consumer test instead

Fixes #109064

Test plan

  • Verified pytest tests/sentry/spans/consumers/process/test_consumer.py::test_basic[None] tests/sentry/testutils/thread_leaks/test_pytest.py::TestSentryCapture::test_capture_event_allowlisted fails without the conftest ('warning' != 'info')
  • Verified the same invocation passes with the conftest

The `ProcessSpansStrategyFactory.create_with_partitions()` method was
setting `sentry_sdk.get_isolation_scope().level = "warning"` directly,
which pollutes the shared isolation scope and persists across tests.
Similarly, `SpanFlusher.main()` was modifying the isolation scope in a
thread that shares the scope object with the parent thread.

Fix by removing the scope level setting from factory (the flusher is
the only component that captures sentry events) and wrapping the
flusher's scope setup in `sentry_sdk.isolation_scope()` to create an
isolated scope that doesn't leak.

Fixes #109064
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Feb 23, 2026
…of production changes

Revert the production code changes from 32636e6 that added
sentry_sdk.isolation_scope() wrapping and _main_loop extraction in
SpanFlusher. The scope pollution only occurs in tests (where the
flusher runs as a thread), not in production (where it runs as a
separate process).

Add a conftest fixture that resets the isolation scope level after
each test instead.

Fixes #109064
Co-Authored-By: Claude <noreply@anthropic.com>
@joshuarli joshuarli changed the title fix(spans): Prevent isolation scope level pollution from span consumer fix(tests): Fix scope level pollution from span consumer tests Feb 25, 2026
@joshuarli joshuarli marked this pull request as ready for review February 25, 2026 22:39
@joshuarli joshuarli requested review from a team as code owners February 25, 2026 22:39
@joshuarli joshuarli requested a review from rbro112 February 25, 2026 22:39
Move the reset_sentry_isolation_scope fixture from the local
tests/sentry/spans/consumers/process/conftest.py to tests/conftest.py
so it protects all tests globally.

Co-Authored-By: Claude <noreply@anthropic.com>
@joshuarli joshuarli merged commit 60870a1 into master Feb 26, 2026
94 checks passed
@joshuarli joshuarli deleted the fix/test-pollution-flusher-scope-level-109064 branch February 26, 2026 00:12
@github-actions github-actions bot locked and limited conversation to collaborators Mar 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components test-pollution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test pollution: tests/sentry/testutils/thread_leaks/test_pytest.py::TestSentryCapture::test_capture_event_allowlisted

2 participants