Skip to content

fix: flaky test_handler by isolating trace context in log handler tests#114491

Merged
joshuarli merged 1 commit intomasterfrom
fix-trace-context-test-pollution
Apr 30, 2026
Merged

fix: flaky test_handler by isolating trace context in log handler tests#114491
joshuarli merged 1 commit intomasterfrom
fix-trace-context-test-pollution

Conversation

@joshuarli
Copy link
Copy Markdown
Member

fixes flakes like https://github.com/getsentry/sentry/actions/runs/25186909243/job/73846655451?pr=114485

this has been verified on my shuffle-tests-v5 branch as well

get_trace_id() reads sentry_sdk.get_current_scope().span. When a preceding test leaves an active span on the shared thread-local scope, test_emit and test_gke_emit receive a real trace ID instead of None and fail their assertions.

alternative solution doesn't work:

sentry_sdk.isolation_scope() alone doesn't help — it shallow-copies the current scope and carries the active span over. The new reset_trace_context() helper wraps isolation_scope() and explicitly zeroes scope.span, giving the enclosed call a clean slate. Applied surgically at the two call sites that assert trace_id=None.

@joshuarli joshuarli requested a review from a team as a code owner April 30, 2026 20:48
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 30, 2026
@joshuarli joshuarli enabled auto-merge (squash) April 30, 2026 20:52
@joshuarli joshuarli merged commit 35b5806 into master Apr 30, 2026
60 checks passed
@joshuarli joshuarli deleted the fix-trace-context-test-pollution branch April 30, 2026 20:59
cleptric pushed a commit that referenced this pull request May 5, 2026
…ests (#114491)

fixes flakes like
https://github.com/getsentry/sentry/actions/runs/25186909243/job/73846655451?pr=114485

this has been verified on my `shuffle-tests-v5` branch as well

`get_trace_id()` reads `sentry_sdk.get_current_scope().span`. When a
preceding test leaves an active span on the shared thread-local scope,
`test_emit` and `test_gke_emit` receive a real trace ID instead of
`None` and fail their assertions.

alternative solution doesn't work:

`sentry_sdk.isolation_scope()` alone doesn't help — it shallow-copies
the current scope and carries the active span over. The new
`reset_trace_context()` helper wraps `isolation_scope()` and explicitly
zeroes `scope.span`, giving the enclosed call a clean slate. Applied
surgically at the two call sites that assert `trace_id=None`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants