Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Add feature-detected fallback for AbortSignal.throwIfAborted (#85214)

Sébastien78cd9d3eedaf
diff --git a/frontend/src/metabase/common/components/EmojiPicker/emoji-data.ts b/frontend/src/metabase/common/components/EmojiPicker/emoji-data.ts
index 0db403477e2..4ff7d6eb320 100644
--- a/frontend/src/metabase/common/components/EmojiPicker/emoji-data.ts
+++ b/frontend/src/metabase/common/components/EmojiPicker/emoji-data.ts
@@ -1,5 +1,7 @@
import type { EmojiData, EmojiDataResolver } from "frimousse";
+import { throwIfSignalAborted } from "metabase/utils/retry";
+
type EmojiSupport = {
emojiVersion: number;
countryFlags: boolean;
@@ -115,7 +117,9 @@ export const resolveEmojiData: EmojiDataResolver = async (
const { default: data } = await import(
/* webpackChunkName: "emoji-data" */ "./emojibase/en.json"
);
- signal?.throwIfAborted();
+ if (signal) {
+ throwIfSignalAborted(signal);
+ }
detectedSupport ??= detectEmojiSupport(data.emojis);
diff --git a/frontend/src/metabase/dashboard/actions/data-fetching.ts b/frontend/src/metabase/dashboard/actions/data-fetching.ts
index 4655ee89ebc..a81d1271c3e 100644
--- a/frontend/src/metabase/dashboard/actions/data-fetching.ts
+++ b/frontend/src/metabase/dashboard/actions/data-fetching.ts
@@ -54,6 +54,7 @@ import {
isVirtualDashCard,
} from "metabase/utils/dashboard";
import { PERFORMANCE_MARKS, markOnce } from "metabase/utils/performance-marks";
+import { throwIfSignalAborted } from "metabase/utils/retry";
import { uuid } from "metabase/utils/uuid";
import { getParameterValuesBySlug } from "metabase-lib/v1/parameters/utils/parameter-values";
import type {
@@ -922,7 +923,7 @@ export const fetchDashboard = createAsyncThunk(
// A request without the signal, such as the prefetched path's metadata,
// can outlive a newer fetch or an unmount, so its result is dropped here
- signal.throwIfAborted();
+ throwIfSignalAborted(signal);
fetchDashboardCancellation = null;
const isUsingCachedResults = entities != null;
diff --git a/frontend/src/metabase/embedding/mcp/auth/mcpUiAuth.ts b/frontend/src/metabase/embedding/mcp/auth/mcpUiAuth.ts
index 792cacd5f97..cfd3b3cb45c 100644
--- a/frontend/src/metabase/embedding/mcp/auth/mcpUiAuth.ts
+++ b/frontend/src/metabase/embedding/mcp/auth/mcpUiAuth.ts
@@ -1,7 +1,7 @@
import type { App } from "@modelcontextprotocol/ext-apps/react";
import { PLUGIN_API } from "metabase/api/client";
-import { retry } from "metabase/utils/retry";
+import { retry, throwIfSignalAborted } from "metabase/utils/retry";
import {
MCP_APPS_METADATA_KEY,
@@ -51,7 +51,7 @@ export async function refreshMcpUiAuth(
},
);
- abortController.signal.throwIfAborted();
+ throwIfSignalAborted(abortController.signal);
return { auth, expiresAt: requestStartedAt + UI_CREDENTIAL_VALIDITY_MS };
}
diff --git a/frontend/src/metabase/utils/retry.ts b/frontend/src/metabase/utils/retry.ts
index b5466180e58..ccb740ba8ad 100644
--- a/frontend/src/metabase/utils/retry.ts
+++ b/frontend/src/metabase/utils/retry.ts
@@ -1,5 +1,21 @@
import { delay } from "metabase/utils/promise";
+/**
+ * Feature-detected fallback for AbortSignal.throwIfAborted, which is
+ * unavailable in Chrome < 100, Firefox < 97, and Safari < 15.4.
+ * Also guards against absent `signal.reason` (Chrome < 98).
+ */
+export function throwIfSignalAborted(signal: AbortSignal): void {
+ if (typeof signal.throwIfAborted === "function") {
+ signal.throwIfAborted();
+ } else if (signal.aborted) {
+ const reason =
+ signal.reason ??
+ new DOMException("The operation was aborted.", "AbortError");
+ throw reason;
+ }
+}
+
export type RetryOptions = {
/**
* Maximum number of retries after the initial attempt.
@@ -51,7 +67,9 @@ export async function retry<T>(
for (let attempt = 0; ; attempt++) {
// Bail before each attempt if aborted — including right after an
// interrupted backoff — rather than making another call to `fn`.
- signal?.throwIfAborted();
+ if (signal) {
+ throwIfSignalAborted(signal);
+ }
try {
return await fn();
} catch (error) {
diff --git a/frontend/src/metabase/utils/retry.unit.spec.ts b/frontend/src/metabase/utils/retry.unit.spec.ts
index 79f6607c0b7..6303ab71333 100644
--- a/frontend/src/metabase/utils/retry.unit.spec.ts
+++ b/frontend/src/metabase/utils/retry.unit.spec.ts
@@ -168,6 +168,80 @@ describe("retry", () => {
expect(fn).not.toHaveBeenCalled();
});
+ describe("when AbortSignal.throwIfAborted is unavailable", () => {
+ function signalWithoutThrowIfAborted(): {
+ controller: AbortController;
+ signal: AbortSignal;
+ } {
+ const controller = new AbortController();
+ Object.defineProperty(controller.signal, "throwIfAborted", {
+ configurable: true,
+ value: undefined,
+ });
+ return { controller, signal: controller.signal };
+ }
+
+ it("throws before calling fn when the signal is already aborted", async () => {
+ const { controller, signal } = signalWithoutThrowIfAborted();
+ controller.abort();
+ const fn = jest.fn().mockResolvedValue("ok");
+
+ await expect(
+ retry(fn, {
+ maxRetries: 0,
+ shouldRetry: () => false,
+ signal,
+ }),
+ ).rejects.toBeInstanceOf(DOMException);
+
+ expect(fn).not.toHaveBeenCalled();
+ });
+
+ it("preserves a custom abort reason", async () => {
+ const { controller, signal } = signalWithoutThrowIfAborted();
+ const customReason = new Error("custom reason");
+ controller.abort(customReason);
+ const fn = jest.fn().mockResolvedValue("ok");
+
+ await expect(
+ retry(fn, {
+ maxRetries: 0,
+ shouldRetry: () => false,
+ signal,
+ }),
+ ).rejects.toBe(customReason);
+
+ expect(fn).not.toHaveBeenCalled();
+ });
+
+ it("ends the backoff early when aborted during a retry wait", async () => {
+ const { controller, signal } = signalWithoutThrowIfAborted();
+ const error = new Error("flaky");
+ const fn = jest.fn().mockRejectedValue(error);
+
+ const promise = retry(fn, {
+ maxRetries: 5,
+ shouldRetry: () => true,
+ delayMs: () => 1000,
+ signal,
+ });
+ const resultPromise = promise.catch((e: unknown) => e);
+
+ // First attempt fails and schedules a 1000ms backoff.
+ await Promise.resolve();
+ expect(fn).toHaveBeenCalledTimes(1);
+
+ controller.abort();
+ await jest.runAllTimersAsync();
+
+ const result = await resultPromise;
+ expect(result).toBeInstanceOf(DOMException);
+ // AbortError name check — reason falls back to DOMException
+ expect((result as DOMException).name).toBe("AbortError");
+ expect(fn).toHaveBeenCalledTimes(1);
+ });
+ });
+
it("waits delayMs(attempt) between attempts", async () => {
const fn = jest
.fn()