Skip to content

fix: do not treat a secondary rate limit as a primary one - #51

Open
benlangfeld wants to merge 3 commits into
gofri:mainfrom
benlangfeld:dont-arm-on-secondary
Open

benlangfeld wants to merge 3 commits into
gofri:mainfrom
benlangfeld:dont-arm-on-secondary

Conversation

@benlangfeld

@benlangfeld benlangfeld commented Sep 30, 2026 •

Copy link
Copy Markdown

Fixes #50.

GitHub sends x-ratelimit-remaining: 0 on some secondary rate limits, so the status code and that header together do not tell the two apart. Both limiters decided on the header alone, from opposite directions:

  • github_secondary_ratelimit.isSecondaryRateLimit returned early with // a primary rate limit whenever remaining == 0, before looking at the body.
  • github_primary_ratelimit.ParsedResponse.limitReached returned true for any 403/429 with remaining == 0.

Since the secondary limiter is stacked outside the primary one, the primary sees the response first, arms the category, and returns an error — so the secondary limiter never gets the chance the deferral assumed it would.

The result is that a burst throttle asking to be waited out in seconds blocks every request in the resource category until the hourly reset, while the primary quota it claims to have exhausted is untouched.

The change

The body says which limit it is, and now decides. SecondaryRateLimitBody.IsSecondaryRateLimit already encodes that test; the primary limiter now consults it before arming, and the secondary detector no longer defers on the header.

Reading the body to decide means the primary limiter has to put it back, which it does on every path, including a failed read — the response is always left in the state it was found in.

One guard is kept rather than removed: a response reporting no requests remaining is still kept away from the x-ratelimit-reset fallback when working out how long to sleep, because that header names the primary window — an hour, rather than the backoff actually being asked for. Such a response is waited out on retry-after.

Known gap

A secondary limit carrying remaining: 0 and no retry-after now has no sleep duration to work from, so it is passed through to the caller as a plain 403 rather than waited out. That is the existing behaviour for any secondary limit with no usable header, and the XXX in parseSecondaryLimitTime already proposes the fix — GitHub documents a 60-second minimum with exponential backoff. It seemed better to leave that decision where you left it than to smuggle it in here. Before this change the same response armed the primary category for an hour, so passing it through is not a regression, but it is not yet a proper handling either.

Test

TestSecondaryRateLimitWithZeroRemainingIsNotPrimary builds the response the issue describes — 403, a secondary body, retry-after: 1, x-ratelimit-remaining: 0, and a reset an hour out — and asserts the request is retried once after a short wait, that it succeeds, and that the primary limit-detected callback is never called.

Before this change the request fails immediately and the category is armed for the hour.

The existing suite passes unchanged, including TestPrimaryRateLimitIgnored, which covers the case the removed early return was there for.

Reproduction in the wild

A second, independent one, from a Terraform run against an organisation of ~200 repositories:

288 identical errors, all within the same second, all citing the same reset
primary quota consumed across the whole run: 210 of 12,500  (1.7%)

A 12,500/hour primary limit cannot be exhausted by 210 requests. The run had not come close to its primary budget; it hit a burst limit, had it filed as a primary one, and lost the rest of the window. The 288-at-once shape is the category being armed and every in-flight request being refused without being sent.

🤖 Generated with Claude Code

GitHub sends `x-ratelimit-remaining: 0` on some secondary rate limits, so the
status code and that header together do not tell the two apart. Both limiters
decided on the header alone: the secondary detector deferred such a response
to the primary path, and the primary limiter armed the whole resource category
until the hourly reset.

The effect is that a burst throttle, which asks to be waited out in seconds,
blocks every request in the category for up to an hour - and the primary quota
it claims to have exhausted is untouched.

The body says which limit it is, and now decides. A response that reports no
requests remaining is still kept away from the x-ratelimit-reset fallback,
since that names the primary window rather than the backoff being asked for.

Fixes gofri#50
benlangfeld and others added 2 commits September 30, 2026 19:22
A failed read closed the body and returned, so whoever handled the response
next - the secondary limiter, or the caller - found it closed. Put the bytes
that were read back before returning, so the response is always left in the
state it was found in.

Co-Authored-By: Claude Opus 5 <[email protected]>
@benlangfeld

Copy link
Copy Markdown
Author

@gofri Could I get a review please?

@stevehipwell stevehipwell 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.

The code change looks good but there doesn't seem to be a test for the change you made to parseXRateLimitReset?

Also would it make sense to use synctest to speed up the tests where the code is waiting?

@stevehipwell

Copy link
Copy Markdown

@gofri we've recently started using your package in the GitHub Terraform Provider, so thank you for your work. This issue is impacting us and we'd like to get a fix in before we cut our next release. Could you possibly take a look?

@benlangfeld

Copy link
Copy Markdown
Author

@gofri Is this project dead?

@lnutimura

Copy link
Copy Markdown

Hey @benlangfeld! Just to let you know, I opened #53, which also touches parseXRateLimitReset, and I think it complements #51.

While this change fixes secondary limits that come with x-ratelimit-remaining: 0 (#50), #53 fixes the ones that don't. Without retry-after, the secondary limiter sleeps until x-ratelimit-reset, the primary window's reset, which can be an hour away (#38). #53 waits one minute instead. Together, they also cover the "Known gap" here: a remaining: 0 limit without retry-after would wait a minute instead of being passed through.

They only conflict in text: #51 adds a guard to parseXRateLimitReset, and #53 removes the function, so whichever lands second can drop the function. Does that match how you'd expect them to combine?

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.

Secondary rate limit with x-ratelimit-remaining: 0 is misclassified as a primary limit and blocks the whole category

3 participants