From 34dc6857f031300ee765edd24f252975d6799465 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 10 Jul 2025 11:24:00 -0700 Subject: [PATCH 1/4] Add retry logic with exponential backoff to listOrganizations for user account creation delay --- core/control-plane/client.ts | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/core/control-plane/client.ts b/core/control-plane/client.ts index a1fe22099..6deca88b4 100644 --- a/core/control-plane/client.ts +++ b/core/control-plane/client.ts @@ -135,14 +135,33 @@ export class ControlPlaneClient { return []; } - try { - const resp = await this.request("ide/list-organizations", { - method: "GET", - }); - const { organizations } = (await resp.json()) as any; - return organizations; - } catch (e) { - return []; + // We try again here because when users sign up with an email domain that is + // captured by an org, we need to wait for the user account creation webhook to + // take effect. Otherwise the organization(s) won't show up. + let retries = 0; + const maxRetries = 5; + const maxWaitTime = 20000; // 20 seconds in milliseconds + + while (true) { + try { + const resp = await this.request("ide/list-organizations", { + method: "GET", + }); + const { organizations } = (await resp.json()) as any; + return organizations; + } catch (error: any) { + if (error?.message?.includes("404") && retries < maxRetries) { + retries++; + const waitTime = Math.min( + Math.pow(2, retries) * 100, + maxWaitTime / maxRetries, + ); + await new Promise((resolve) => setTimeout(resolve, waitTime)); + } else { + // If not a 404 error or we've exceeded max retries, rethrow + throw error; + } + } } } From 92cd0cc31bc864ef96f58f383621a2a76b64fa8f Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 14 Jul 2025 10:47:48 -0700 Subject: [PATCH 2/4] check status code --- core/control-plane/client.ts | 55 +++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/core/control-plane/client.ts b/core/control-plane/client.ts index 6deca88b4..63c140bcc 100644 --- a/core/control-plane/client.ts +++ b/core/control-plane/client.ts @@ -57,7 +57,7 @@ export class ControlPlaneClient { })); } - const resp = await this.request("ide/sync-secrets", { + const resp = await this.requestAndHandleError("ide/sync-secrets", { method: "POST", body: JSON.stringify({ fqsns, orgScopeId }), }); @@ -94,6 +94,15 @@ export class ControlPlaneClient { }, }); + return resp; + } + + private async requestAndHandleError( + path: string, + init: RequestInit, + ): Promise { + const resp = await this.request(path, init); + if (!resp.ok) { throw new Error( `Control plane request failed: ${resp.status} ${await resp.text()}`, @@ -121,7 +130,7 @@ export class ControlPlaneClient { ? `ide/list-assistants?organizationId=${organizationId}` : "ide/list-assistants"; - const resp = await this.request(url, { + const resp = await this.requestAndHandleError(url, { method: "GET", }); return (await resp.json()) as any; @@ -138,30 +147,30 @@ export class ControlPlaneClient { // We try again here because when users sign up with an email domain that is // captured by an org, we need to wait for the user account creation webhook to // take effect. Otherwise the organization(s) won't show up. + // This error manifests as a 404 (user not found) let retries = 0; const maxRetries = 5; const maxWaitTime = 20000; // 20 seconds in milliseconds while (true) { - try { - const resp = await this.request("ide/list-organizations", { - method: "GET", - }); - const { organizations } = (await resp.json()) as any; - return organizations; - } catch (error: any) { - if (error?.message?.includes("404") && retries < maxRetries) { - retries++; - const waitTime = Math.min( - Math.pow(2, retries) * 100, - maxWaitTime / maxRetries, - ); - await new Promise((resolve) => setTimeout(resolve, waitTime)); - } else { - // If not a 404 error or we've exceeded max retries, rethrow - throw error; - } + const resp = await this.request("ide/list-organizations", { + method: "GET", + }); + + if (resp.status === 404) { + retries++; + const waitTime = Math.min( + Math.pow(2, retries) * 100, + maxWaitTime / maxRetries, + ); + await new Promise((resolve) => setTimeout(resolve, waitTime)); + } else if (!resp.ok) { + throw new Error( + `Failed to list organizations (${resp.status}): ${await resp.text()}`, + ); } + const { organizations } = (await resp.json()) as any; + return organizations; } } @@ -177,7 +186,7 @@ export class ControlPlaneClient { : "ide/list-assistant-full-slugs"; try { - const resp = await this.request(url, { + const resp = await this.requestAndHandleError(url, { method: "GET", }); const { fullSlugs } = (await resp.json()) as any; @@ -193,7 +202,7 @@ export class ControlPlaneClient { } try { - const resp = await this.request("ide/free-trial-status", { + const resp = await this.requestAndHandleError("ide/free-trial-status", { method: "GET", }); return (await resp.json()) as FreeTrialStatus; @@ -224,7 +233,7 @@ export class ControlPlaneClient { params.set("vscode_uri_scheme", vsCodeUriScheme); } - const resp = await this.request( + const resp = await this.requestAndHandleError( `ide/get-models-add-on-checkout-url?${params.toString()}`, { method: "GET", From 907c576947fbe26b61410db0a5eb46dd907c75c3 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 14 Jul 2025 10:49:26 -0700 Subject: [PATCH 3/4] fix: continue through loop and return [] instead of throwing --- core/control-plane/client.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/control-plane/client.ts b/core/control-plane/client.ts index 63c140bcc..2287fb8c6 100644 --- a/core/control-plane/client.ts +++ b/core/control-plane/client.ts @@ -164,10 +164,12 @@ export class ControlPlaneClient { maxWaitTime / maxRetries, ); await new Promise((resolve) => setTimeout(resolve, waitTime)); + continue; } else if (!resp.ok) { - throw new Error( + console.warn( `Failed to list organizations (${resp.status}): ${await resp.text()}`, ); + return []; } const { organizations } = (await resp.json()) as any; return organizations; From 94d32b6aeb41e62602bc2e0cd9c01c723cd3bdc1 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 28 Jul 2025 11:20:21 -0700 Subject: [PATCH 4/4] fix: respect maxRetries --- core/control-plane/client.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/core/control-plane/client.ts b/core/control-plane/client.ts index c1686a75d..58b2ed71b 100644 --- a/core/control-plane/client.ts +++ b/core/control-plane/client.ts @@ -156,16 +156,22 @@ export class ControlPlaneClient { // take effect. Otherwise the organization(s) won't show up. // This error manifests as a 404 (user not found) let retries = 0; - const maxRetries = 5; + const maxRetries = 10; const maxWaitTime = 20000; // 20 seconds in milliseconds - while (true) { + while (retries < maxRetries) { const resp = await this.request("ide/list-organizations", { method: "GET", }); if (resp.status === 404) { retries++; + if (retries >= maxRetries) { + console.warn( + `Failed to list organizations after ${maxRetries} retries: user not found`, + ); + return []; + } const waitTime = Math.min( Math.pow(2, retries) * 100, maxWaitTime / maxRetries, @@ -181,6 +187,12 @@ export class ControlPlaneClient { const { organizations } = (await resp.json()) as any; return organizations; } + + // This should never be reached due to the while condition, but adding for safety + console.warn( + `Failed to list organizations after ${maxRetries} retries: maximum attempts exceeded`, + ); + return []; } public async listAssistantFullSlugs(