Skip to content

fix(github): gh calls no longer stall silently for up to a minute behind a fixed 60-call window, and the unmaintained ratelimit package is removed #1118

Closed

Description

@dan-petty

Problem

Lines cited at dcafadf. A bare :N is a line of src/devops_cli/github/rate_limiter.py. Other paths are under src/devops_cli/ unless they start with tests/ or docs/, name a top-level file, or start with fastmcp/, anyio/, ratelimit/ or aiolimiter/, which are inside those locked packages.
At 132ecc5 these cited lines moved, and every other citation is unchanged: config/defaults.py:764 is 773, config/constants.py:2156-2177 is 2156-2178, README.md:364 is 365, docs/CLI_REFERENCE.md:6038 is 6055, ai/mcp/server.py:1678 and :2136 are 1701 and 2159, commands/pr.py:874-889 is 876-891, and in roadmap/github_store.py :91 is 109, :346-354 is 521-529 and :936 is 1458.

run_gh passes every gh process it starts through a burst ceiling built on ratelimit (github/rate_limiter.py:31 and :59-83, called at :1546, :1642 and :1750). The ratelimit==2.2.1 pin (pyproject.toml:43) has been abandoned upstream:

  • Its last PyPI release, 2.2.1, was uploaded 2018-12-17 (uv.lock:2808-2812). The tomasbasham/ratelimit repository has had commits since then (the latest on its default branch is from 2025-05-31) but no release.
  • It ships only an sdist, with no wheel (uv.lock:2812). It is one of two sdist-only packages in uv.lock; the other is actionlint-py, in the dev group (pyproject.toml:74). A uv sync with an empty cache builds it from source.
  • It ships no type information. The module therefore carries # type: ignore[import-untyped] (:31) and two untyped-decorator ignores (:62-63), and each call site wraps its result in a cast (:1544, :1640, :1748). Those three casts are the only uses of the cast imported at :27.

What the ceiling does:

  • limits(calls=60, period=60) under sleep_and_retry is a fixed window of 60 calls per 60 s. It is held in memory per process, and its first window opens when the module is imported. The 61st gh start in a window sleeps until the window ends. In an offline probe (patched runner, no gh, no network), the 61st call asked for a sleep of 59.1–59.8 s, the rest of the window that had opened at import.
  • That sleep is silent. Nothing logs it, and record_throttle (:812-817) does not count it, so devops gh rate-limit (commands/gh.py:1489-1490) never shows it. The ledger logs its own pauses (:1005-1009).
  • It does not bound bursts: when a window closes after 60 starts, the next 60 can follow at once.
  • It counts calls the ledger exempts by design: gh auth, version, help and gh api rate_limit (config/constants.py:871-879, github/rate_limiter.py:1440-1447). It also counts every retry.
  • For a short-lived command it is per invocation. Each devops run starts a fresh window, so it never limited callers that run many short commands. The state shared across processes is the ledger's quota file, which is guarded by a file lock (:213-267, :983-1012).
  • The MCP server is different: one long-lived process runs many commands, so the window carries across them:
    • 36 MCP tools and 4 resources run devops gh … or devops pr … (ai/mcp/server.py, for example gh_issue_list at ai/mcp/server.py:1678 and pr_thread_resolve at ai/mcp/server.py:2136). They go through _run_mcp_cmd (ai/mcp/server.py:64-95), which hands them to InProcessDispatcher.dispatch and from there to Typer's CliRunner, inside the server process (ai/mcp/dispatcher.py:186-251).
    • FastMCP 3.4.7 starts each incoming request in its own task (fastmcp/server/low_level.py:257-268). It runs sync tools in anyio worker threads (fastmcp/utilities/async_utils.py:9,34), at most 40 at once by anyio's default (anyio/_backends/_asyncio.py:3162, anyio 4.15.1). gh calls from parallel tool calls can therefore overlap.
    • The ceiling caps the whole server at 60 gh starts per window, and it stalls whichever tool call makes the 61st start.
  • It contradicts the module's stated contract, "No initial quotas, no hardcoded default windows, default request rate, or bursting" (:3-6). The 60/60 is a literal, and no setting changes it.

