Skip to content

fix(github): a gh call at a quota window's reset waits out the reset second instead of failing as unknown or broken state #1364

Closed

Description

@dan-petty

Problem

Lines are cited at 910b823 (main and release/v0.2.29). A bare :N is a line of src/devops_cli/github/rate_limiter.py. The Service image runs the same code: its tracebacks raise at :886, called from calculate_delay at :938.

What fails. Between 2026-10-05 03:32Z and 2026-10-07 23:32Z, the roadmap Service logged 18 errors reading GitHubRateLimitError: Rate limit state for subcommand 'core' is in an unknown or broken state and could not be refreshed from GitHub. The count is from Loki over the 9 days up to 2026-10-08 01:31Z, the time of the check: sum(count_over_time({namespace="devops", container="service"} |= "unknown or broken" [9d])). The errors came from six pods, one per rollout, and from two managed repositories: this one and one other.

  • 8 Service rounds failed before any job ran (Service job execution failed, src/devops_cli/server/service.py:173-185). Examples are the change read (roadmap/run.py:687) and the config read. Every job due in that round waited for the next 300 s poll.
  • 2 single jobs failed.
  • 8 metrics reads were dropped with a WARNING (github/metrics.py:275, :344, :371, :403): Actions runs, milestones, open issues and traffic views.
  • No other limiter error appears in the same window (about 360k lines scanned): no Failed to refresh rate limits, no empty or malformed rate_limit answer, no time until reset must be non-negative, no reset_epoch is unknown, no remaining requests is unknown. So every call to gh api rate_limit succeeded and returned a usable answer.

Every failure falls in the second or two after a core window's reset. The pause between requests is the time to reset divided by the remaining requests (calculate_request_delay, :422-444). It reads 0.00s just before a window ends and 0.72s (3,600 s / 5,000) in a fresh window:

Last 0.00s pause Failure(s) First fresh 0.72s pause
2026-10-05 13:01:20.93Z 13:01:22.70, 13:01:23.07 13:01:23.45
2026-10-06 04:15:44.73Z 04:15:45.46, 04:15:45.80 04:15:46.17
2026-10-07 23:32:06.24Z 23:32:07.72 23:32:08.16

GitHub's reset is a whole second. In each row the stored reset was still in the future at the last 0.00s pause, and GitHub's answer was already at or before the clock at the first failure. That brackets the reset:

  • 2026-10-06: the reset is :45. On the pod's clock, GitHub still reported the old window 0.80 s after it, and the new one by 1.17 s.
  • 2026-10-07: the reset is :07. The old window was still reported 0.72 s after it, and the new one by 1.16 s.
  • 2026-10-05: the reset is :21 or :22. The old window was still reported 1.07 to 2.07 s after it, and the new one by 1.45 to 2.45 s.

So, on the pod's clock, GitHub keeps reporting a window for up to about 2 s after its reset. The logs can't tell whether that is GitHub's rollover or the pod's clock running ahead.

Why it raises. The call path is acquire -> _acquire_locked -> calculate_delay -> _resolve_quota_state (:863-895).

  1. A stored quota is usable only while reset_epoch > now and it was updated in the last 300 s (QuotaState.is_valid, :150-164; DEFAULT_GH_QUOTA_MAX_AGE_SECONDS, config/defaults.py:772). Roadmap and metrics reads don't pass -i, so they carry no headers. The stored reset is therefore the one the last refresh read.
  2. Once the pod's clock passes that reset, the limiter refreshes once from gh api rate_limit (:876-884, _refresh_from_github :897-927).
  3. GitHub still reports the window that is ending. reset is "the time at which the current rate limit window resets in UTC epoch seconds" [sourced: GitHub REST docs, rate limits for the REST API], so it is whole seconds. For up to about 2 s after it (above), the answer carries a reset at or before the pod's clock.
  4. _resolve_quota_state checks that answer against the now it read before the refresh (:871, :882), finds it not valid, and raises (:886-895). A failed refresh never reaches this raise, because _refresh_from_github raises its own error first (:909-924). So the raise only fires after a successful refresh.
  5. A second check at the same boundary is latent. Suppose the refresh returns a reset that falls between that now and the end of the refresh. Then calculate_delay raises time until reset must be non-negative (:948-954). test_calculate_delay_rejects_negative_time_left (tests/test_github_rate_limiter.py:138-146) pins that behaviour by patching the private _resolve_quota_state. It has not fired yet.

