Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-8781 fix(core): cap the dev User Timing buffer so React render measures stop leaking (#10131)

Jiayuan Zhangbadeeae53572
diff --git a/apps/desktop/src/renderer/src/main.tsx b/apps/desktop/src/renderer/src/main.tsx
index 3bb7baeb0e..0ba4c09790 100644
--- a/apps/desktop/src/renderer/src/main.tsx
+++ b/apps/desktop/src/renderer/src/main.tsx
@@ -1,4 +1,5 @@
import ReactDOM from "react-dom/client";
+import { installUserTimingCap } from "@multica/core/diagnostics";
import App from "./App";
import { AppCrashBoundary } from "./components/app-crash-boundary";
import { launchWorkspaceSlug } from "./platform/workspace-windows";
@@ -43,6 +44,10 @@ if (import.meta.env.DEV && import.meta.env.VITE_REACT_GRAB) {
document.head.appendChild(grab);
}
+// React's development build never clears its render measures; see
+// installUserTimingCap.
+if (import.meta.env.DEV) installUserTimingCap();
+
// A window opened for a workspace starts on it rather than on whichever
// workspace was last active in any window.
const launchSlug = launchWorkspaceSlug();
diff --git a/apps/web/components/web-providers.test.tsx b/apps/web/components/web-providers.test.tsx
new file mode 100644
index 0000000000..5bda25587e
--- /dev/null
+++ b/apps/web/components/web-providers.test.tsx
@@ -0,0 +1,53 @@
+import { renderToString } from "react-dom/server";
+import { render } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+const installUserTimingCap = vi.hoisted(() => vi.fn());
+
+vi.mock("@multica/core/diagnostics", () => ({ installUserTimingCap }));
+vi.mock("@multica/core/platform", () => ({
+ CoreProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
+}));
+vi.mock("@multica/core/i18n/browser", () => ({
+ createBrowserCookieLocaleAdapter: () => ({}),
+}));
+vi.mock("@/platform/navigation", () => ({
+ WebNavigationProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
+}));
+vi.mock("@/platform/scroll-restoration", () => ({
+ WebScrollRestorationProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
+}));
+vi.mock("@/platform/user-locale-sync", () => ({ useUserLocaleSyncEnabled: () => false }));
+
+import { WebProviders } from "./web-providers";
+
+const tree = (
+ <WebProviders locale="en" resources={{}}>
+ <span />
+ </WebProviders>
+);
+
+afterEach(() => {
+ vi.unstubAllEnvs();
+ installUserTimingCap.mockClear();
+});
+
+describe("WebProviders user timing cap", () => {
+ it("installs the cap in development", () => {
+ vi.stubEnv("NODE_ENV", "development");
+ render(tree);
+ expect(installUserTimingCap).toHaveBeenCalledTimes(1);
+ });
+
+ it("does not install the cap in production", () => {
+ vi.stubEnv("NODE_ENV", "production");
+ render(tree);
+ expect(installUserTimingCap).not.toHaveBeenCalled();
+ });
+
+ it("does not install the cap during server rendering", () => {
+ vi.stubEnv("NODE_ENV", "development");
+ renderToString(tree);
+ expect(installUserTimingCap).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/components/web-providers.tsx b/apps/web/components/web-providers.tsx
index ee7aeb36d4..4ff3f1b875 100644
--- a/apps/web/components/web-providers.tsx
+++ b/apps/web/components/web-providers.tsx
@@ -1,7 +1,8 @@
"use client";
-import { useMemo } from "react";
+import { useEffect, useMemo } from "react";
import { CoreProvider } from "@multica/core/platform";
+import { installUserTimingCap } from "@multica/core/diagnostics";
import { createBrowserCookieLocaleAdapter } from "@multica/core/i18n/browser";
import type { LocaleResources, SupportedLocale } from "@multica/core/i18n";
import packageJson from "../package.json";
@@ -66,6 +67,11 @@ export function WebProviders({
[],
);
const localeAdapter = useMemo(() => createBrowserCookieLocaleAdapter(), []);
+ // React's development build never clears its render measures; see
+ // installUserTimingCap.
+ useEffect(() => {
+ if (process.env.NODE_ENV === "development") installUserTimingCap();
+ }, []);
return (
<CoreProvider
apiBaseUrl={apiBaseUrl}
diff --git a/packages/core/diagnostics/index.ts b/packages/core/diagnostics/index.ts
index 36b80ab28c..87a30fac6e 100644
--- a/packages/core/diagnostics/index.ts
+++ b/packages/core/diagnostics/index.ts
@@ -1,4 +1,5 @@
export { installFreezeWatchdog } from "./freeze-watchdog";
+export { installUserTimingCap } from "./user-timing-cap";
export {
bucketDiagnosticPath,
getDiagnosticRoute,
diff --git a/packages/core/diagnostics/user-timing-cap.test.ts b/packages/core/diagnostics/user-timing-cap.test.ts
new file mode 100644
index 0000000000..0a2d1d3910
--- /dev/null
+++ b/packages/core/diagnostics/user-timing-cap.test.ts
@@ -0,0 +1,52 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+let clearMeasures: ReturnType<typeof vi.fn>;
+
+async function load() {
+ vi.resetModules();
+ return (await import("./user-timing-cap")).installUserTimingCap;
+}
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ clearMeasures = vi.fn();
+ vi.stubGlobal("window", {});
+ vi.stubGlobal("performance", { clearMeasures });
+});
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.useRealTimers();
+});
+
+describe("installUserTimingCap", () => {
+ it("empties the measure buffer once a minute", async () => {
+ const installUserTimingCap = await load();
+ installUserTimingCap();
+
+ vi.advanceTimersByTime(59_999);
+ expect(clearMeasures).not.toHaveBeenCalled();
+ vi.advanceTimersByTime(1);
+ expect(clearMeasures).toHaveBeenCalledTimes(1);
+ vi.advanceTimersByTime(60_000);
+ expect(clearMeasures).toHaveBeenCalledTimes(2);
+ });
+
+ it("installs a single timer when called repeatedly", async () => {
+ const installUserTimingCap = await load();
+ installUserTimingCap();
+ installUserTimingCap();
+
+ vi.advanceTimersByTime(60_000);
+ expect(clearMeasures).toHaveBeenCalledTimes(1);
+ });
+
+ it("does nothing on the server", async () => {
+ vi.stubGlobal("window", undefined);
+ const installUserTimingCap = await load();
+ installUserTimingCap();
+
+ vi.advanceTimersByTime(120_000);
+ expect(clearMeasures).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/core/diagnostics/user-timing-cap.ts b/packages/core/diagnostics/user-timing-cap.ts
new file mode 100644
index 0000000000..d44362d3bd
--- /dev/null
+++ b/packages/core/diagnostics/user-timing-cap.ts
@@ -0,0 +1,32 @@
+// Cap on the User Timing buffer for development windows — shared by web and
+// desktop, which each install it from their entry point only in development.
+//
+// React's development build records a `performance.measure` entry, carrying a
+// changed-props `detail`, for component renders and never clears them. The
+// browser keeps User Timing entries for the life of the page, so a dev window
+// that stays open while runs stream grows without bound: about 1.4KB per entry
+// at 50–200 entries a second, mostly in native memory that V8's heap limit
+// does not cap. A Desktop Canary left open for two days reached 30GB+
+// (MUL-8781). Production React emits no measures, so production never needs
+// this.
+//
+// DevTools Performance recordings capture measures as they are emitted, so
+// emptying the buffer loses nothing a recording would show.
+
+const CLEAR_INTERVAL_MS = 60_000;
+
+let installed = false;
+
+/**
+ * Periodically empty the User Timing measure buffer. Safe to call multiple
+ * times (idempotent) and safe on the server. The caller decides whether this
+ * is a development build.
+ */
+export function installUserTimingCap(): void {
+ if (installed) return;
+ if (typeof window === "undefined") return;
+ if (typeof performance === "undefined" || typeof performance.clearMeasures !== "function") return;
+ installed = true;
+
+ setInterval(() => performance.clearMeasures(), CLEAR_INTERVAL_MS);
+}