What already paces gh without the ceiling:

  • GitHubRateLimiter.acquire (:983-1012) spaces every non-exempt request by time until reset divided by remaining requests for its resource (:935-974, calculate_request_delay at :412-434). With a full 5,000-request core quota and the reset 60 minutes away, requests are 0.72 s apart.

  • That spacing holds across threads in one process. _acquire_locked (:1014-1042) books each start after the previous booking for its resource (:1023-1033) while holding self._lock, and the sleep happens outside the lock (:1004-1010). In an offline probe, two threads released together, each calling run_gh, slept 0.72 s and 1.44 s (5,000 remaining, reset 3,600 s away). In the MCP server, this is what spaces gh starts once the ceiling is gone. Secondary-limit answers get the backoff below, and record_throttle counts them.

  • It waits at least DEFAULT_GH_MUTATION_MIN_INTERVAL_SECONDS = 1.0 (config/defaults.py:764) before a mutation (:979-980). GitHub's "Best practices for using the REST API" page asks for "at least one second between each" mutative request. The wait applies only to writes that _is_mutation_command (:498-509) recognizes: a mutation verb such as create or resolve (config/constants.py:2156-2177), or an explicit -X/--method with POST, PUT, PATCH or DELETE (:484-495). An offline probe confirmed that it misses two kinds of write:

    • GraphQL mutations sent with gh api graphql. These are the thread reply, resolve and unresolve (github/pr_threads.py:146-179, sent at github/pr_threads.py:260-295); resolve_all_pr_review_threads (github/pr_threads.py:331-354) runs the resolve once per thread. They also include the view writes in github/projects.py:1335-1362 and the field writes in roadmap/github_store.py:346-354, sent at roadmap/github_store.py:936.
    • REST writes that rely on gh's documented default of POST when fields are given, such as pull request creation in commands/pr.py:874-889.

    For these writes the ceiling was the only per-process cap, at 60 per window. After this item they are paced by quota spacing alone.

  • After a rate-limit error it returns a server Retry-After unchanged, waits for the reset when the primary quota is exhausted, and otherwise waits a 60 s floor plus tenacity's wait_random_exponential (:646-716, :1178-1202, :1778-1803, from feat(cli): correct secondary-limit backoff and retry-after handling in run_gh #705). This is what the same page's "Handle rate limit errors appropriately" section prescribes.

  • GitHub's documented secondary limits ("Rate limits for the REST API"), set against a serial client:

    • No more than 100 concurrent requests. A serial client makes one at a time, and the MCP server makes at most 40.
    • 900 points a minute per REST endpoint and 2,000 for GraphQL, where a read costs 1 point and a write 5. A serial reader would need a read every 67 ms to reach 900. On the development container, gh --version alone took 0.10–0.17 s (median 0.11 s over 20 runs, gh 2.102.0, load1 about 11 on 16 CPUs). Earlier runs on the same container took 0.21–0.44 s and 0.31–0.83 s. A serial caller therefore starts at most about 600 gh processes a minute before any network round trip. Recognized writes are at least 1 s apart: at most 60 a minute, or 300 points.
    • No more than 90 s of CPU time per 60 s of real time, which GitHub says can be estimated from total response time. A serial client's response times cannot add up to more than 60 s a minute.
    • No more than 80 content-generating requests a minute. Recognized writes stay at 60 or fewer.
    • No more than 500 content-generating requests an hour. Neither the ceiling (up to 3,600 calls an hour) nor the ledger (a 1 s interval allows 3,600 an hour) bounds this, so removing the ceiling changes nothing here. A hit gets the secondary backoff.
  • 19 modules use run_gh: 18 import it, and commands/release.py loads it lazily (commands/release.py:47). None of them starts a thread pool or task group. The one asyncio.to_thread route, run_gh_async (:1944-1980), has no caller.

The ceiling binds in two cases:

  • A single command makes more than 60 gh calls in a minute while the ledger's spacing is short (plenty of quota, reset close). It stalls the command silently, though GitHub's limits would not have been reached.
  • In the MCP server, tool calls together pass 60 gh starts in a window. It stalls one tool call silently, whichever made the 61st start.

