Elffuss BraidHub

kikocis/continue public

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

fix: resolve VS Code logout bugs
Changeset3cacb00b7d18f521fcd1dbd61a75710abc0cbbcd04c331ea8688eb6ec252de3f
AuthorPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-08 09:58 -07:00
CommitterGitHub <noreply@github.com> · 2025-07-08 09:58 -07:00
Parents05a3d4926f34, e5974e20392a

Browse the files at this changeset

2 files changed

modified extensions/vscode/src/stubs/WorkOsAuthProvider.ts +22 −22
@@ -77,6 +77,7 @@
7777 { promise: Promise<string>; cancel: EventEmitter<void> }
7878 >();
7979 private _refreshInterval: NodeJS.Timeout | null = null;
80+ private _isRefreshing = false;
8081
8182 private static EXPIRATION_TIME_MS = 1000 * 60 * 15; // 15 minutes
8283 private static REFRESH_INTERVAL_MS = 1000 * 60 * 10; // 10 minutes
@@ -100,11 +101,11 @@
100101 this.secretStorage = new SecretStorage(context);
101102
102103 // Immediately refresh any existing sessions
103- this.refreshSessions();
104+ void this.refreshSessions();
104105
105106 // Set up a regular interval to refresh tokens
106107 this._refreshInterval = setInterval(() => {
107- this.refreshSessions();
108+ void this.refreshSessions();
108109 }, WorkOsAuthProvider.REFRESH_INTERVAL_MS);
109110 }
110111
@@ -182,10 +183,18 @@
182183 }
183184
184185 async refreshSessions() {
186+ // Prevent concurrent refresh operations
187+ if (this._isRefreshing) {
188+ return;
189+ }
190+
185191 try {
192+ this._isRefreshing = true;
186193 await this._refreshSessions();
187194 } catch (e) {
188195 console.error(`Error refreshing sessions: ${e}`);
196+ } finally {
197+ this._isRefreshing = false;
189198 }
190199 }
191200
@@ -198,12 +207,9 @@
198207 const finalSessions = [];
199208 for (const session of sessions) {
200209 try {
201- // For expired tokens, don't use retries - if refresh fails, we drop the session
202- const refreshMethod = this.jwtIsExpiredOrInvalid(session.accessToken)
203- ? this._refreshSession.bind(this) // Direct refresh for expired tokens
204- : this._refreshSessionWithRetry.bind(this); // Retry for valid tokens
205-
206- const newSession = await refreshMethod(session.refreshToken);
210+ const newSession = await this._refreshSessionWithRetry(
211+ session.refreshToken,
212+ );
207213 finalSessions.push({
208214 ...session,
209215 accessToken: newSession.accessToken,
@@ -232,7 +238,6 @@
232238 private async _refreshSessionWithRetry(
233239 refreshToken: string,
234240 attempt = 1,
235- maxAttempts = 3,
236241 baseDelay = 1000,
237242 ): Promise<{
238243 accessToken: string;
@@ -242,30 +247,25 @@
242247 try {
243248 return await this._refreshSession(refreshToken);
244249 } catch (error: any) {
245- // Don't retry for auth errors (401 Unauthorized) or if we've reached max attempts
250+ // Don't retry for auth errors
246251 if (
247252 error.message?.includes("401") ||
248253 error.message?.includes("Invalid refresh token") ||
249- error.message?.includes("Unauthorized") ||
250- attempt >= maxAttempts
254+ error.message?.includes("Unauthorized")
251255 ) {
252256 throw error;
253257 }
254258
255259 // For network errors or transient server issues, retry with backoff
256260 // Calculate exponential backoff delay with jitter
257- const delay =
258- baseDelay * Math.pow(2, attempt - 1) * (0.5 + Math.random() * 0.5);
261+ const delay = Math.min(
262+ baseDelay * Math.pow(2, attempt - 1) * (0.5 + Math.random() * 0.5),
263+ 2 * 60 * 1000, // 2 minutes
264+ );
259265
260- // Schedule retry after delay
261266 return new Promise((resolve, reject) => {
262267 setTimeout(() => {
263- this._refreshSessionWithRetry(
264- refreshToken,
265- attempt + 1,
266- maxAttempts,
267- baseDelay,
268- )
268+ this._refreshSessionWithRetry(refreshToken, attempt + 1, baseDelay)
269269 .then(resolve)
270270 .catch(reject);
271271 }, delay);
@@ -360,7 +360,7 @@
360360
361361 return session;
362362 } catch (e) {
363- window.showErrorMessage(`Sign in failed: ${e}`);
363+ void window.showErrorMessage(`Sign in failed: ${e}`);
364364 throw e;
365365 }
366366 }
modified extensions/vscode/src/stubs/WorkOsAuthProvider.vitest.ts +2 −2
@@ -641,7 +641,7 @@
641641 }
642642 });
643643
644-it("should remove expired tokens when refresh fails", async () => {
644+it("should remove expired tokens when refresh fails with a 401 error", async () => {
645645 // Mock Date.now to return a time that makes tokens appear expired
646646 const originalDateNow = Date.now;
647647 const futureTime = Date.now() + 7200000; // 2 hours in the future
@@ -675,7 +675,7 @@
675675 global.setInterval = vi.fn().mockReturnValue(123 as any);
676676
677677 // Refresh will fail with network error
678- fetchMock.mockRejectedValueOnce(new Error("Network error"));
678+ fetchMock.mockRejectedValueOnce(new Error("401"));
679679
680680 // Setup storage
681681 mockSecretStorageGet.mockResolvedValue(JSON.stringify([expiredSession]));