Skip to content

fix(transport): Keep the longest active rate limit - #8297

Open
sentry-junior[bot] wants to merge 1 commit into
masterfrom
fix/transport-keep-longest-rate-limit
Open

sentry-junior[bot] wants to merge 1 commit into
masterfrom
fix/transport-keep-longest-rate-limit

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

Description

HttpTransportCore stored the last rate limit it saw for each category, so a shorter limit replaced a longer one that was still active. This happened for repeated categories in one X-Sentry-Rate-Limits header (3600:error:organization, 0:error:project left errors unlimited), across responses, and for the all-categories limit that a 429 sets. The spec requires keeping the longer limit.

The deadlines now live in a private _RateLimits class in sentry_sdk/transport.py. Its only write operation, update(category, until), keeps the later of the stored and the new deadline. Its read methods are is_active(category) and any_active(). Both update paths in _update_rate_limits (the X-Sentry-Rate-Limits header, and Retry-After or the 60-second default on a 429) call update, and the sync and async transports share that code through HttpTransportCore. Because the type enforces the rule, there is no separate fix per update path.

What enforces the rule: _RateLimits has no setter, no __setitem__, and no other mapping methods. Its backing dict is name-mangled (__deadlines), and __slots__ prevents adding attributes to an instance. Python cannot enforce full privacy: code can still reach the dict as _RateLimits__deadlines, or replace transport._disabled_until with a new object.

_disabled_until keeps its name but is no longer a dict. It is a private attribute, so public API is unchanged.

This follows the fix for the same bug in the Rust SDK: getsentry/sentry-rust#1409, reported by @itsrayeedscodespace, fixed in getsentry/sentry-rust#1410 with a RateLimitTimeout type whose only mutator keeps the later deadline.

Issues

via @szokeasaurusrex.

--

View Session [Sentry]

A shorter rate limit replaced a longer one that was still active, both for repeated categories in one X-Sentry-Rate-Limits header and across responses, including the all-categories limit set by a 429. The SDK then sent that category before the longer quota expired.

The deadlines are now stored in a private _RateLimits class. Its only write operation, update, keeps the later of the stored and the new deadline, so no update path can shorten an active limit. The backing dict is name-mangled and the class uses __slots__.

Fixes #8296

Co-Authored-By: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com>
@szokeasaurusrex
szokeasaurusrex marked this pull request as ready for review November 16, 2026 13:09
@szokeasaurusrex
szokeasaurusrex requested a review from a team as a code owner November 16, 2026 13:09
@github-actions

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139245 passed | ⏭️ 7857 skipped | Total: 147102 | Pass Rate: 94.66% | Execution Time: 436m 10s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +17
Passed Tests 📈 +17
Failed Tests —
Skipped Tests —

All tests are passing successfully.

✅ Patch coverage is 85.71% (18 of 21 changed executable lines covered; target 80%).
Project statement coverage is 90.22% (down 0.01 percentage points from base (e540bd6) to head (bbb516b)).

Changed files with executable lines (1)
File Patch coverage Changed executable lines
sentry_sdk/transport.py 85.71% 18/21 covered; missed: 239, 240, 500
Coverage diff
@@            Coverage Diff             @@
##        master     #8297       +/-##
==========================================
- Coverage    90.23%    90.22%    -0.01%
==========================================
  Files          203       203         —
  Tracked lines     27335     27348       +13
  Branches     10246     10250        +4
==========================================
+ Hits         24663     24672        +9
- Misses        2672      2676        +4
- Partials      1556      1558        +2

Generated by Coverage Action

This branch has not been deployed

No deployments
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.

Rate limits from later headers shorten longer active limits

1 participant