The refresh doesn't say what it covered. _extract_rate_limit_endpoint_response (:1496-1511) returns nothing. _update_single_resource_quota (:1483-1493) silently skips a resource that is missing or has no remaining. The disk sync keeps in-memory entries that the disk no longer holds (:788-792). So a stored entry the answer didn't cover survives the refresh unchanged, and _resolve_quota_state can't tell "the answer lacked core" from "the answer repeated an old reset".

The rest of the module already treats a passed reset as a reset. _acquire_locked restarts its schedule when reset_epoch <= now (:1017-1020), and _calculate_primary_delay returns no wait (:671-673). Only _resolve_quota_state and calculate_delay treat a passed reset as broken. PyGithub, locked at 2.10.0 (pyproject.toml:16), waits for the reset "plus 1s as it is not clear when in that second the reset occurs" [sourced: PyGithub 2.10.0 github/GithubRetry.py:173-174].

Why it recurs on a beat. The Service polls every 300 s (DEFAULT_SERVICE_POLL_INTERVAL_SECONDS, config/defaults.py:963). When the token is idle between polls, a window opens with one poll's first request. It ends 3,600 s later, at the start of the twelfth poll after that one, so that poll's first calls land in the reset second. On 2026-10-06 one pod failed at 04:15:45, 06:20:45, 07:20:47, 10:30:45 and 11:35:45.

Threads: no race, but each thread fails separately.

  • One limiter serves the whole process (get_github_rate_limiter, :1351). Each managed repository's RepoWorker thread uses it (server/service.py:63-90).
  • calculate_delay holds self._lock, an RLock, for the whole refresh (:935-940). The refresh runs gh through run_subprocess, which never calls back into the limiter. So _is_refreshing (:756, :876-881) is never true when it is read; it is dead code.
  • Each thread that reaches the boundary runs its own refresh and fails in turn. On 2026-10-06, the two repositories' workers failed on Actions-runs reads at 11:35:45.62 and 11:35:45.98.

The saved ledger doesn't help. _load_disk_quota drops entries whose reset has passed (:280-307). A restart at the boundary therefore takes the same path.

The log lacks the numbers. The error's details (remaining, reset) never reach the log line. No record shows the reset or how far the clock was past it.

Cost. A failed job is not stamped (roadmap/run.py:780), so it runs again on the next poll. A failed change read loses the whole round. The 2026-10-07 failure hit close 11 minutes before the v0.2.28 release pull request merged.

