Skip to content

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

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

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

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

Fixes #3211

Origin: getsentry/sentry-rust#1409, the same bug in the Rust SDK, found by @itsrayeedscodespace. Pattern: getsentry/sentry-rust#1410, which enforces the rule with a type.

A shorter rate limit could replace a longer one that was still active. parse_rate_limit_header let the last group per category win, so 3600:error:organization, 0:error:project left errors unlimited. The rate limiting spec requires keeping the longer limit.

What enforces the rule. The deadlines now live in Sentry::Transport::RateLimits (@api private). Its only write method, update(category, deadline), keeps the later of the stored and the new deadline. It has no []=, merge!, delete, or reader that returns its internal Hash; to_h returns a copy. The HTTP transport writes every limit through update: each group of x-sentry-rate-limits (so repeated categories within one header are covered), Retry-After, a 429 without headers, and the nil all-categories limit. is_rate_limited? and any_rate_limited? delegate to the class.

What Ruby cannot enforce. Code can still reach the internal Hash with instance_variable_get, replace Transport's @rate_limits ivar, or reopen the class. A subclass of Sentry::Transport that wrote to @rate_limits as a Hash would break; no transport in this repo does.

Public API. Transport#rate_limits was an attr_reader that returned the live Hash. It still returns a Hash with the same keys and values, but as a copy: reads ([], ==, each) behave as before, and mutating it no longer changes the transport's limits. Only specs in this repo mutated it, to seed limits; they now use RateLimits#update. No other gem in the monorepo uses it. The copy is not frozen, because a frozen copy would make such writes raise FrozenError in host apps instead of being ignored.

via @szokeasaurusrex.

--

View Session [Sentry]

A shorter rate limit could replace a longer one that was still active. When an x-sentry-rate-limits header listed the same category more than once, the last group won, so "3600:error:organization, 0:error:project" left errors unlimited.

The deadlines now live in Sentry::Transport::RateLimits. Its only write operation, update, keeps the later of the stored and the new deadline, and every update path (each group of the rate limit header, Retry-After, and a 429 without headers) writes through it, so no path can shorten a limit. Transport#rate_limits still returns a Hash, now a copy of the deadlines.

Fixes #3211

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

Copy link
Copy Markdown
Member

Junior: does this change break public API? If not, it's good to mark ready for review

@sentry-junior

sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

No break to the documented API. Transport#rate_limits keeps its name and arity, and it still returns a Hash with the same keys (String category or nil) and Time values. Reads through it ([], ==, each, values) behave as before. is_rate_limited? and any_rate_limited? keep their signatures and results.

The observable change is in mutation. The reader now returns a copy, so transport.rate_limits.merge!(...) or []= on it no longer changes the transport's limits, and it does not raise. Nothing documents that use. A GitHub code search for transport.rate_limits finds only this repo's sentry-ruby/spec/sentry_spec.rb and forks of it. getsentry/sentry-docs has no mention of rate_limits.

The second change is internal. The @rate_limits ivar is now a Sentry::Transport::RateLimits instead of a Hash, so a custom Transport subclass that wrote to that ivar directly would break. Code search finds no such subclass.

@sentry-junior
sentry-junior Bot marked this pull request as ready for review November 16, 2026 13:20
@sentry-junior
sentry-junior Bot requested review from a team and solnic as code owners November 16, 2026 13:20
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.

Shorter rate limit in the same header replaces a longer one

1 participant