Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix(uplot): missing destroy uplot on unmount (#12182)

Vinicius Lourençoa5d4ef4498d0
diff --git a/frontend/src/lib/uPlotV2/components/UPlotChart/UPlotChart.tsx b/frontend/src/lib/uPlotV2/components/UPlotChart/UPlotChart.tsx
index 2b8f20da66..4f7c20e647 100644
--- a/frontend/src/lib/uPlotV2/components/UPlotChart/UPlotChart.tsx
+++ b/frontend/src/lib/uPlotV2/components/UPlotChart/UPlotChart.tsx
@@ -126,6 +126,15 @@ export default function UPlotChart({
}
}, [isDataEmpty, destroyPlot]);
+ /**
+ * Destroy the plot on unmount. Without this, uPlot's window-level
+ * `dppxchange` listener keeps the instance (and its whole detached DOM
+ * subtree) alive after the component is gone.
+ */
+ const destroyPlotRef = useRef(destroyPlot);
+ destroyPlotRef.current = destroyPlot;
+ useEffect(() => (): void => destroyPlotRef.current(), []);
+
/**
* Handle initialization and prop changes
*/
diff --git a/frontend/src/lib/uPlotV2/components/__tests__/UPlotChart.test.tsx b/frontend/src/lib/uPlotV2/components/__tests__/UPlotChart.test.tsx
index 8cd167995f..c85be4dd46 100644
--- a/frontend/src/lib/uPlotV2/components/__tests__/UPlotChart.test.tsx
+++ b/frontend/src/lib/uPlotV2/components/__tests__/UPlotChart.test.tsx
@@ -327,6 +327,32 @@ describe('UPlotChart', () => {
expect(firstInstance.destroy).toHaveBeenCalled();
expect(instances).toHaveLength(2);
});
+
+ it('destroys the instance and notifies callbacks on unmount', () => {
+ const plotRef = jest.fn();
+ const onDestroy = jest.fn();
+
+ const { unmount } = render(
+ <UPlotChart
+ config={createMockConfig()}
+ data={validData}
+ width={600}
+ height={400}
+ plotRef={plotRef}
+ onDestroy={onDestroy}
+ />,
+ { wrapper: Wrapper },
+ );
+
+ const firstInstance = instances[0];
+ plotRef.mockClear();
+
+ unmount();
+
+ expect(onDestroy).toHaveBeenCalledWith(firstInstance);
+ expect(firstInstance.destroy).toHaveBeenCalledTimes(1);
+ expect(plotRef).toHaveBeenCalledWith(null);
+ });
});
describe('spanGaps data transformation', () => {