Changesets · Branches · Tags · Compare
| Changeset | 3cacb00b7d18f521fcd1dbd61a75710abc0cbbcd04c331ea8688eb6ec252de3f |
|---|---|
| Author | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-08 09:58 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-07-08 09:58 -07:00 |
| Parents | 05a3d4926f34, e5974e20392a |
2 files changed
modified extensions/vscode/src/stubs/WorkOsAuthProvider.ts +22 −22
| @@ -77,6 +77,7 @@ | ||
| 77 | 77 | { promise: Promise<string>; cancel: EventEmitter<void> } |
| 78 | 78 | >(); |
| 79 | 79 | private _refreshInterval: NodeJS.Timeout | null = null; |
| 80 | + private _isRefreshing = false; | |
| 80 | 81 | |
| 81 | 82 | private static EXPIRATION_TIME_MS = 1000 * 60 * 15; // 15 minutes |
| 82 | 83 | private static REFRESH_INTERVAL_MS = 1000 * 60 * 10; // 10 minutes |
| @@ -100,11 +101,11 @@ | ||
| 100 | 101 | this.secretStorage = new SecretStorage(context); |
| 101 | 102 | |
| 102 | 103 | // Immediately refresh any existing sessions |
| 103 | - this.refreshSessions(); | |
| 104 | + void this.refreshSessions(); | |
| 104 | 105 | |
| 105 | 106 | // Set up a regular interval to refresh tokens |
| 106 | 107 | this._refreshInterval = setInterval(() => { |
| 107 | - this.refreshSessions(); | |
| 108 | + void this.refreshSessions(); | |
| 108 | 109 | }, WorkOsAuthProvider.REFRESH_INTERVAL_MS); |
| 109 | 110 | } |
| 110 | 111 | |
| @@ -182,10 +183,18 @@ | ||
| 182 | 183 | } |
| 183 | 184 | |
| 184 | 185 | async refreshSessions() { |
| 186 | + // Prevent concurrent refresh operations | |
| 187 | + if (this._isRefreshing) { | |
| 188 | + return; | |
| 189 | + } | |
| 190 | + | |
| 185 | 191 | try { |
| 192 | + this._isRefreshing = true; | |
| 186 | 193 | await this._refreshSessions(); |
| 187 | 194 | } catch (e) { |
| 188 | 195 | console.error(`Error refreshing sessions: ${e}`); |
| 196 | + } finally { | |
| 197 | + this._isRefreshing = false; | |
| 189 | 198 | } |
| 190 | 199 | } |
| 191 | 200 | |
| @@ -198,12 +207,9 @@ | ||
| 198 | 207 | const finalSessions = []; |
| 199 | 208 | for (const session of sessions) { |
| 200 | 209 | 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 | + ); | |
| 207 | 213 | finalSessions.push({ |
| 208 | 214 | ...session, |
| 209 | 215 | accessToken: newSession.accessToken, |
| @@ -232,7 +238,6 @@ | ||
| 232 | 238 | private async _refreshSessionWithRetry( |
| 233 | 239 | refreshToken: string, |
| 234 | 240 | attempt = 1, |
| 235 | - maxAttempts = 3, | |
| 236 | 241 | baseDelay = 1000, |
| 237 | 242 | ): Promise<{ |
| 238 | 243 | accessToken: string; |
| @@ -242,30 +247,25 @@ | ||
| 242 | 247 | try { |
| 243 | 248 | return await this._refreshSession(refreshToken); |
| 244 | 249 | } 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 | |
| 246 | 251 | if ( |
| 247 | 252 | error.message?.includes("401") || |
| 248 | 253 | error.message?.includes("Invalid refresh token") || |
| 249 | - error.message?.includes("Unauthorized") || | |
| 250 | - attempt >= maxAttempts | |
| 254 | + error.message?.includes("Unauthorized") | |
| 251 | 255 | ) { |
| 252 | 256 | throw error; |
| 253 | 257 | } |
| 254 | 258 | |
| 255 | 259 | // For network errors or transient server issues, retry with backoff |
| 256 | 260 | // 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 | + ); | |
| 259 | 265 | |
| 260 | - // Schedule retry after delay | |
| 261 | 266 | return new Promise((resolve, reject) => { |
| 262 | 267 | setTimeout(() => { |
| 263 | - this._refreshSessionWithRetry( | |
| 264 | - refreshToken, | |
| 265 | - attempt + 1, | |
| 266 | - maxAttempts, | |
| 267 | - baseDelay, | |
| 268 | - ) | |
| 268 | + this._refreshSessionWithRetry(refreshToken, attempt + 1, baseDelay) | |
| 269 | 269 | .then(resolve) |
| 270 | 270 | .catch(reject); |
| 271 | 271 | }, delay); |
| @@ -360,7 +360,7 @@ | ||
| 360 | 360 | |
| 361 | 361 | return session; |
| 362 | 362 | } catch (e) { |
| 363 | - window.showErrorMessage(`Sign in failed: ${e}`); | |
| 363 | + void window.showErrorMessage(`Sign in failed: ${e}`); | |
| 364 | 364 | throw e; |
| 365 | 365 | } |
| 366 | 366 | } |
modified extensions/vscode/src/stubs/WorkOsAuthProvider.vitest.ts +2 −2
| @@ -641,7 +641,7 @@ | ||
| 641 | 641 | } |
| 642 | 642 | }); |
| 643 | 643 | |
| 644 | -it("should remove expired tokens when refresh fails", async () => { | |
| 644 | +it("should remove expired tokens when refresh fails with a 401 error", async () => { | |
| 645 | 645 | // Mock Date.now to return a time that makes tokens appear expired |
| 646 | 646 | const originalDateNow = Date.now; |
| 647 | 647 | const futureTime = Date.now() + 7200000; // 2 hours in the future |
| @@ -675,7 +675,7 @@ | ||
| 675 | 675 | global.setInterval = vi.fn().mockReturnValue(123 as any); |
| 676 | 676 | |
| 677 | 677 | // Refresh will fail with network error |
| 678 | - fetchMock.mockRejectedValueOnce(new Error("Network error")); | |
| 678 | + fetchMock.mockRejectedValueOnce(new Error("401")); | |
| 679 | 679 | |
| 680 | 680 | // Setup storage |
| 681 | 681 | mockSecretStorageGet.mockResolvedValue(JSON.stringify([expiredSession])); |