Repository navigation
fix(gh_client): fail fast on 401/403 auth errors instead of retrying forever (#5325) - #5329
Conversation
…forever (unslothai#5325) Fixes unslothai#5325. The Studio data-recipe GitHub Crawler swallows 401 Unauthorized (and 403 Forbidden without rate-limit headers) into the generic "network error" retry path, so a job with a stale or wrong-scoped GitHub token spins indefinitely emitting "Retry." lines until the user cancels. Changes: - Add GitHubAuthError. Raised on 401, and on 403 unless the response carries a clear rate-limit signal (Retry-After header for secondary limits, or X-RateLimit-Remaining: 0 for primary limits). - Track which token source resolved at construction time: explicit argument (recipe-level field), GH_TOKEN, or GITHUB_TOKEN. Surfaced in the error message so the user knows which credential to rotate. - Insert the auth-failure check before the existing 403/429 rate-limit branch in both .graphql() and .rest() so auth failures bypass the sleep-and-retry loop and abort the recipe immediately. Genuine rate limiting still retries via the existing path. requests.RequestException handling is unchanged because GitHubAuthError does not inherit from it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 731a0b37e4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if r.headers.get("Retry-After"): | ||
| return False | ||
| if r.headers.get("X-RateLimit-Remaining") == "0": | ||
| return False | ||
| return True |
There was a problem hiding this comment.
Treat headerless secondary 403s as retryable
_is_auth_failure classifies every 403 as credential failure unless Retry-After or X-RateLimit-Remaining: 0 is present, but GitHub secondary rate limiting can return 403 without those headers (the signal is often only in the response body/message). In that case both graphql() and rest() now raise GitHubAuthError immediately (because they call this helper before the 403/429 retry branch), so valid tokens can cause jobs to fail instead of backing off and recovering.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Code Review
This pull request introduces a new GitHubAuthError exception and logic to distinguish between authentication failures and rate limiting in the GitHubClient. It also tracks the source of the GitHub token for more informative error messages. The review feedback suggests optimizing the token resolution logic in the init method by using the walrus operator to eliminate redundant environment variable lookups.
| if token: | ||
| self._token_source = "explicit token argument (recipe-level field)" | ||
| elif os.environ.get("GH_TOKEN"): | ||
| self._token_source = "GH_TOKEN environment variable" | ||
| token = os.environ["GH_TOKEN"] | ||
| elif os.environ.get("GITHUB_TOKEN"): | ||
| self._token_source = "GITHUB_TOKEN environment variable" | ||
| token = os.environ["GITHUB_TOKEN"] | ||
| else: | ||
| raise RuntimeError("GH_TOKEN not set in environment") |
There was a problem hiding this comment.
This block for resolving the GitHub token can be simplified to avoid redundant lookups in os.environ. Currently, you call os.environ.get(...) to check for existence and then os.environ[...] to retrieve the value. Using the walrus operator (:=) allows you to assign the value and check for truthiness in one step, avoiding the second lookup and improving efficiency.
if token:
self._token_source = "explicit token argument (recipe-level field)"
elif (token := os.environ.get("GH_TOKEN")):
self._token_source = "GH_TOKEN environment variable"
elif (token := os.environ.get("GITHUB_TOKEN")):
self._token_source = "GITHUB_TOKEN environment variable"
else:
raise RuntimeError("GH_TOKEN not set in environment")References
- To improve efficiency, avoid redundant data iterations. Combine checks and transformations into a single loop and return computed values for callers to reuse.
Preserve GitHub token source through the repo seed scraper and fail fast on non-rate-limit auth errors while keeping genuine rate-limit retries.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request enhances GitHub token resolution and error handling by introducing a ResolvedToken structure and a new GitHubAuthError exception. It adds logic to distinguish between authentication failures and rate limiting, and improves Retry-After header parsing to support both durations and timestamps. Feedback focuses on centralizing the token resolution logic, which is currently duplicated between scraper.py and gh_client.py, to improve maintainability.
|
tested and validated by @wasimysaid . delivered ok to merge . merging |
What this PR does
Fixes #5325.
The Studio data-recipe GitHub Crawler treats
401 Unauthorizedfrom theGitHub GraphQL API as a transient network error and keeps retrying. Because
the token is invalid (rotated, expired, or wrong scope), every retry returns
401, the job stays alive emitting
[WARNING] GraphQL network error: 401 ... Retry.lines, and the user has to notice and cancel it manually.The same problem applies to
403 Forbiddenresponses that aren't actuallyrate-limited (the existing 403 branch already handles
Retry-AfterandX-RateLimit-Remaining: 0, but otherwise falls back to a permanenttime.sleep(60); continue).Root cause
gh_client.pyonly special-cases502/503/504and403/429. Any othernon-2xx — including
401— falls through tor.raise_for_status(), whichraises
requests.HTTPError. That gets caught by the outerexcept requests.RequestExceptionand logged as a "network error" — samepath as a real connection failure.
Fix
New
GitHubAuthError(RuntimeError)exception.New
_is_auth_failure(response)helper:401→ always auth failure.403→ auth failure unless the response carriesRetry-After(secondary rate limit) or
X-RateLimit-Remaining: 0(primary ratelimit).
Auth-failure check inserted in both
.graphql()and.rest()before theexisting
403/429rate-limit branch, so auth errors bypass thesleep-and-retry loop.
The token source resolved at construction time (recipe-level argument,
GH_TOKEN, orGITHUB_TOKEN) is captured onself._token_sourceandembedded in the raised error message — so the user knows exactly which
credential to rotate:
What does NOT change
Retry-AfterorX-RateLimit-Remaining: 0) stilltakes the existing retry path.
requests.ConnectionErroretc.) still retrythrough the existing
except requests.RequestExceptionbranch.GitHubAuthErroris aRuntimeError, not aRequestException, so itpropagates up and aborts the job as intended.
5xxupstream errors (502/503/504) still retry.Files
studio/backend/plugins/data-designer-github-repo-seed/src/data_designer_github_repo_seed/scraper_impl/gh_client.py🤖 Generated with Claude Code