The existing dependencies cover these jobs:

  • aiolimiter 1.3.0 (pyproject.toml:44) is async-only: AsyncLimiter.acquire is a coroutine tied to an event loop (aiolimiter/leakybucket.py:129). Its only use is the hardcoded AsyncLimiter(max_rate=60, time_period=60) in get_async_limiter (:775-780), which serves only run_gh_async.
  • tenacity 9.1.4 retries after a failure and already supplies the backoff above. It has no limiter that acts before a request.
  • PyGithub 2.10.0 throttles its own requests by default: seconds_between_requests=0.25, seconds_between_writes=1.0, and retry=GithubRetry() with a 60 s secondary wait. GitHubClient builds it with those defaults (github/client.py:48). This covers only calls made through PyGithub, which never passed the ceiling, and it cannot pace gh processes.
  • GitHub's x-ratelimit-* and Retry-After headers already feed the ledger when gh prints them (:588-643), and they feed the backoff (:609-621).

Nothing new is needed to replace the ceiling. Once it is deleted, gh is paced by the ledger and the backoff, which together follow GitHub's documented guidance for every write the ledger recognizes.

Out of scope:

  • The module's hand-written pagination and parsing. That covers _run_gh_paginated and its helpers (:1499-1669), header-line and Retry-After parsing (:565-621), JSON extraction from mixed output (:388-409) and rate-limit message matching (:1164-1176). It is a separate, later family: refactor(github): the github session owns transport, ledger, error classes and link pagination #983 replaces the page loop with Link pagination and the string matching with one classifier.
  • Recognizing the writes _is_mutation_command misses (see Key questions).

