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.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix: prevent memory leaks from uncleaned uPlot event listeners (#9320)
Abhi kumar88f7502a1595
diff --git a/frontend/src/components/Uplot/Uplot.tsx b/frontend/src/components/Uplot/Uplot.tsxindex 1df0df63db..8bb044552a 100644--- a/frontend/src/components/Uplot/Uplot.tsx+++ b/frontend/src/components/Uplot/Uplot.tsx@@ -18,11 +18,6 @@ import UPlot from 'uplot';import { dataMatch, optionsUpdateState } from './utils';-// Extended uPlot interface with custom properties-interface ExtendedUPlot extends uPlot {- _legendScrollCleanup?: () => void;-}-export interface UplotProps {options: uPlot.Options;data: uPlot.AlignedData;@@ -71,12 +66,6 @@ const Uplot = forwardRef<ToggleGraphProps | undefined, UplotProps>(const destroy = useCallback((chart: uPlot | null) => {if (chart) {- // Clean up legend scroll event listener- const extendedChart = chart as ExtendedUPlot;- if (extendedChart._legendScrollCleanup) {- extendedChart._legendScrollCleanup();- }-onDeleteRef.current?.(chart);chart.destroy();chartRef.current = null;diff --git a/frontend/src/lib/uPlotLib/getUplotChartOptions.ts b/frontend/src/lib/uPlotLib/getUplotChartOptions.tsindex 8a8d60bc82..fc90ad54d8 100644--- a/frontend/src/lib/uPlotLib/getUplotChartOptions.ts+++ b/frontend/src/lib/uPlotLib/getUplotChartOptions.ts@@ -36,6 +36,7 @@ import { getYAxisScale } from './utils/getYAxisScale';interface ExtendedUPlot extends uPlot {_legendScrollCleanup?: () => void;_tooltipCleanup?: () => void;+ _legendElementCleanup?: Array<() => void>;}export interface GetUPlotChartOptions {@@ -473,6 +474,9 @@ export const getUPlotChartOptions = ({if (legend) {const legendElement = legend as HTMLElement;+ // Initialize cleanup array for legend element listeners+ (self as ExtendedUPlot)._legendElementCleanup = [];+// Apply enhanced legend stylingif (enhancedLegend) {applyEnhancedLegendStyling(@@ -639,6 +643,17 @@ export const getUPlotChartOptions = ({thElement.addEventListener('mouseenter', showTooltip);thElement.addEventListener('mouseleave', hideTooltip);+ // Store cleanup function for tooltip listeners+ (self as ExtendedUPlot)._legendElementCleanup?.push(() => {+ thElement.removeEventListener('mouseenter', showTooltip);+ thElement.removeEventListener('mouseleave', hideTooltip);+ // Cleanup any lingering tooltip+ if (tooltipElement) {+ tooltipElement.remove();+ tooltipElement = null;+ }+ });+// Add click handlers for marker and text separatelyconst currentMarker = thElement.querySelector('.u-marker');const textElement = thElement.querySelector('.legend-text');@@ -658,7 +673,7 @@ export const getUPlotChartOptions = ({// Marker click handler - checkbox behavior (toggle individual series)if (currentMarker) {- currentMarker.addEventListener('click', (e) => {+ const markerClickHandler = (e: Event): void => {e.stopPropagation?.(); // Prevent event bubbling to text handlerif (stackChart) {@@ -680,12 +695,19 @@ export const getUPlotChartOptions = ({return newGraphVisibilityStates;});}+ };++ currentMarker.addEventListener('click', markerClickHandler);++ // Store cleanup function for marker click listener+ (self as ExtendedUPlot)._legendElementCleanup?.push(() => {+ currentMarker.removeEventListener('click', markerClickHandler);});}// Text click handler - show only/show all behavior (existing behavior)if (textElement) {- textElement.addEventListener('click', (e) => {+ const textClickHandler = (e: Event): void => {e.stopPropagation?.(); // Prevent event bubblingif (stackChart) {@@ -716,6 +738,13 @@ export const getUPlotChartOptions = ({return newGraphVisibilityStates;});}+ };++ textElement.addEventListener('click', textClickHandler);++ // Store cleanup function for text click listener+ (self as ExtendedUPlot)._legendElementCleanup?.push(() => {+ textElement.removeEventListener('click', textClickHandler);});}}@@ -723,6 +752,33 @@ export const getUPlotChartOptions = ({}},],+ destroy: [+ (self): void => {+ // Clean up legend scroll listener+ if ((self as ExtendedUPlot)._legendScrollCleanup) {+ (self as ExtendedUPlot)._legendScrollCleanup?.();+ (self as ExtendedUPlot)._legendScrollCleanup = undefined;+ }++ // Clean up tooltip global listener+ if ((self as ExtendedUPlot)._tooltipCleanup) {+ (self as ExtendedUPlot)._tooltipCleanup?.();+ (self as ExtendedUPlot)._tooltipCleanup = undefined;+ }++ // Clean up all legend element listeners+ if ((self as ExtendedUPlot)._legendElementCleanup) {+ (self as ExtendedUPlot)._legendElementCleanup?.forEach((cleanup) => {+ cleanup();+ });+ (self as ExtendedUPlot)._legendElementCleanup = [];+ }++ // Clean up any remaining tooltips in DOM+ const existingTooltips = document.querySelectorAll('.legend-tooltip');+ existingTooltips.forEach((tooltip) => tooltip.remove());+ },+ ],},series: customSeries? customSeries(apiResponse?.data?.result || [])