Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions packages/shared/src/workspace-client/modular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,34 @@ 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 ||
request.signal?.aborted ||
Date.now() + waitMs > deadline
) {
if (!retryable || Date.now() + waitMs > deadline) {
return { ...response, body: new Response(body).body };
}
await new Promise((resolve) => setTimeout(resolve, waitMs));
await abortableWait(waitMs, request.signal);
}
}

// 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<void> {
return new Promise((resolve, reject) => {
if (signal?.aborted) {
reject(signal.reason);
return;
}
const timer = setTimeout(() => {
signal?.removeEventListener("abort", onAbort);
resolve();
}, ms);
const onAbort = () => {
clearTimeout(timer);
reject(signal?.reason);
};
signal?.addEventListener("abort", onAbort, { once: true });
});
}

/** A raw REST call against the workspace host. */
export interface WorkspaceRequest {
method: string;
Expand Down
69 changes: 68 additions & 1 deletion packages/shared/src/workspace-client/tests/modular.test.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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(
Expand All @@ -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 }),
});
}

Expand All @@ -1061,6 +1064,70 @@ describe("migration review fixes (host-only auth, DATABRICKS_AUTH_TYPE, retries,
expect(responses).toEqual([]);
});

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"}',
});
await expect(send("{}", controller.signal)).rejects.toBe(
controller.signal.reason,
);
expect(sent).toHaveLength(1);
expect(vi.getTimerCount()).toBe(0);
});

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,
Expand Down
Loading