Merge pull request #6503 from continuedev/nate/logout-issues

fix: resolve VS Code logout bugs
This commit is contained in:
Patrick Erichsen 2025-07-08 09:58:12 -07:00 • committed by GitHub
commit 58b1fadbeb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 24 deletions

View file

@ -77,6 +77,7 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
{ promise: Promise<string>; cancel: EventEmitter<void> }
>();
private _refreshInterval: NodeJS.Timeout | null = null;
private _isRefreshing = false;
private static EXPIRATION_TIME_MS = 1000 * 60 * 15; // 15 minutes
private static REFRESH_INTERVAL_MS = 1000 * 60 * 10; // 10 minutes
@ -100,11 +101,11 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
this.secretStorage = new SecretStorage(context);
// Immediately refresh any existing sessions
this.refreshSessions();
void this.refreshSessions();
// Set up a regular interval to refresh tokens
this._refreshInterval = setInterval(() => {
this.refreshSessions();
void this.refreshSessions();
}, WorkOsAuthProvider.REFRESH_INTERVAL_MS);
}
@ -182,10 +183,18 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
}
async refreshSessions() {
// Prevent concurrent refresh operations
if (this._isRefreshing) {
return;
}
try {
this._isRefreshing = true;
await this._refreshSessions();
} catch (e) {
console.error(`Error refreshing sessions: ${e}`);
} finally {
this._isRefreshing = false;
}
}
@ -198,12 +207,9 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
const finalSessions = [];
for (const session of sessions) {
try {
// For expired tokens, don't use retries - if refresh fails, we drop the session
const refreshMethod = this.jwtIsExpiredOrInvalid(session.accessToken)
? this._refreshSession.bind(this) // Direct refresh for expired tokens
: this._refreshSessionWithRetry.bind(this); // Retry for valid tokens
const newSession = await refreshMethod(session.refreshToken);
const newSession = await this._refreshSessionWithRetry(
session.refreshToken,
);
finalSessions.push({
...session,
accessToken: newSession.accessToken,
@ -232,7 +238,6 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
private async _refreshSessionWithRetry(
refreshToken: string,
attempt = 1,
maxAttempts = 3,
baseDelay = 1000,
): Promise<{
accessToken: string;
@ -242,30 +247,25 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
try {
return await this._refreshSession(refreshToken);
} catch (error: any) {
// Don't retry for auth errors (401 Unauthorized) or if we've reached max attempts
// Don't retry for auth errors
if (
error.message?.includes("401") ||
error.message?.includes("Invalid refresh token") ||
error.message?.includes("Unauthorized") ||
attempt >= maxAttempts
error.message?.includes("Unauthorized")
) {
throw error;
}
// For network errors or transient server issues, retry with backoff
// Calculate exponential backoff delay with jitter
const delay =
baseDelay * Math.pow(2, attempt - 1) * (0.5 + Math.random() * 0.5);
const delay = Math.min(
baseDelay * Math.pow(2, attempt - 1) * (0.5 + Math.random() * 0.5),
2 * 60 * 1000, // 2 minutes
);
// Schedule retry after delay
return new Promise((resolve, reject) => {
setTimeout(() => {
this._refreshSessionWithRetry(
refreshToken,
attempt + 1,
maxAttempts,
baseDelay,
)
this._refreshSessionWithRetry(refreshToken, attempt + 1, baseDelay)
.then(resolve)
.catch(reject);
}, delay);
@ -360,7 +360,7 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
return session;
} catch (e) {
window.showErrorMessage(`Sign in failed: ${e}`);
void window.showErrorMessage(`Sign in failed: ${e}`);
throw e;
}
}

View file

@ -641,7 +641,7 @@ it("should preserve valid tokens during network errors by retrying", async () =>
}
});
it("should remove expired tokens when refresh fails", async () => {
it("should remove expired tokens when refresh fails with a 401 error", async () => {
// Mock Date.now to return a time that makes tokens appear expired
const originalDateNow = Date.now;
const futureTime = Date.now() + 7200000; // 2 hours in the future
@ -675,7 +675,7 @@ it("should remove expired tokens when refresh fails", async () => {
global.setInterval = vi.fn().mockReturnValue(123 as any);
// Refresh will fail with network error
fetchMock.mockRejectedValueOnce(new Error("Network error"));
fetchMock.mockRejectedValueOnce(new Error("401"));
// Setup storage
mockSecretStorageGet.mockResolvedValue(JSON.stringify([expiredSession]));