Repository navigation
Warn when an expiring token cannot be refreshed - #15213
Merged
Merged
Conversation
…refresh Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
…esh lock Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
…resh Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the expanded token-refresh warnings.
No open issues 🎉
What changed in this PR
Extends the existing token-refresh warning mechanism so users can understand authentication failures and recovery options.
Changes:
- Adds warnings for rejected tokens, failed exchanges, credential read/reload failures, and invalid responses.
- Adds test assertions for warnings and for silence when a credential disappears while waiting for the lock.
| File | Description |
|---|---|
| internal/config/refreshable.go | Records warnings for additional token-refresh failure outcomes. |
| internal/config/refreshable_telemetry_test.go | Covers new warnings and the missing-credential case. |
Review effort: Balanced
BagToad
approved these changes
Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When gh is signed in with an expiring OAuth token, it refreshes the token before using it if the token has expired or is about to. Callers of the refreshing token methods (the HTTP transport, the git credential helper,
gh auth token,gh auth status, and others) drop the error a failed refresh returns, so the failure itself never reaches the user. All they see is what happens next: usually a genericHTTP 401: Bad credentialsfrom the stale token, or, when the refresh token was rejected, requests that go out without a token at all.#15178 added a way to record warnings during a refresh, which
ghcmdprints to stderr at the end of every command, and used it for the skipped refresh, persist failure, and failed removal cases. This PR records a warning for the remaining failure outcomes, so the user (or a calling agent) can see why authentication is failing and what to do about it:These are added for better discovery and debugging on the user's side. The refresh behavior itself does not change: the same credential is returned, kept, or removed as before.
How did you test this change?
I built gh from
trunkand from this branch, and ran each against its own throwawayGH_CONFIG_DIRandXDG_STATE_HOME, with telemetry off and no token environment variables set. Each config held a made-up refreshable credential for a made-up accountfakeuseron github.com, stored inhosts.ymlwith anexpires_atin the past, so the first request had to refresh it. The made-up refresh token was rejected by github.com's token endpoint.Given that config, when I ran
gh api userwith thetrunkbuild, I saw:and
hosts.ymlafterwards had an empty entry forfakeuser, so the credential had been removed without any explanation.Given the same config, when I ran
gh api userwith this branch's build, I saw the same output followed by:and
hosts.ymlafterwards was the same as withtrunk.I did not exercise the other outcomes by hand, since they need a failing keyring, config reload, or token endpoint. They are covered by unit tests only.
Key points
addWarningalready drops duplicate messages. A command that makes many requests with a failing refresh prints the warning once.warnRefreshFailed.Notes for reviewers
Start at
refreshCredentialForUserininternal/config/refreshable.go; each outcome is handled there or in a helper next to it. The commits are split per outcome and can be reviewed one at a time. The tests are cases in the existingTestRefreshTelemetrytable ininternal/config/refreshable_telemetry_test.go, which asserts the recorded warning for each outcome, plus an assertion inTestRefreshTelemetryRecordsUnfinishedAttemptsfor the reload failure.Related: #15178 introduced the warnings mechanism and the first refresh warnings.
Authorship and follow-up
Who wrote this:
Who answers review comments: