diff --git a/ui/src/api/client.test.ts b/ui/src/api/client.test.ts index cdd371a0119..3039e573933 100644 --- a/ui/src/api/client.test.ts +++ b/ui/src/api/client.test.ts @@ -3,6 +3,7 @@ import { createTenantSessionRecoveryCoordinator, tenantSessionRecovery, } from "@/lib/tenant-session-recovery"; +import { isIntentionalAbortReason } from "@/lib/intentional-abort"; import { api, detachInflightGet } from "./client"; import { toolsApi } from "./tools"; import { museInvitationsApi } from "./museInvitations"; @@ -279,6 +280,25 @@ describe("per-caller abort semantics", () => { expect(await fresh).toEqual({ value: "fresh" }); }); + it("marks the shared fetch's cancellation, whether every caller aborted or timed out", async () => { + vi.useFakeTimers(); + fetchMock.mockReturnValue(deferred().promise); + const controller = new AbortController(); + const aborted = api.get("/intentional-abort", { signal: controller.signal }); + const timed = api.get("/intentional-timeout", { timeoutMs: 100 }).catch(error => error); + const [abortedSignal, timedSignal] = fetchMock.mock.calls.map(([, init]) => init.signal as AbortSignal); + + controller.abort(); + await expect(aborted).rejects.toMatchObject({ name: "AbortError" }); + await vi.advanceTimersByTimeAsync(100); + expect(await timed).toMatchObject({ status: 408 }); + + expect(abortedSignal.reason).toMatchObject({ name: "AbortError" }); + expect(isIntentionalAbortReason(abortedSignal.reason)).toBe(true); + expect(isIntentionalAbortReason(timedSignal.reason)).toBe(true); + expect(isIntentionalAbortReason(controller.signal.reason)).toBe(false); + }); + it("rejects immediately if the caller signal is already aborted", async () => { const controller = new AbortController(); controller.abort(); diff --git a/ui/src/api/client.ts b/ui/src/api/client.ts index 1eee1bc3b2f..30cd8958a49 100644 --- a/ui/src/api/client.ts +++ b/ui/src/api/client.ts @@ -1,3 +1,4 @@ +import { intentionalAbortReason } from "@/lib/intentional-abort"; import { getPageVisibility, getVisibilityHeaderValue } from "@/lib/page-visibility"; import { tenantSessionRecovery } from "@/lib/tenant-session-recovery"; import { ApiError, apiErrorFromResponse } from "./api-error"; @@ -208,7 +209,7 @@ function coalescedGet(path: string, options?: RequestOptions): Promise { // Last caller gone before the fetch settled → abort the shared request. if (activeEntry.refs.size === 0) { if (inflightGets.get(key) === activeEntry) inflightGets.delete(key); - activeEntry.controller.abort(); + activeEntry.controller.abort(intentionalAbortReason()); } }; diff --git a/ui/src/lib/intentional-abort.ts b/ui/src/lib/intentional-abort.ts new file mode 100644 index 00000000000..2bbf885fbba --- /dev/null +++ b/ui/src/lib/intentional-abort.ts @@ -0,0 +1,11 @@ +const reasons = new WeakSet(); + +export function intentionalAbortReason(): DOMException { + const reason = new DOMException("The operation was aborted.", "AbortError"); + reasons.add(reason); + return reason; +} + +export function isIntentionalAbortReason(value: unknown): boolean { + return typeof value === "object" && value !== null && reasons.has(value); +} diff --git a/ui/src/lib/sentry.test.ts b/ui/src/lib/sentry.test.ts index 7970f75b050..d4787fd1102 100644 --- a/ui/src/lib/sentry.test.ts +++ b/ui/src/lib/sentry.test.ts @@ -559,8 +559,8 @@ describe("captured event shape against the real @sentry/browser SDK", () => { ...options, transport: () => ({ send: async () => ({}), flush: async () => true }), beforeSend: async (event, hint) => { - const enriched = await options.beforeSend?.(event, hint) ?? event; - onEvent(enriched as unknown as Record); + const enriched = options.beforeSend ? await options.beforeSend(event, hint) : event; + if (enriched) onEvent(enriched as unknown as Record); return enriched; }, }); @@ -590,6 +590,26 @@ describe("captured event shape against the real @sentry/browser SDK", () => { } }); + it("drops only the rejection carrying the app's own request cancellation", async () => { + const captured: Record[] = []; + const Sentry = await initRealSentryForTest((event) => { captured.push(event); }); + const { intentionalAbortReason } = await import("./intentional-abort"); + try { + window.onunhandledrejection?.call(window, { reason: intentionalAbortReason() } as PromiseRejectionEvent); + await Sentry.flush(2000); + expect(captured).toEqual([]); + + window.onunhandledrejection?.call(window, { + reason: new DOMException("signal is aborted without reason", "AbortError"), + } as PromiseRejectionEvent); + await Sentry.flush(2000); + expect(captured).toHaveLength(1); + expect(captured[0]).toMatchObject({ exception: { values: [{ type: "AbortError" }] } }); + } finally { + await Sentry.close(); + } + }); + it.each([ ["staging", "staging"], ["production", "production"], diff --git a/ui/src/lib/sentry.ts b/ui/src/lib/sentry.ts index 56e759849a2..b93b3fa684b 100644 --- a/ui/src/lib/sentry.ts +++ b/ui/src/lib/sentry.ts @@ -48,6 +48,7 @@ import { takeRejectionEventSnapshot, type BrowserErrorDetails, } from "./browser-error-context"; +import { isIntentionalAbortReason } from "./intentional-abort"; let queue: Promise = Promise.resolve(); @@ -211,6 +212,14 @@ export function buildBrowserSentryInitOptions( tracesSampleRate: 0, sendDefaultPii: false, beforeSend: (event, hint) => { + const isUnhandledRejection = event.exception?.values?.some((value) => + value.mechanism?.type === "onunhandledrejection" + || value.mechanism?.type === "auto.browser.global_handlers.onunhandledrejection", + ) ?? false; + if (isUnhandledRejection && isIntentionalAbortReason(hint.originalException)) { + takeRejectionEventSnapshot(classifyRejectionReason(hint.originalException)); + return null; + } // Global handlers do not pass through our React boundaries. Give their // reports the same bounded document state, without URLs or breadcrumbs. // Preserve a boundary's earlier snapshot across the asynchronous queue. @@ -224,10 +233,7 @@ export function buildBrowserSentryInitOptions( ...event.tags, browser_build_mode: import.meta.env.DEV ? "development" : "production", }; - if (event.exception?.values?.some((value) => - value.mechanism?.type === "onunhandledrejection" - || value.mechanism?.type === "auto.browser.global_handlers.onunhandledrejection", - )) { + if (isUnhandledRejection) { // The SDK supplies the rejected value directly. Classify without // reading object properties, coercing strings, or copying its value. const kind = classifyRejectionReason(hint.originalException);