Skip to content

ref: Flush trace buckets when segment spans finish - #7170

Merged
alexander-alderman-webb merged 22 commits into
masterfrom
webb/flush-segment-buckets
Aug 12, 2026
Merged

ref: Flush trace buckets when segment spans finish#7170
alexander-alderman-webb merged 22 commits into
masterfrom
webb/flush-segment-buckets

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Description

Mark the corresponding bucket as pending in the span buffer when a segment span is added.

This aims to keep memory pressure low. Segment spans typically finish after their children. There may be multiple segments in a trace, and flushing more frequently leads to more outbound network requests in these cases.

Adapt span batcher tests by adding an outer segment in most tests. As a result, the various flush conditions are still exercised as the assertions run before the segment span has finished (finishing the segment span otherwise flushes the buffer as well).

The tests that exercise multiple buckets are changed to use traces.new_trace() in combination with a segment span that's left open

Without changes to the sentry_init fixture, tests fail on free-threading. This is because the background flusher collects pending items before sentry_sdk.flush() can synchronously flush all everything. The background flush is asynchronous and creates a race conditions.

Issues

Reminders

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

105016 passed | ⏭️ 6677 skipped | Total: 111693 | Pass Rate: 94.02% | Execution Time: 367m 48s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +6292
Passed Tests 📈 +6094
Failed Tests
Skipped Tests 📈 +198

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 2484 uncovered lines.
✅ Project coverage is 90.15%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    90.15%    90.15%        —%
==========================================
  Files          193       193         —
  Lines        25215     25215         —
  Branches      9224      9224         —
==========================================
+ Hits         22730     22731        +1
- Misses        2485      2484        -1
- Partials      1433      1431        -2

Generated by Codecov Action

@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review August 11, 2026 14:46
@alexander-alderman-webb
alexander-alderman-webb requested a review from a team as a code owner August 11, 2026 14:46
Comment thread sentry_sdk/_span_batcher.py Outdated
Comment thread sentry_sdk/_span_batcher.py Outdated
@alexander-alderman-webb
alexander-alderman-webb marked this pull request as draft August 11, 2026 15:08
Base automatically changed from webb/batcher/remove-global-limits to master August 12, 2026 11:34
@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review August 12, 2026 11:45

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 54334a3. Configure here.

Comment thread tests/conftest.py
Comment thread tests/conftest.py
Comment on lines +301 to +303
The span batcher flushes pending items asynchronously with the main thread.
Flushes triggered by segments finishing are asynchronous, and can collect buckets
that would have otherwise been flushed synchronously by `sentry_sdk.flush()`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm finding this comment a little tricky to follow. My understanding of this section is:

  • The span batcher sends out pending items in the background, separately from the main thread.
  • When a segment finishes, that triggers a background flush too
  • The background flush in point 2 can end up sending buckets that sentry_sdk.flush() would otherwise have sent immediately.

Do I have this right? And why is point 3 problematic? Is it because we need those buckets immediately when invoking sentry_sdk.flush() and we may not get them because those buckets are in the process of being flushed by the background process?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's correct! To add some links:

Each batcher has a flushing thread that runs the _flush_loop() method.
When a segment span finishes, _flush() is triggered inside the thread here:

self._flush(only_pending=True)

This is what I called "asynchronous" in the comment.
When you call sentry_sdk.flush(), it triggers the flush here

which is not in the flushing thread. This runs synchronously with the user code.

@alexander-alderman-webb alexander-alderman-webb changed the title ref: Flush trace bucket when segment span finishes ref: Flush trace buckets when segment spans finish Aug 12, 2026
@alexander-alderman-webb
alexander-alderman-webb merged commit 1c3b50d into master Aug 12, 2026
144 checks passed
@alexander-alderman-webb
alexander-alderman-webb deleted the webb/flush-segment-buckets branch August 12, 2026 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants