Skip to content

fix(transport): retain the longest active rate limit - #1410

Merged
szokeasaurusrex merged 3 commits into
getsentry:masterfrom
itsrayeedscodespace:fix/preserve-longer-rate-limits
Nov 16, 2026
Merged

szokeasaurusrex merged 3 commits into
getsentry:masterfrom
itsrayeedscodespace:fix/preserve-longer-rate-limits

Conversation

@itsrayeedscodespace

@itsrayeedscodespace itsrayeedscodespace commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

A later, shorter quota currently replaces a longer active rate limit. For example, 3600:error:organization, 0:error:project clears the one-hour error limit and allows sending again immediately. The same happens across separate responses, and when a shorter Retry-After or a headerless 429 response replaces the global limit. The rate limiting spec requires SDKs to keep the longer limit.

The deadlines are now stored in a private RateLimitTimeout type, defined in its own submodule (sentry/src/transports/ratelimit/timeout.rs) so RateLimiter cannot reach the inner Option<SystemTime>. Its only mutator, update, keeps the later of the stored deadline and the new one, so no update path can shorten a limit. Header parsing, category handling, and response-header precedence are unchanged.

The first commit, by @itsrayeedscodespace, fixed this by calling .max(...) at each assignment. The second commit moves the max into RateLimitTimeout, so future update paths do not need to remember it, and drops the per-path unit tests from the first commit because the type enforces the behavior.

Issues

via @szokeasaurusrex.

--

View Session [Sentry]

Merge incoming quota expirations with stored deadlines instead of replacing them, including category groups, Retry-After, and headerless 429 fallback delays. Cover all categories and shorter/longer updates without changing header parsing.

Co-Authored-By: GPT-6 <gpt-6@noreply.example.com>
@itsrayeedscodespace
itsrayeedscodespace requested a review from a team as a code owner November 14, 2026 22:09
Calling `.max(...)` at each assignment fixes the bug, but every future update path would also have to remember it. The deadlines are now stored in a private `RateLimitTimeout` type, defined in its own submodule so `RateLimiter` cannot reach the inner `Option<SystemTime>`. Its only mutator, `update`, keeps the later of the stored and the new deadline, so no update path can shorten a limit.

Because the type enforces this, the unit tests added for each update path are removed. The changelog entry now links the PR instead of the issue.

Fixes getsentry#1409

Co-Authored-By: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com>
Comment thread sentry/src/transports/ratelimit/mod.rs Resolved
@szokeasaurusrex

Copy link
Copy Markdown
Member

Hey @itsrayeedscodespace! Thank you for the PR.

I have taken it over from here; I decided to refactor the rate limiter and introduced a new type that encapsulates the updating logic. Hopefully, this ensures that the same bug cannot be reintroduced in the future by someone forgetting to write .max when updating a ratelimit.

I also removed the tests as they are no longer needed with the type system ensuring the updating logic.

@szokeasaurusrex szokeasaurusrex self-assigned this Nov 16, 2026
Address review feedback: move `ratelimit.rs` into `ratelimit/mod.rs` next to its `timeout` submodule, and make the `RateLimitTimeout` methods `pub`, since the struct itself is already `pub(super)`.

References getsentry#1409

Co-Authored-By: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com>
@codecov

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.29630% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 74.48%. Comparing base (a57b91c) to head (bda4801).
⚠️ Report is 215 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1410      +/-   ##
==========================================
+ Coverage   73.81%   74.48%   +0.66%     
==========================================
  Files          64       84      +20     
  Lines        7538    10153    +2615     
==========================================
+ Hits         5564     7562    +1998     
- Misses       1974     2591     +617     

@giortzisg giortzisg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@szokeasaurusrex
szokeasaurusrex enabled auto-merge (squash) November 16, 2026 13:17
@szokeasaurusrex
szokeasaurusrex merged commit ea90839 into getsentry:master Nov 16, 2026
27 checks passed
sentry-junior Bot added a commit to getsentry/sentry-dotnet that referenced this pull request Nov 16, 2026
Follows the approach of #5947 and getsentry/sentry-rust#1410.

Co-Authored-By: Ricardo Oliveira <ricardo.oliveira@sentry.io>
jamescrosswell pushed a commit to getsentry/sentry-dotnet that referenced this pull request Nov 16, 2026
… one (#5946)

* fix: keep the longer rate limit when a later response sends a shorter one

Closes #5945

Co-Authored-By: Ricardo Oliveira <ricardo.oliveira@sentry.io>

* test: cover a longer rate limit replacing a shorter one

* ref: enforce the longer rate limit in a RateLimitResets type

Follows the approach of #5947 and getsentry/sentry-rust#1410.

Co-Authored-By: Ricardo Oliveira <ricardo.oliveira@sentry.io>

* test: cover a rate limit at its exact reset time

Co-Authored-By: Ricardo Oliveira <ricardo.oliveira@sentry.io>

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Ricardo Oliveira <ricardo.oliveira@sentry.io>
JPeer264 pushed a commit to getsentry/sentry-javascript that referenced this pull request Nov 17, 2026
## What

A shorter rate limit no longer replaces a longer one that is still
active. This applies to a category repeated in one
`X-Sentry-Rate-Limits` header, to limits from later responses, and to
the `all` limit from `Retry-After` or a 429 without headers.

## Why

The
[spec](https://develop.sentry.dev/sdk/foundations/transport/rate-limiting/#stage-2-determine-rate-limits)
requires keeping the longer limit. @itsrayeedscodespace reported the
same bug in the Rust SDK in getsentry/sentry-rust#1409, and
getsentry/sentry-rust#1410 fixed it by putting the rule in one type
instead of calling `max` at each assignment.

Here, the header parsing is in a module-private `forEachRateLimit`
function. It has no reference to the stored limits and reports each
category and deadline through a callback. That callback in
`updateRateLimits` is the only code that writes a limit, and it keeps
the later deadline, so a new update path in the parser cannot shorten a
limit. The transport stores the limits as `Readonly<RateLimits>`, so it
can only replace them with the result of `updateRateLimits`.

`RateLimits` (`Record<string, number>`) and `updateRateLimits` are
exported from `@sentry/core`, and `replay-internal` passes a plain `{}`.
A branded or readonly public type would break that API, so `RateLimits`
stays a mutable record. TypeScript does not stop code inside
`updateRateLimits`, or a caller outside the transport that holds a
`RateLimits` object, from writing to it directly.

Closes #26409

<!-- junior-request-attribution:start -->
via @szokeasaurusrex.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A9964492fd8caae68d7e3609da276b3f9 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A9964492fd8caae68d7e3609da276b3f9)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A9964492fd8caae68d7e3609da276b3f9/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com>
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 limiter replaces longer quotas with shorter limits from later headers

3 participants