Skip to content

fix(tracing): Flush pending span buckets in segment finish order - #8261

Closed
sentry-junior[bot] wants to merge 1 commit into
masterfrom
fix/8027-span-batcher-flush-order
Closed

sentry-junior[bot] wants to merge 1 commit into
masterfrom
fix/8027-span-batcher-flush-order

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #8027
Repro: getsentry/repro#61

Cause
In streaming mode, every finished segment adds its trace ID to SpanBatcher._pending_flush, and that attribute was a set. When the flusher thread wakes after two or more segments have finished, list(self._pending_flush) sends the traces in hash order, not the order the segments finished. That is why test_cache_spans_item_size[True] and test_cache_spans_decorator[True] flake on busy CI runners: request 2's spans sometimes arrive first.

Fix
Make _pending_flush an insertion-ordered dict[str, None], so pending traces are sent in the order they became ready. No other behavior changes.

Validation

  • New test_pending_traces_flush_in_segment_finish_order holds the flusher back until 10 segments have finished. It fails on master (trace order scrambled) and passes with the fix (3 of 3 runs).
  • tests/tracing on py3.14-common passes. ruff check, ruff format --check, and mypy sentry_sdk pass.
  • Repro: with sentry-sdk==2.72.0 it exits 1 (164 and 154 of 300 iterations out of order with --delay-flusher, 92 of 200 with natural timing). With this branch installed editable it exits 0 (0 out of order in all three runs).
  • Not run locally: tests/integrations/django/test_cache_module.py, because it needs Postgres, which the sandbox doesn't have.

via automation evt_986cce2def3acf1f0c69ed3fe2462a31 · Run ID dispatch_b69ae416eae39d09bca4bd53df50133b.

--

View Session [Sentry]

SpanBatcher._pending_flush was a set, so when the flusher woke after
several segments had finished, it sent their traces in hash order.
Use an insertion-ordered dict instead.

Fixes #8027
@sentry-junior sentry-junior Bot added the junior-automation:repro PR from the Junior SDK repro flow label Nov 12, 2026
@sentry-junior
sentry-junior Bot requested a review from a team November 12, 2026 12:20
@github-actions

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139239 passed | ⏭️ 7861 skipped | Total: 147100 | Pass Rate: 94.66% | Execution Time: 441m 8s

All tests are passing successfully.

❌ Patch coverage is 66.67% (2 of 3 changed executable lines covered; target 80%).
Project statement coverage is 90.24% (up 0.03 percentage points from base (860a060) to head (4ed7e7b)).

Changed files with executable lines (1)
File Patch coverage Changed executable lines
sentry_sdk/_span_batcher.py 66.67% 2/3 covered; missed: 88
Coverage diff
@@            Coverage Diff             @@
##        master     #8261       +/-##
==========================================
+ Coverage    90.21%    90.24%    +0.03%
==========================================
  Files          203       203         —
  Tracked lines     27335     27335         —
  Branches     10246     10246         —
==========================================
+ Hits         24660     24667        +7
+ Misses        2675      2668        -7
  Partials      1559      1559         —

Generated by Coverage Action

@sentrivana sentrivana closed this Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

junior-automation:repro PR from the Junior SDK repro flow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky Django cache span tests in streaming mode

1 participant