Skip to content

Flaky Django cache span tests in streaming mode #8027

Open

Description

@sentry-junior

tests/integrations/django/test_cache_module.py::test_cache_spans_item_size[True] and ::test_cache_spans_decorator[True] fail intermittently on master in streaming mode (span_streaming=True). Both tests make two requests, one trace each, and index the captured spans by position. Sometimes request 2's spans arrive first.

Failure

tests/integrations/django/test_cache_module.py:763: in test_cache_spans_item_size
    assert not spans[0]["attributes"]["cache.hit"]
E   assert not True

test_cache_spans_decorator[True] fails the same way at line 384.

Expected vs actual

  • Expected: spans[0] is request 1's cache.get, a miss.
  • Actual: spans[0] is request 2's cache.get, a hit. len(spans) == 7 still passes; only the order across the two traces changes.

CI failures on master (Sep 30 – Oct 13, 6 of 33 Test runs)

  • test_cache_spans_item_size[True]: py3.14 django-v6.1.2, 916942d (job); py3.14 django-v6.1.1, b5737a0 (job); py3.14 django-v6.1.1, 8afefe8 (job)
  • test_cache_spans_decorator[True]: py3.12 django-v4.2.30, 6a4eb20 (job); py3.14 django-v5.2.17, bf1e2f9 (job); py3.10 django-v5.2.17, 8012047 (job)
  • Always the Web 1 job, always [True] (streaming). It appears on Django 4.2 through 6.1 and Python 3.10 through 3.14.

Known trigger

  • In streaming mode, each finished segment adds its trace_id to SpanBatcher._pending_flush, which is a set (sentry_sdk/_span_batcher.py). Pending flushing was added in ref: Flush trace buckets when segment spans finish #7170.
  • If the background flusher wakes only after both requests' segments have finished, list(self._pending_flush) sends the two traces in set (hash) order instead of the order they finished. That happens about 50% of the time once both are pending.
  • On an idle machine the flusher usually sends request 1 before request 2 ends. A busy CI runner delays the flusher thread, so both end up pending.
  • test(django): Fix flaky cache tests by using uuid for cache LOCATION #7444 (uuid LOCATION for the cache) fixed a different flake in these tests. These failures all happened after it was merged.

via johdax.

--

View Junior Session [Sentry]

Activity

  1. linear-code commented on Oct 14, 2026

    @linear-code
  2. sentry-junior commented on Oct 14, 2026

    @sentry-junior
    ContributorAuthor

    Minimal reproduction (no Django): getsentry/repro#61. About 49% of iterations send the second trace's spans first when both segments are pending at the same time.

  3. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Oct 15, 2026
  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Oct 15, 2026
  5. sentry-junior commented on Nov 12, 2026

    @sentry-junior
    ContributorAuthor

    Reproduced in getsentry/repro#61. Draft fix: #8261, waiting for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugJunior: reproJunior reproduces this bug and drafts a fixPythonSDK

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions