You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add provider option to wait until primary rate limit reset before retrying requests (opt-in) #3565
Add an optional provider configuration to allow the Terraform GitHub provider to block (wait) until the GitHub primary rate limit reset time when a token-based request hits the primary rate limit. This would be disabled by default and opt-in to avoid surprising blocking behavior.
Motivation
When using personal access tokens or other token-based auth the provider can hit GitHub's primary rate limit (hourly 5000 limit). In many use-cases (ad-hoc runs, interactive retries, small infra changes) it is preferable for the provider to wait until the reset time and then retry the request instead of immediately failing. However, this behavior should be opt-in to avoid blocking CI/CD pipelines unexpectedly.
Current Behaviour
The provider fails with the following error when primary rate limit is hit (hourly 5000 token limit)
10: resource "github_team_repository""team_repo_assignment" {
Error: failed to retrieve repository example-repo: Get "https://api.github.com/repos/example-org/example-repo": (http.RoundTripper).RoundTrip failed: primary rate limit reached on request to https://api.github.com/repos/example-org/example-repo with category: core. waituntil 2026-07-21 05:21:38 +0000 UTC before sending more requests.
Expected Behaviour
If the "opt in" flag is set to true the provider should block until the rate limit reset time.
This repo is maintained by GitHub and community members on a best-effort basis. We'll get to this as soon as we can.
You can help us prioritize by adding details, reproduction steps, or a 👍 to issues that matter to you, and by joining the discussion on open issues and PRs.
@kishaningithub this isn't something that could currently be implemented as the provider architecture is being standardised on the default 20 min context which is significantly shorter than the usual 60 min primary rate limit timeout. This is something that we've already discussed but for it to even be a potential option we need to finish the context migration, at that point we'd be able to evaluate if we wanted to add support for custom context timeout using the Terraform Plugin SDK v2 or if we should wait until we've migrated over to the Terraform Plugin Framework. Also note that to make this work in a usable and understandable way we'd need to add support to all resource and data sources in one go.
My guidance on this remains that we should not be looking into this until we're on the Terraform Plugin Framework where this is a better supported pattern and where we wouldn't have to implement it twice.
Reacted by Timo Sand, Kishan B and Silver Preece (Save on Foods)
@kishaningithub this isn't something that could currently be implemented as the provider architecture is being standardised on the default 20 min context which is significantly shorter than the usual 60 min primary rate limit timeout. This is something that we've already discussed but for it to even be a potential option we need to finish the context migration, at that point we'd be able to evaluate if we wanted to add support for custom context timeout using the Terraform Plugin SDK v2 or if we should wait until we've migrated over to the Terraform Plugin Framework. Also note that to make this work in a usable and understandable way we'd need to add support to all resource and data sources in one go.
My guidance on this remains that we should not be looking into this until we're on the Terraform Plugin Framework where this is a better supported pattern and where we wouldn't have to implement it twice.
Thanks for the clear info on this, that does make sense. I was about to request the same feature as I'm managing a lot of repos through this provider. Is there a decent workaround for rate limits in the meantime? I know that Terraform has time_sleep as a resource, but that feels awkward to use, and a lot of my resources are using for_each loops so I'm not sure that pausing between them is going to help much.
@SilverPreeceSOF if you use the new (non-legacy) client you should be able to max out to the primary rate limit, outside of that you'll need to split up your workspace so you're under the primary rate limit. Please note that we're actively refactoring the resources to use fewer API calls in each minor version.
We hit this on a repository that manages most of a GitHub org from a single root module — ~1,700 GitHub resources, where one terraform plan costs north of 4,000 of the 5,000 core requests an hour a PAT gets. So squarely the audience for this request.
Before writing anything I checked whether the obvious implementation actually works, and I don't believe it does. Recording that here so the next person doesn't start down the same path.
go-github-ratelimit/v2, which the non-legacy client already uses, ships github_primary_ratelimit.WithSleepUntilReset() — which looks exactly like the option being asked for. It isn't. Standalone reproduction against v2.0.2, stub API returning an exhausted primary limit that resets 3s out:
C (started during A's sleep) after 0s ERROR: primary rate limit reached...
B (started during A's sleep) after 0s ERROR: primary rate limit reached...
A (trips the limit) after 2.9s ERROR: primary rate limit reached...
D (after reset) after 0s status 200
Two separate problems:
The request that trips the limit sleeps until the reset and then still fails. In PrimaryRateLimiter.RoundTrip, TriggerLimitReached(...) is immediately followed by return nil, callbackContext.AsError() — the callback is a notification hook, not a retry point.
Requests arriving while it sleeps are rejected instantly, since GetResetTime stays non-nil for the category until the timer fires.
At Terraform's default parallelism = 10 that means all ten in-flight reads still error — the run fails an hour later than it does today rather than succeeding. Delivering the requested behaviour needs a real retry of the request after the reset, not a sleep in the detection callback.
That runs into a second ceiling underneath the 20 minute SDKv2 context @stevehipwell mentioned: internal/ghclient/const.go sets clientTimeout = 5 * time.Minute, applied in rest.go via github.WithTimeout(...). Any wait inside a RoundTrip is cut off at five minutes regardless of the operation context.
None of this argues against the Status: Blocked call — if anything it's another reason for it.
@SilverPreeceSOF on workarounds: we moved the waiting out of the provider and into CI. Before terraform starts, a step polls GET /rate_limit (that endpoint is itself exempt from the limit) and sleeps until the reset unless close to a full window is available. That turns "fails nine minutes in, retry by hand" into "starts later, finishes", with no provider support needed. It does nothing if a single run genuinely needs more than 5,000 requests — for that, splitting the workspace as suggested above is the only real fix.
@benlangfeld I just reviewed the implementation and it looks like this behaviour is being caused by the retry transport handling 429 responses instead of passing them back to the rate limiter. I'll add a PR to fix this so we get the expected behaviour of erroring fast on primary rate limit failures. I'll also fix the throttler ordering so it doesn't hold bypass the rate limit check.
FYI a GitHub app should give you a higher rate limit than a PAT.
This is labelled Status: Blocked, and the thing blocking it now has a proposed fix, so here is where it stands and what the provider would need to do.
Why the library cannot do this today
github_primary_ratelimit offers WithSleepUntilReset, which does not do what the name suggests once more than one request is in flight. It is implemented as a LimitDetectedCallback, so it sleeps on the one request that discovers the limit and then fails that request anyway, while every request arriving afterwards takes the "limit is active" path and fails immediately without sleeping at all. One goroutine naps; the rest error. It also occupies the callback slot, so the provider could not both wait and keep primaryRateLimitCallback for its tflog warning.
Wiring it up at internal/ghclient/transport.go:76 would therefore not give wait_for_primary_rate_limit = true the behaviour this issue asks for.
Proposed upstream fix
gofri/go-github-ratelimit#52 adds WithRetryAfterReset(), which waits on both paths — the request that detects the limit and the requests prevented while it is in force — and then sends them, so the caller sees its requests complete. It leaves LimitDetectedCallback free, so the provider keeps its warning. It tracks gofri/go-github-ratelimit#46.
What the provider would need to do
Bump github.com/gofri/go-github-ratelimit/v2 past the release carrying it.
Add the provider schema attribute (wait_for_primary_rate_limit, defaulting to false, as proposed above) and thread it through ClientOptions.
github_ratelimit.New routes options to the right limiter by type, so it goes in the same variadic list as the callbacks.
Three things worth knowing before wiring it up
Order matters: this must not ship before gofri/go-github-ratelimit#51. A secondary (burst) rate limit carrying x-ratelimit-remaining: 0 is currently misfiled as a primary one — see #3660 for the evidence and the mechanism. Today that fails the run fast. With waiting turned on and the misclassification still present, the same burst throttle would instead put the provider to sleep until the primary window resets, up to an hour, for a limit that asked for a few seconds. Turning waiting on first would make the bug considerably worse.
The five-minute client timeout caps the wait, and would need raising with it.internal/ghclient/rest.go:55 sets github.WithTimeout(clientTimeout), and clientTimeout = 5 * time.Minute at internal/ghclient/const.go:41. http.Client.Timeout bounds the entire round trip including whatever the outermost transport does, so a wait for a core reset up to an hour away would end in a context deadline rather than a completed request. wait_for_primary_rate_limit = true therefore needs that timeout raised or made configurable alongside it, or it will not do what it says. It is already sufficient for secondary/burst limits, which ask to be waited out in seconds.
The provider's concurrency semaphore is inside the limiter, which is the right way round.newTransport wraps throttler before ratelimit.New, so a request waiting for a reset has not yet acquired its permit. Waiting requests will not starve the semaphore. This is worth keeping in mind if that ordering is ever changed.
I am not a maintainer here — happy for any of this to be taken, adapted or ignored.
@benlangfeld we're not adding support to wait on the primary rate limit, not waiting is by design. We're currently fixing the defect that causes the rate limit responses to be incorrectly handled. Once this has been completed my intention is to close this issue as will-not-fix.
6. @SilverPreeceSOF if you use the new (non-legacy) client you should be able to max out to the primary rate limit, outside of that you'll need to split up your workspace so you're under the primary rate limit. Please note that we're actively refactoring the resources to use fewer API calls in each minor version.
I'm only seeing this response very late, but I actually activated the new client in my configuration to test it out, and I hit the rate limit inside a few seconds! It might be configured to prevent me hitting the hourly rate but it hit the per-minute rate immediately. Might be a bug, but this was not a viable option for me. Edit - Seems like you might have been working on related issues, so thanks for that!
I've got the request rate slowed all the way down right now and I'm still hitting rate limits because the integration is using REST for data lookups (I assume the internal structure of the Terraform provider prevents it from using GraphQL to aggregate lookups together).
I think I might have to break the repo down into modules (with each module being a related group of repositories) and target them when executing runs. Not ideal, but if I break up the repo because of the number of repositories I'm managing, then I end up with duplicate configuration in multiple Git repositories and that's much worse IMO.
PR #3739 will allow rate limits that can be retried within the available window to be retried regardless of if they are primary or secondary. In reality this will not help for a primary rate limit as the provider has a total request timeout of 10 mins (and resource timeout of 20 mins), but it makes it clearer where we'd need to build such logic in the future. As it currently stands this isn't something that we'e going to implement.
Activity
github-actions commentedon Jul 21, 2026
👋 Hi, and thank you for opening this issue!
This repo is maintained by GitHub and community members on a best-effort basis. We'll get to this as soon as we can.
You can help us prioritize by adding details, reproduction steps, or a 👍 to issues that matter to you, and by joining the discussion on open issues and PRs.
🤖 This is an automated message.
stevehipwell commentedon Jul 22, 2026
@kishaningithub this isn't something that could currently be implemented as the provider architecture is being standardised on the default 20 min context which is significantly shorter than the usual 60 min primary rate limit timeout. This is something that we've already discussed but for it to even be a potential option we need to finish the context migration, at that point we'd be able to evaluate if we wanted to add support for custom context timeout using the Terraform Plugin SDK v2 or if we should wait until we've migrated over to the Terraform Plugin Framework. Also note that to make this work in a usable and understandable way we'd need to add support to all resource and data sources in one go.
My guidance on this remains that we should not be looking into this until we're on the Terraform Plugin Framework where this is a better supported pattern and where we wouldn't have to implement it twice.
SilverPreeceSOF commentedon Aug 10, 2026
Thanks for the clear info on this, that does make sense. I was about to request the same feature as I'm managing a lot of repos through this provider. Is there a decent workaround for rate limits in the meantime? I know that Terraform has
time_sleepas a resource, but that feels awkward to use, and a lot of my resources are usingfor_eachloops so I'm not sure that pausing between them is going to help much.stevehipwell commentedon Aug 11, 2026
@SilverPreeceSOF if you use the new (non-legacy) client you should be able to max out to the primary rate limit, outside of that you'll need to split up your workspace so you're under the primary rate limit. Please note that we're actively refactoring the resources to use fewer API calls in each minor version.
benlangfeld commentedon Sep 22, 2026
We hit this on a repository that manages most of a GitHub org from a single root module — ~1,700 GitHub resources, where one
terraform plancosts north of 4,000 of the 5,000 core requests an hour a PAT gets. So squarely the audience for this request.Before writing anything I checked whether the obvious implementation actually works, and I don't believe it does. Recording that here so the next person doesn't start down the same path.
go-github-ratelimit/v2, which the non-legacy client already uses, shipsgithub_primary_ratelimit.WithSleepUntilReset()— which looks exactly like the option being asked for. It isn't. Standalone reproduction against v2.0.2, stub API returning an exhausted primary limit that resets 3s out:Two separate problems:
PrimaryRateLimiter.RoundTrip,TriggerLimitReached(...)is immediately followed byreturn nil, callbackContext.AsError()— the callback is a notification hook, not a retry point.GetResetTimestays non-nil for the category until the timer fires.At Terraform's default
parallelism = 10that means all ten in-flight reads still error — the run fails an hour later than it does today rather than succeeding. Delivering the requested behaviour needs a real retry of the request after the reset, not a sleep in the detection callback.That runs into a second ceiling underneath the 20 minute SDKv2 context @stevehipwell mentioned:
internal/ghclient/const.gosetsclientTimeout = 5 * time.Minute, applied inrest.goviagithub.WithTimeout(...). Any wait inside a RoundTrip is cut off at five minutes regardless of the operation context.None of this argues against the
Status: Blockedcall — if anything it's another reason for it.@SilverPreeceSOF on workarounds: we moved the waiting out of the provider and into CI. Before terraform starts, a step polls
GET /rate_limit(that endpoint is itself exempt from the limit) and sleeps until the reset unless close to a full window is available. That turns "fails nine minutes in, retry by hand" into "starts later, finishes", with no provider support needed. It does nothing if a single run genuinely needs more than 5,000 requests — for that, splitting the workspace as suggested above is the only real fix.stevehipwell commentedon Sep 23, 2026
@benlangfeld I just reviewed the implementation and it looks like this behaviour is being caused by the retry transport handling 429 responses instead of passing them back to the rate limiter. I'll add a PR to fix this so we get the expected behaviour of erroring fast on primary rate limit failures. I'll also fix the throttler ordering so it doesn't hold bypass the rate limit check.
FYI a GitHub app should give you a higher rate limit than a PAT.
benlangfeld commentedon Sep 23, 2026
Yes, but there are resource types that only work with a PAT.
deiga commentedon Sep 23, 2026
For those it can be useful to use two separate github provider blocks
stevehipwell commentedon Sep 25, 2026
PR #3655 should resolve the issues stopping a primary rate limit failure failing fast.
benlangfeld commentedon Sep 30, 2026
This is labelled
Status: Blocked, and the thing blocking it now has a proposed fix, so here is where it stands and what the provider would need to do.Why the library cannot do this today
github_primary_ratelimitoffersWithSleepUntilReset, which does not do what the name suggests once more than one request is in flight. It is implemented as aLimitDetectedCallback, so it sleeps on the one request that discovers the limit and then fails that request anyway, while every request arriving afterwards takes the "limit is active" path and fails immediately without sleeping at all. One goroutine naps; the rest error. It also occupies the callback slot, so the provider could not both wait and keepprimaryRateLimitCallbackfor its tflog warning.Wiring it up at
internal/ghclient/transport.go:76would therefore not givewait_for_primary_rate_limit = truethe behaviour this issue asks for.Proposed upstream fix
gofri/go-github-ratelimit#52 adds
WithRetryAfterReset(), which waits on both paths — the request that detects the limit and the requests prevented while it is in force — and then sends them, so the caller sees its requests complete. It leavesLimitDetectedCallbackfree, so the provider keeps its warning. It tracks gofri/go-github-ratelimit#46.What the provider would need to do
github.com/gofri/go-github-ratelimit/v2past the release carrying it.wait_for_primary_rate_limit, defaulting tofalse, as proposed above) and thread it throughClientOptions.newTransport, append the option conditionally:github_ratelimit.Newroutes options to the right limiter by type, so it goes in the same variadic list as the callbacks.Three things worth knowing before wiring it up
Order matters: this must not ship before gofri/go-github-ratelimit#51. A secondary (burst) rate limit carrying
x-ratelimit-remaining: 0is currently misfiled as a primary one — see #3660 for the evidence and the mechanism. Today that fails the run fast. With waiting turned on and the misclassification still present, the same burst throttle would instead put the provider to sleep until the primary window resets, up to an hour, for a limit that asked for a few seconds. Turning waiting on first would make the bug considerably worse.The five-minute client timeout caps the wait, and would need raising with it.
internal/ghclient/rest.go:55setsgithub.WithTimeout(clientTimeout), andclientTimeout = 5 * time.Minuteatinternal/ghclient/const.go:41.http.Client.Timeoutbounds the entire round trip including whatever the outermost transport does, so a wait for acorereset up to an hour away would end in a context deadline rather than a completed request.wait_for_primary_rate_limit = truetherefore needs that timeout raised or made configurable alongside it, or it will not do what it says. It is already sufficient for secondary/burst limits, which ask to be waited out in seconds.The provider's concurrency semaphore is inside the limiter, which is the right way round.
newTransportwrapsthrottlerbeforeratelimit.New, so a request waiting for a reset has not yet acquired its permit. Waiting requests will not starve the semaphore. This is worth keeping in mind if that ordering is ever changed.I am not a maintainer here — happy for any of this to be taken, adapted or ignored.
stevehipwell commentedon Oct 1, 2026
@benlangfeld we're not adding support to wait on the primary rate limit, not waiting is by design. We're currently fixing the defect that causes the rate limit responses to be incorrectly handled. Once this has been completed my intention is to close this issue as will-not-fix.
SilverPreeceSOF commentedon Oct 19, 2026
I'm only seeing this response very late, but I actually activated the new client in my configuration to test it out, and I hit the rate limit inside a few seconds! It might be configured to prevent me hitting the hourly rate but it hit the per-minute rate immediately. Might be a bug, but this was not a viable option for me. Edit - Seems like you might have been working on related issues, so thanks for that!
I've got the request rate slowed all the way down right now and I'm still hitting rate limits because the integration is using REST for data lookups (I assume the internal structure of the Terraform provider prevents it from using GraphQL to aggregate lookups together).
I think I might have to break the repo down into modules (with each module being a related group of repositories) and target them when executing runs. Not ideal, but if I break up the repo because of the number of repositories I'm managing, then I end up with duplicate configuration in multiple Git repositories and that's much worse IMO.
stevehipwell commentedon Oct 21, 2026
@SilverPreeceSOF the currently released version has issues which should have been resolved and will be included in the next release.
stevehipwell commentedon Nov 23, 2026
PR #3739 will allow rate limits that can be retried within the available window to be retried regardless of if they are primary or secondary. In reality this will not help for a primary rate limit as the provider has a total request timeout of 10 mins (and resource timeout of 20 mins), but it makes it clearer where we'd need to build such logic in the future. As it currently stands this isn't something that we'e going to implement.