Detect stale GitHub credentials and prompt re-authentication#8728
Merged
Conversation
Closed
Agent-Logs-Url: https://github.com/microsoft/vscode-pull-request-github/sessions/d7437db6-d6f8-4e32-80f2-d39c6660a4f2 Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix bad credentials error during GitHub authentication
Detect stale GitHub credentials and prompt re-authentication
May 7, 2026
alexr00
approved these changes
May 7, 2026
alexr00
left a comment
Member
There was a problem hiding this comment.
Kind of a dirty layering change, but looks like it will do the trick without requiring some significant refactoring.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds centralized detection of authentication-related API failures (REST + GraphQL) and wires a re-authentication trigger so the extension can proactively prompt users to sign in again when tokens are revoked/expired instead of repeatedly retrying with stale credentials.
Changes:
- Add
isAuthError(e)and extendRateLoggerto optionally invoke an auth-error handler from the existinglogApiErrorchokepoint. - Add
CredentialStore.handleAuthError(...)with de-duping and cooldown behavior, and wire it intocreateHubvia theRateLoggerhandler. - Add unit tests for
isAuthErrorandRateLogger.logApiErrorauth-handler invocation behavior.
Show a summary per file
| File | Description |
|---|---|
src/github/loggingOctokit.ts |
Adds auth-error detection and a callback hook from RateLogger.logApiError for all REST/GraphQL call paths. |
src/github/credentials.ts |
Implements de-duped/cooldown re-auth prompting and wires it to API error handling. |
src/test/github/loggingOctokit.test.ts |
Adds unit tests for auth-error detection and handler invocation/swallowing behavior. |
Copilot's findings
Comments suppressed due to low confidence (2)
src/github/credentials.ts:614
- The auth error handler passed to
RateLoggerstarts an async flow viavoid this.handleAuthError(authProviderId), but any rejection fromhandleAuthError(e.g., ifinitialize/getSessionthrows) will become an unhandled promise rejection. This should be caught and logged/ignored here (orhandleAuthErrorshould internally swallow and return{ canceled: true }on errors) to avoid destabilizing the extension host.
const rateLogger = new RateLogger(this._telemetry, isEnterprise(authProviderId), (_e) => {
void this.handleAuthError(authProviderId);
});
src/github/credentials.ts:330
_lastAuthErrorHandledAtis updated in thefinallyblock regardless of whetherrecreate(reason)succeeded or the user canceled/it threw. This can start the cooldown even though no new valid session was established, potentially suppressing re-prompting for 60s while requests continue to fail. Consider only setting the cooldown timestamp after a successful re-auth (and possibly not whencanceled: true).
const promise = (async () => {
try {
return await this.recreate(reason);
} finally {
this._handlingAuthError.delete(authProviderId);
this._lastAuthErrorHandledAt.set(authProviderId, Date.now());
}
- Files reviewed: 3/3 changed files
- Comments generated: 1
| const reason = vscode.l10n.t('Your GitHub{0} authentication session is no longer valid. Please sign in again.', getGitHubSuffix(authProviderId)); | ||
| const promise = (async () => { | ||
| try { | ||
| return await this.recreate(reason); |
Member
|
@copilot, address the unresolved PR comment. |
Agent-Logs-Url: https://github.com/microsoft/vscode-pull-request-github/sessions/56935e9d-de2d-4ee5-a5b6-44ba4fb409b4 Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Contributor
Author
aiday-mar
approved these changes
May 7, 2026
alexr00
approved these changes
May 8, 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.
API calls intermittently fail with
Bad credentials - https://docs.github.com/restonce the shared auth token expires/is revoked, and the extension keeps reusing the stale token instead of prompting re-auth (unlike e.g. the GitHub Actions extension). Users currently have to sign out, fully restart VS Code, and sign back in.Changes
Centralized auth-error detection (
loggingOctokit.ts): NewisAuthError(e)recognizes 401 status,Bad credentialsREST messages, and401 UnauthorizedGraphQL/network errors.RateLoggernow takes an optional auth-error callback and invokes it from the existinglogApiErrorpath, which is already wired intoLoggingOctokit.call,LoggingApolloClient.query, andLoggingApolloClient.mutate— so every REST and GraphQL call is covered without touching call sites.Re-auth trigger (
credentials.ts): NewCredentialStore.handleAuthError(authProviderId)forces a new session for the affected provider only (viainitialize(authProviderId, { forceNewSession: { detail: reason } })) with a localized prompt. It deliberately bypassesrecreate()/doCreate()so that when both GitHub.com and Enterprise are configured, only the provider that returned the auth error is re-prompted. It:Wiring (
createHub): ConstructsRateLoggerwith a handler bound to the appropriateAuthProvider.Tests:
src/test/github/loggingOctokit.test.tscoversisAuthErrorshapes (REST 401, message-based, GraphQL networkError, unrelated errors) and verifiesRateLogger.logApiErrorinvokes — and tolerates exceptions from — the handler.Sketch
The previous ad-hoc handlers (
categoryNode.ts"Bad credentials" branch,githubRepository.query401 branch) remain as-is; this layer catches the cases they miss (REST calls extension-wide, GraphQL mutations, and any other call site).