fix(tests): Fix scope level pollution from span consumer tests#109105
Merged
fix(tests): Fix scope level pollution from span consumer tests#109105
Conversation
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
…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>
1 task
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>
rbro112
approved these changes
Feb 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ProcessSpansStrategyFactory.create_with_partitions()andSpanFlusher.main()setsentry_sdk.get_isolation_scope().level = "warning", which pollutes the shared isolation scope and persists across testsmultiprocessing.Process), so the scope is naturally isolated. Thethreading.Threadpath is only used in tests (whenproduce_to_pipeis set)sentry_sdk.isolation_scope()wrapping and_main_loopextraction inSpanFlusherFixes #109064
Test plan
pytest tests/sentry/spans/consumers/process/test_consumer.py::test_basic[None] tests/sentry/testutils/thread_leaks/test_pytest.py::TestSentryCapture::test_capture_event_allowlistedfails without the conftest ('warning' != 'info')