Acceptance criteria

  • Pending a person: the owner confirms the two key questions marked (owner decision) before work starts. The criteria below assume the proposed answers.

  • ratelimit is gone. Its line at pyproject.toml:43 and its uv.lock entries (uv.lock:664, uv.lock:730, uv.lock:2808-2812) are removed via uv lock. rg -n "ratelimit import|import ratelimit|_burst_protected_subprocess|_BURST_LIMIT_DECORATOR" src tests prints nothing.

  • The three call sites (:1546, :1642, :1750) call run_subprocess (core/process.py:218-231, which returns CompletedProcess[str]) with the same arguments and no cast, and cast leaves the typing import at :27. The type: ignore comments at :31 and :62-63 are removed, and mypy passes with no new ignore.

  • No gh start loses pacing. Every non-exempt start still passes GitHubRateLimiter.acquire. That includes the --paginate fallback with no endpoint (:1639-1645), which today passes only the ceiling. Test: with a patched runner, that fallback calls acquire once before starting gh.

  • No minute-long stall. A unit test in tests/test_github_rate_limiter.py:

    • sets update_quota("core", remaining=5000, limit=5000, reset_epoch=time.time() + 60), replacing the autouse fixture's 3,600 s (tests/test_github_rate_limiter.py:31-40);
    • patches run_subprocess with a runner that returns success;
    • patches time.sleep with a fake that records each request and raises AssertionError on any request of 1 s or more;
    • sends 61 run_gh reads and asserts 61 runner calls.

    Before the change the test fails at once: the first call over 60 in ratelimit's window makes sleep_and_retry ask the fake for the rest of that window (59.1–59.8 s, measured). After the change it passes, and the largest recorded sleep is the ledger's own spacing, 60 s / 5,000, accumulated over 61 calls (0.67–0.72 s in the probes, under the 1 s limit). The setup matters:

    • With the fixture's 3,600 s and a clock that the fake sleep does not advance, the ledger's bookings add up to 44 s, and 60 of the 61 sleeps are 1 s or more. Use the 60 s reset above, or a fake time.time that each recorded sleep advances (largest sleep 0.72 s).
    • Patching time.monotonic has no effect before the change, because ratelimit binds it at import (ratelimit/decorators.py:11, used as the default clock at ratelimit/decorators.py:17).
    • A recording sleep that never raises makes sleep_and_retry (ratelimit/decorators.py:115) retry without end.
  • Parallel callers stay spaced. In a second test, two threads released together by a threading.Barrier each call run_gh once, with the fixture's quota (5,000 remaining, reset 3,600 s away) and a recording time.sleep. The test asserts that the larger recorded sleep exceeds the smaller by at least 0.5 s (0.72 s and 1.44 s in the probe). The test passes before and after the change. It pins the cross-thread spacing that the MCP server relies on once the ceiling is gone.

  • The existing pacing, Retry-After, secondary-floor and mutation-interval tests in tests/test_github_rate_limiter.py pass unchanged.

  • The sentences that call run_gh's pacing a token bucket are changed to describe what remains: quota-derived spacing, the mutation interval and the backoff. They are at lang/en/help.py:746, commands/gh.py:1284, AGENTS.md:79 and AGENTS.md:369. README.md:364, docs/commands/gh.md:9 and docs/CLI_REFERENCE.md:6038 repeat the help string. uv run devops docs generate rewrites them, and uv run devops docs check passes. run_gh_async's docstring (:1959) goes away with refactor(github): the github session owns transport, ledger, error classes and link pagination #983.

  • The end of the silent stall of up to a minute in gh commands under ### Fixed.

  • Gate:

    • The tests are offline and start no gh. The 61-call loop ran in 0.02–0.08 s when measured, and the new tests take well under 1 s together. No gate time is added.
    • The full suite at 132ecc5 (dcafadf plus feat(github): current release admission, cap, descoping and stall rules (devops roadmap reprioritize) #740, which leaves github/rate_limiter.py unchanged) sends 48 calls through the ceiling: 8,382 tests on 16 xdist workers, at most 15 calls in one worker. The ceiling never raised, so the gate never reached it.
    • uv run devops ci passes. uv sync installs one package fewer and builds one sdist fewer.

Key questions

Strategic alignment

Update 2026-10-08: refinement

The pin and import remain (pyproject.toml:46, github/rate_limiter.py:35, :73-101). #1125 already treats GraphQL mutations and implicit POSTs as writes (rate_limiter.py:2099), so drop the missed-writes question; only the fixed-ceiling question waits on the owner.

  • test_paginated_read_fails_closed_when_exceeding_page_cap (tests/test_github_rate_limiter.py:804) runs under 1 s alone and in the gate (54-105 s today).

Activity

  1. added
    type/bug
    scope/github
    priority/p2-medium
    on Oct 3, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Evidence, 2026-10-08: the ratelimit ceiling of 60 calls per 60 s trips inside the full devops ci run. tests/test_github_rate_limiter.py::test_paginated_read_fails_closed_when_exceeding_page_cap (#1034) spun for 60.10 s in one gate run: pytest took 5m 21s and the gate 5m 53s, against the 5-minute budget. Run as a single file, it spins for 104.66 s at 3f1935a. Any test that patches time.sleep to a no-op or a recorder, including #1364's boundary tests, busy-waits until the window ends once its xdist worker has made 60 gh calls in 60 s, and an exact sleep-list assertion would then fail. Removing the ceiling removes both the delay and the flake.

  3. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Evidence for this item's gate cost, from #1360's delivery (2026-10-08): tests/test_github_rate_limiter.py::test_paginated_read_fails_closed_when_exceeding_page_cap (added by #1034, #1406) takes about 54 s when run alone. It already patches time.sleep and sets the page cap to 2, so the time probably goes to the fixed 60-call window this item removes, waited on through a clock the test doesn't control. That one test accounts for much of devops ci's overrun of its 5-minute budget (CI gate speed is P0). Add to this item's criteria: that test's call phase is under 1 s. If this item isn't scheduled soon, the test should get a controlled clock first. Folded here instead of a new issue because the agent filing quota (#1153) is spent.

  4. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Linked: #983, #1415 and #1441. Pacing and quota accounting miss what GitHub charges; #983's session rewrites the same module, and each fix stays independent. Found by the 2026-10-08 backlog review.

  5. added a commit that references this issue on Oct 24, 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

    priority/p2-mediumscope/githubtype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions