From 3201908e0d2830ba450d27d08c144179b1704b03 Mon Sep 17 00:00:00 2001 From: ditadi Date: Mon, 16 Nov 2026 11:27:45 +0000 Subject: [PATCH 1/2] fix(shared): end workspace retry waits when the request aborts The 429 backoff slept on a bare setTimeout, so a caller's abort was held for the rest of the wait and then failed on the next send. Co-authored-by: Isaac Signed-off-by: ditadi --- .../shared/src/workspace-client/modular.ts | 23 +++++++++-- .../workspace-client/tests/modular.test.ts | 40 ++++++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/packages/shared/src/workspace-client/modular.ts b/packages/shared/src/workspace-client/modular.ts index acb4b051b..25c819b8b 100644 --- a/packages/shared/src/workspace-client/modular.ts +++ b/packages/shared/src/workspace-client/modular.ts @@ -397,15 +397,32 @@ async function sendWithRetry( const waitMs = Math.min(attempt * 1000 + 50 + Math.random() * 700, 10_000); if ( !retryable || - request.signal?.aborted || - Date.now() + waitMs > deadline + Date.now() + waitMs > deadline || + !(await waitUnlessAborted(waitMs, request.signal)) ) { return { ...response, body: new Response(body).body }; } - await new Promise((resolve) => setTimeout(resolve, waitMs)); } } +// Resolves `true` after `ms`, or `false` as soon as `signal` aborts, so a +// caller's timeout is not held for the rest of a backoff and then failed by a +// send on the aborted signal. +function waitUnlessAborted(ms: number, signal?: AbortSignal): Promise { + if (signal?.aborted) return Promise.resolve(false); + return new Promise((resolve) => { + const timer = setTimeout(() => { + signal?.removeEventListener("abort", onAbort); + resolve(true); + }, ms); + const onAbort = () => { + clearTimeout(timer); + resolve(false); + }; + signal?.addEventListener("abort", onAbort, { once: true }); + }); +} + /** A raw REST call against the workspace host. */ export interface WorkspaceRequest { method: string; diff --git a/packages/shared/src/workspace-client/tests/modular.test.ts b/packages/shared/src/workspace-client/tests/modular.test.ts index ef5ee12c1..510b92b89 100644 --- a/packages/shared/src/workspace-client/tests/modular.test.ts +++ b/packages/shared/src/workspace-client/tests/modular.test.ts @@ -1,3 +1,5 @@ +import { getEventListeners } from "node:events"; + import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; // The wrapper's own tests are the one place allowed to mock the SDK directly. @@ -1033,7 +1035,7 @@ describe("migration review fixes (host-only auth, DATABRICKS_AUTH_TYPE, retries, expect(cliCalls).toEqual([]); }); - async function send(body?: unknown) { + async function send(body?: unknown, signal?: AbortSignal) { buildWarehousesClient({ host: "https://x" }); const client = ctorOpts[0].httpClient as { send( @@ -1045,6 +1047,7 @@ describe("migration review fixes (host-only auth, DATABRICKS_AUTH_TYPE, retries, method: "POST", headers: new Headers(), ...(body !== undefined && { body }), + ...(signal && { signal }), }); } @@ -1061,6 +1064,41 @@ describe("migration review fixes (host-only auth, DATABRICKS_AUTH_TYPE, retries, expect(responses).toEqual([]); }); + test("transport ends a retry wait as soon as the request aborts: last 429 back, no further send", async () => { + vi.useFakeTimers(); + const controller = new AbortController(); + responses.push({ + statusCode: 429, + body: '{"error_code":"TOO_MANY_REQUESTS"}', + }); + const pending = send("{}", controller.signal); + await vi.advanceTimersByTimeAsync(100); // inside the >= 1050ms backoff + expect(sent).toHaveLength(1); + expect(vi.getTimerCount()).toBe(1); + controller.abort(); + expect(vi.getTimerCount()).toBe(0); + const res = await pending; + expect(res.statusCode).toBe(429); + expect(await new Response(res.body).text()).toContain("TOO_MANY_REQUESTS"); + expect(sent).toHaveLength(1); + }); + + test("transport still retries a 429 when the request signal never aborts, and drops its listener", async () => { + vi.useFakeTimers(); + const controller = new AbortController(); + responses.push({ + statusCode: 429, + body: '{"error_code":"TOO_MANY_REQUESTS"}', + }); + const pending = send("{}", controller.signal); + await vi.runAllTimersAsync(); + const res = await pending; + expect(res.statusCode).toBe(200); + expect(sent).toHaveLength(2); + expect(vi.getTimerCount()).toBe(0); + expect(getEventListeners(controller.signal, "abort")).toHaveLength(0); + }); + test("transport does not retry a plain 400 and keeps its body readable", async () => { responses.push({ statusCode: 400, From 5fe6012a96e709ba58cc6a428347d547609a1228 Mon Sep 17 00:00:00 2001 From: ditadi Date: Mon, 16 Nov 2026 15:30:32 +0000 Subject: [PATCH 2/2] fix(shared): reject an aborted retry wait with the signal's reason Returning the last 429 turned a cancel or timeout during a backoff into ApiError(429), which callers that recognize an abort by its error (the SQL warehouse connector, analytics) reported as a failure. The wait now rejects with signal.reason, like the SDK's own retry sleep. Co-authored-by: Isaac Signed-off-by: ditadi --- .../shared/src/workspace-client/modular.ts | 26 +++++----- .../workspace-client/tests/modular.test.ts | 47 +++++++++++++++---- 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/packages/shared/src/workspace-client/modular.ts b/packages/shared/src/workspace-client/modular.ts index 25c819b8b..84496f341 100644 --- a/packages/shared/src/workspace-client/modular.ts +++ b/packages/shared/src/workspace-client/modular.ts @@ -395,29 +395,29 @@ async function sendWithRetry( response.statusCode === 429 || TRANSIENT_ERROR_MESSAGES.some((m) => text.includes(m)); const waitMs = Math.min(attempt * 1000 + 50 + Math.random() * 700, 10_000); - if ( - !retryable || - Date.now() + waitMs > deadline || - !(await waitUnlessAborted(waitMs, request.signal)) - ) { + if (!retryable || Date.now() + waitMs > deadline) { return { ...response, body: new Response(body).body }; } + await abortableWait(waitMs, request.signal); } } -// Resolves `true` after `ms`, or `false` as soon as `signal` aborts, so a -// caller's timeout is not held for the rest of a backoff and then failed by a -// send on the aborted signal. -function waitUnlessAborted(ms: number, signal?: AbortSignal): Promise { - if (signal?.aborted) return Promise.resolve(false); - return new Promise((resolve) => { +// Waits `ms`, or rejects with `signal.reason` as soon as `signal` aborts, like +// the SDK's own retry sleep, so a cancel or timeout ends the backoff and still +// reaches the caller as that abort. +function abortableWait(ms: number, signal?: AbortSignal): Promise { + return new Promise((resolve, reject) => { + if (signal?.aborted) { + reject(signal.reason); + return; + } const timer = setTimeout(() => { signal?.removeEventListener("abort", onAbort); - resolve(true); + resolve(); }, ms); const onAbort = () => { clearTimeout(timer); - resolve(false); + reject(signal?.reason); }; signal?.addEventListener("abort", onAbort, { once: true }); }); diff --git a/packages/shared/src/workspace-client/tests/modular.test.ts b/packages/shared/src/workspace-client/tests/modular.test.ts index 510b92b89..61ee22ffd 100644 --- a/packages/shared/src/workspace-client/tests/modular.test.ts +++ b/packages/shared/src/workspace-client/tests/modular.test.ts @@ -1064,23 +1064,52 @@ describe("migration review fixes (host-only auth, DATABRICKS_AUTH_TYPE, retries, expect(responses).toEqual([]); }); - test("transport ends a retry wait as soon as the request aborts: last 429 back, no further send", async () => { + test.each([ + ["a cancel", () => undefined, "AbortError"], + [ + "a timeout", + () => new DOMException("The operation timed out.", "TimeoutError"), + "TimeoutError", + ], + ])( + "transport ends a retry wait on %s and rejects with the signal's reason, without another send", + async (_case, reasonFor, name) => { + vi.useFakeTimers(); + const controller = new AbortController(); + responses.push({ + statusCode: 429, + body: '{"error_code":"TOO_MANY_REQUESTS"}', + }); + const pending = send("{}", controller.signal); + await vi.advanceTimersByTimeAsync(100); // inside the >= 1050ms backoff + expect(sent).toHaveLength(1); + expect(vi.getTimerCount()).toBe(1); + const outcome = pending.then( + () => undefined, + (error: unknown) => error, + ); + controller.abort(reasonFor()); + expect(vi.getTimerCount()).toBe(0); + const error = await outcome; + expect(error).toBe(controller.signal.reason); + expect(error).toMatchObject({ name }); + expect(sent).toHaveLength(1); + }, + ); + + test("transport does not wait to retry a 429 whose signal is already aborted", async () => { vi.useFakeTimers(); const controller = new AbortController(); + controller.abort(); responses.push({ statusCode: 429, body: '{"error_code":"TOO_MANY_REQUESTS"}', }); - const pending = send("{}", controller.signal); - await vi.advanceTimersByTimeAsync(100); // inside the >= 1050ms backoff + await expect(send("{}", controller.signal)).rejects.toBe( + controller.signal.reason, + ); expect(sent).toHaveLength(1); - expect(vi.getTimerCount()).toBe(1); - controller.abort(); expect(vi.getTimerCount()).toBe(0); - const res = await pending; - expect(res.statusCode).toBe(429); - expect(await new Response(res.body).text()).toContain("TOO_MANY_REQUESTS"); - expect(sent).toHaveLength(1); }); test("transport still retries a 429 when the request signal never aborts, and drops its listener", async () => {