Acceptance criteria

  • A passed reset means the window has ended. It is not unknown state.
    • When the stored reset is at or before now, _resolve_quota_state refreshes once, as it does today.
    • GitHub's answer may still have a reset at or before now, by no more than the clock bound below. In that case the call waits until that reset plus 1 s, then runs. No GitHubRateLimitError is raised.
    • The wait is acquire's existing sleep (:998-1004), which runs after self._lock is released (:989-996). Other repositories' worker threads are not held behind it.
  • A clock far from GitHub's still fails closed.
    • Suppose GitHub's fresh answer has a reset more than the bound (proposed 5 s) before the local clock. Then _resolve_quota_state raises GitHubRateLimitError, and the command never runs.
    • The message says the clocks disagree. It names the resource, the reported reset and the local clock in UTC, and the gap in seconds.
    • The gap is measured only on GitHub's fresh answer. A stored entry whose reset passed long ago, as after an idle hour, refreshes and paces as today.
  • Both numbers are named constants: the 1 s margin and the clock bound. They go in config/constants.py, or beside github/rate_limiter.py if docs(agents): a matching table lives beside its one owner, and moves to config/ only when a second package imports it #1016's placement invariant has landed first.
    • The margin's comment cites GitHub's whole-second reset and PyGithub's margin.
    • The bound's comment cites the gaps measured above.
  • A passed reset means the same thing everywhere in the module. _resolve_quota_state and calculate_delay follow the rule that _acquire_locked (:1017-1020) and _calculate_primary_delay (:671-673) already apply. The time until reset must be non-negative raise (:948-954) is gone. test_calculate_delay_rejects_negative_time_left is deleted; test (1) covers the behaviour through run_gh.
  • A request sent after the reset is not counted against the old window. _acquire_locked already records use only on a valid entry (:1033-1034), and that stays. The next call refreshes again and paces from the new window.
  • Truly unknown state still fails closed. _resolve_quota_state raises when:
    • the refresh fails (_refresh_from_github's errors, unchanged);
    • GitHub's answer has no usable entry for the resource. This holds even when a stale stored entry with a passed reset exists. The refresh reports the resources it updated, from the parsed resources keys _extract_rate_limit_endpoint_response already holds (:1504-1511). The message names the resource and those resources.
  • The boundary shows in the log. Each wait at a passed reset logs one INFO record. It names the resource, the reported reset in UTC, how many seconds the clock is past it, and the wait.
  • _is_refreshing is deleted (:756, :876-881). The lock already serializes the refresh, and nothing calls back into it.
  • Tests go in tests/test_github_rate_limiter.py, or its mirrored path if test(cli): every test file sits in the directory mirroring the package it tests or in tests/invariants/, and the root conftest holds only the hermetic floor #1313's move has landed. They patch no devops-cli name (test(cli): tests double only process edges and mirror the source tree #1307).
    • gh is faked at the stdlib process edge, subprocess.run, as tests/test_github_rate_limiter.py:1148 already does. If test(github): github/ is tested through its public interface over the gh process and session fakes, with no internal patches or private imports #1321's gh process fake has landed, they use that instead.
    • The fake answers gh auth token, gh api rate_limit and the command, and records each argv. Refreshes are counted as the gh api rate_limit calls it saw.
    • Time is frozen and waits are recorded with refactor(cli): tests control time: time-machine fakes wall-clock reads, and retry and rate-limit waits return at once and are recorded #1320's clock fixture if it has landed. Otherwise the tests patch time.time and time.sleep.
    • Stored quota is seeded through the public update_quota.
    • The tests make no network call, and each test's call phase stays under 1 s.
    • Cases:
      • (1) Boundary. The stored core reset is 0.5 s behind the clock, and gh api rate_limit answers with the same reset. run_gh(["api", "repos/o/r/issues"]) runs the command once, after exactly one refresh and a recorded wait of 0.5 s, and raises nothing.
      • (2) New window after idle. The stored core reset is an hour behind the clock. The refresh answers 5,000 remaining with the reset 3,600 s ahead. The recorded wait is 0.72 s, and there is no clock error.
      • (3) Clock ahead, within the bound. The reset is 2 s behind the clock, and every refresh repeats it. The command runs with no wait, and the next run_gh refreshes again before it runs.
      • (4) Clock ahead, past the bound. GitHub's answer has the reset 60 s behind the clock. The error says the clocks disagree and names the reported reset, the local clock and the 60 s gap. The command never runs.
      • (5) Refresh fails. gh api rate_limit exits 1 with HTTP 502. The GitHubRateLimitError names the gh failure, and the command never runs.
      • (6) Resource missing over a stale entry. A stored core entry has its reset 0.5 s behind the clock. The answer lists graphql and search but no core. The error names core and the listed resources, and the command never runs.
      • (7) Two worker threads. Two threads call run_gh at the boundary from case (1). Both commands run, and neither raises.
  • uv run devops ci passes within its 5-minute budget.
  • changelog.d/<n>.md records the change. CHANGELOG.md and docs/ROADMAP.md are untouched (uv run devops pr check-readiness passes).
  • Pending a person, after merge and once the Service runs an image with this fix:
    • 7 days later, sum(count_over_time({namespace="devops", container="service"} |= "unknown or broken" [7d])) returns no data.
    • In the same 7 days, the INFO record from the log criterion appears at least once, which shows the boundary path ran. Every gap it reports is below the clock bound.

Key questions

Strategic alignment

Update 2026-10-08: delivered scope

Activity

  1. added
    type/bug
    scope/github
    priority/p1-high
    on Oct 8, 2026
  2. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. 18 production errors over 9 days caused 8 failed service rounds and 2 failed jobs, but the failure window is 1–2 s per reset and the service self-recovers on the next poll.
    • Value: Medium. Eliminates intermittent job failures and dropped metrics reads at every rate-limit reset, improving service reliability for all managed repositories.
    • Effort: Medium. Requires adjusting the quota-validity check and refresh logic in rate_limiter.py to tolerate a small reset-boundary tolerance, plus regression tests covering the race.
  3. added
    source/agent
    and removed
    priority/p1-high
    on Oct 8, 2026
  4. added this to the v0.2.29 milestone on Oct 8, 2026
  5. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Placed in v0.2.29 by the owner on 2026-10-08. The owner's instruction was to pull every issue that keeps the roadmap service from working into the current milestone and complete them. This is a person's placement and should stay in the release (#1349). Implementation started the same day, so the board Status is In Progress.

  6. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    What changed

    #1413, merged into release/v0.2.29 as 2f1f4f0.

    Closes #1364

    A gh call in the second or two after a quota window's reset no longer fails with "Rate limit state for subcommand 'core' is in an unknown or broken state". GitHub's reset is in whole seconds, and its rate_limit answer keeps reporting a window for up to about 2 s after it ends. So the Service lost a round, a job or a metrics read about once an hour.

    • _resolve_quota_state refreshes once when the stored quota is not valid. It fails closed in three cases:
      • the refresh fails;
      • GitHub's answer has no remaining and reset for the resource (it names the resources it does report);
      • the local clock, read just before the refresh so the refresh's own latency never counts, is more than 5 s past the reset GitHub reported (it names both times in UTC and the gap).
    • calculate_delay, at a reset that has passed, returns the time until 1 s past it (the margin PyGithub 2.10.0 uses) and logs one INFO record with the resource, the reset, the seconds past it and the time the call waits until at least (the reset plus the margin; a write may wait longer for its own pacing).
    • acquire sleeps outside the lock, so other repositories' workers are not held behind it.
    • _refresh_from_github returns the resets GitHub reported, so the gap is measured on the fresh answer only.
    • Removed: the "time until reset must be non-negative" raise and the dead _is_refreshing flag.
    • Constants: the two numbers are CONST_GH_RATE_LIMIT_RESET_MARGIN_SECONDS…

    6 file(s) changed.

    How it was verified

    Check runs at f2f4928:

    • Prune PR DevContainer Images: pass
    • CodeQL: pass
    • Static Analysis: pass
    • Analyze (actions): pass
    • Analyze (python): pass
    • Tests & Coverage: pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-1364-quota-reset-boundary.md:

    • A passed reset means the window has ended. At a stored reset at or before the clock, the call refreshes once. When GitHub's answer still has a reset at or before the clock, within the bound, the call waits until a second past that reset and runs; nothing raises. The wait is acquire's sleep, outside self._lock. Tests: test_a_call_just_past_a_reset_waits_out_the_reset_second (one refresh, one recorded 0.5 s wait, the command once) and test_a_clock_within_the_bound_past_the_reset_runs_and_refreshes_again (no wait, a refresh before each call).
    • A clock far from GitHub's still fails closed. A fresh reset more than 5 s behind the clock raises GitHubRateLimitError saying the clocks disagree. The error names the resource, the reported reset and the local clock in UTC, and the gap. The command never runs. The gap is measured on GitHub's fresh answer only: a stored window that ended an hour ago refreshes into the new one and paces from it. Tests: test_a_clock_past_the_bound_fails_closed_naming_both_clocks and test_a_call_after_an_idle_hour_paces_from_the_new_window (one recorded 0.72 s wait).
    • Both numbers are named constants in src/devops_cli/config/constants.py: CONST_GH_RATE_LIMIT_RESET_MARGIN_SECONDS cites GitHub's whole-second reset and PyGithub 2.10.0's margin (github/GithubRetry.py:173-174), and CONST_GH_RATE_LIMIT_CLOCK_SKEW_BOUND_SECONDS cites the measured 0.72–2.07 s gaps.
    • A passed reset means the same thing everywhere in the module. The "time until reset must be non-negative" raise and test_calculate_delay_rejects_negative_time_left are deleted. The boundary tests cover the behaviour through run_gh.
    • A request sent after the reset is not counted against the old window. _acquire_locked still records use only on a valid entry. The boundary test reads the stored core quota back unchanged (remaining 7, used 4993).
    • Truly unknown state still fails closed. A failed refresh raises with gh's failure (test_a_failed_refresh_fails_closed_naming_the_gh_failure). An answer without the resource raises naming it and the resources the answer does report, even over a stale stored entry (test_an_answer_without_the_resource_fails_closed_over_a_stale_entry). In both cases the command never runs.
    • The boundary shows in the log. Each call at a passed reset logs one INFO record: [RateLimit] '<resource>' quota window reset at <UTC>, <s>s ago; waiting until at least <UTC>, the reset plus the margin. A write may wait longer, for its own pacing.
    • _is_refreshing is deleted.
    • Two worker threads at the boundary each refresh, wait 0.5 s and run, and neither raises (test_two_workers_at_the_reset_both_wait_it_out_and_run).
    • Tests are in tests/test_github_rate_limiter.py. They fake gh at subprocess.run only, rely on the autouse pin_github_session for the token, freeze time.time, record time.sleep, seed the stored quota through update_quota, make no network call and patch no devops-cli name. They assert the whole list of recorded waits.
    • changelog.d/1364.md records the fix. CHANGELOG.md and docs/ROADMAP.md are untouched.
    • uv run devops ci passes.
    • Pending a person: 7 days after the Service runs an image with this fix: sum(count_over_time({namespace="devops", container="service"} |= "unknown or broken" [7d])) returns no data.
    • Pending a person: over the same 7 days, {namespace="devops", container="service"} |= "quota window reset at" shows at least one line, and every <s>s ago it reports is below 5 s.
  7. added a commit that references this issue on Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    scope/githubsource/agenttype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions