:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tracing): stop trace waterfall infinite-scroll from pegging the CPU (#64304)
Frank Hamand0dcb7740bbdd
diff --git a/products/tracing/frontend/TraceWaterfallView.test.tsx b/products/tracing/frontend/TraceWaterfallView.test.tsxindex 9a5623f3a9dc..47d54c1b5c89 100644--- a/products/tracing/frontend/TraceWaterfallView.test.tsx+++ b/products/tracing/frontend/TraceWaterfallView.test.tsx@@ -104,4 +104,43 @@ describe('TraceWaterfallView', () => {expect(within(container).queryByLabelText('Collapse child spans')).toBeNull()expect(within(container).queryByLabelText('Collapse all spans')).toBeNull()})++ it('does not request more spans when hasMore is false', () => {+ const onLoadMore = jest.fn()+ render(<TraceWaterfallView spans={[root, child]} hasMore={false} onLoadMore={onLoadMore} />)++ expect(onLoadMore).not.toHaveBeenCalled()+ })++ it('requests more spans at most once per loaded count, even across rerenders with the same spans', () => {+ // The runaway bug: a page that returns no new rows leaves the window pinned at the bottom, so+ // the trigger refires every render. The guard pages once per loaded count — re-rendering with+ // the same spans (the loading-more flag toggling back) must not refire.+ const onLoadMore = jest.fn()+ const { rerender } = render(<TraceWaterfallView spans={[root, child]} hasMore onLoadMore={onLoadMore} />)++ expect(onLoadMore).toHaveBeenCalledTimes(1)++ rerender(<TraceWaterfallView spans={[root, child]} hasMore loadingMore onLoadMore={onLoadMore} />)+ rerender(<TraceWaterfallView spans={[root, child]} hasMore onLoadMore={onLoadMore} />)++ expect(onLoadMore).toHaveBeenCalledTimes(1)+ })++ it('requests more again once new spans grow the loaded count', () => {+ const grandchild = makeSpan({+ uuid: 'uuid-grandchild',+ span_id: 'span-grandchild',+ parent_span_id: 'span-child',+ name: 'grandchild-operation',+ })+ const onLoadMore = jest.fn()+ const { rerender } = render(<TraceWaterfallView spans={[root, child]} hasMore onLoadMore={onLoadMore} />)++ expect(onLoadMore).toHaveBeenCalledTimes(1)++ rerender(<TraceWaterfallView spans={[root, child, grandchild]} hasMore onLoadMore={onLoadMore} />)++ expect(onLoadMore).toHaveBeenCalledTimes(2)+ })})diff --git a/products/tracing/frontend/TraceWaterfallView.tsx b/products/tracing/frontend/TraceWaterfallView.tsxindex d6e74315d85b..7f2794a09c34 100644--- a/products/tracing/frontend/TraceWaterfallView.tsx+++ b/products/tracing/frontend/TraceWaterfallView.tsx@@ -553,19 +553,26 @@ export function TraceWaterfallView({const listRef = useListRef(null)// Infinite scroll: fetch the next page once the rendered window nears the end of the loaded spans.+ // Tracks the span count we last paged against so the trigger fires at most once per loaded count —+ // without this, a page that returns no new rows (dupes / end of data) leaves the window pinned at+ // the bottom and the trigger spins on every render, pegging the CPU.+ const lastRequestedSpanCountRef = useRef(-1)const handleRowsRendered = useCallback((_visibleRows: { startIndex: number; stopIndex: number },allRows: { startIndex: number; stopIndex: number }): void => {- if (- onLoadMore &&- hasMore &&- !loadingMore &&- allRows.stopIndex >= flatSpans.length - 1 - LOAD_MORE_THRESHOLD- ) {- onLoadMore()+ if (!onLoadMore || !hasMore || loadingMore) {+ return+ }+ if (allRows.stopIndex < flatSpans.length - 1 - LOAD_MORE_THRESHOLD) {+ return+ }+ if (lastRequestedSpanCountRef.current === flatSpans.length) {+ return}+ lastRequestedSpanCountRef.current = flatSpans.length+ onLoadMore()},[onLoadMore, hasMore, loadingMore, flatSpans.length])diff --git a/products/tracing/frontend/tracingDataLogic.ts b/products/tracing/frontend/tracingDataLogic.tsindex a54ec1cbd997..e4c2e8196cb5 100644--- a/products/tracing/frontend/tracingDataLogic.ts+++ b/products/tracing/frontend/tracingDataLogic.ts@@ -358,8 +358,18 @@ export const tracingDataLogic = kea<tracingDataLogicType>([if (values.traceLoadContext?.traceId !== traceId) {return values.traceSpans}+ // Dedupe against what's already loaded: the waterfall keys its tree by span_id,+ // so a page that overlaps prior pages wouldn't grow the rendered rows but would+ // still balloon this array (and pin the infinite-scroll trigger at the bottom,+ // spinning the CPU). If a page adds nothing new, stop paging.+ const seen = new Set(values.traceSpans.map((s) => s.span_id))+ const newSpans = (response.results as Span[]).filter((s) => !seen.has(s.span_id))+ if (newSpans.length === 0) {+ actions.setTracePagination(false, null)+ return values.traceSpans+ }actions.setTracePagination(!!response.hasMore, response.nextOffset ?? null)- return [...values.traceSpans, ...(response.results as Span[])]+ return [...values.traceSpans, ...newSpans]},},],