: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(frontend): three memory-leak mop-ups from the background-tab investigation (#62698)
Paul D'Ambrab218e7e426d6
diff --git a/frontend/src/lib/components/Map/Map.tsx b/frontend/src/lib/components/Map/Map.tsxindex 06e3ebb83232..e35fee09e4c0 100644--- a/frontend/src/lib/components/Map/Map.tsx+++ b/frontend/src/lib/components/Map/Map.tsx@@ -76,6 +76,10 @@ export function MapComponent({ center, markers, className }: MapProps): JSX.Elemmarker.addTo(map.current)}}+ return () => {+ map.current?.remove()+ map.current = null+ }}, [isDarkModeOn]) // oxlint-disable-line react-hooks/exhaustive-depsuseResizeObserver({diff --git a/frontend/src/scenes/activity/live/liveEventsLogic.tsx b/frontend/src/scenes/activity/live/liveEventsLogic.tsxindex 0589d6d97ec5..5c1288a9c9ee 100644--- a/frontend/src/scenes/activity/live/liveEventsLogic.tsx+++ b/frontend/src/scenes/activity/live/liveEventsLogic.tsx@@ -106,10 +106,8 @@ export const liveEventsLogic = kea<liveEventsLogicType>([clearEvents: () => {cache.batch = []},- updateEventsConnection: async () => {- if (cache.eventSourceController) {- cache.eventSourceController.abort()- }+ updateEventsConnection: () => {+ cache.disposables.dispose('eventsConnection')if (values.streamPaused) {return@@ -138,46 +136,49 @@ export const liveEventsLogic = kea<liveEventsLogicType>([}url.searchParams.append('columns', '$current_url,$screen_name')- cache.batch = []- cache.eventSourceController = new AbortController()-- await api.stream(url.toString(), {- headers: {- Authorization: `Bearer ${values.currentTeam.live_events_token}`,- },- signal: cache.eventSourceController.signal,- onMessage: (event) => {- lemonToast.dismiss(ERROR_TOAST_ID)- let eventData: LiveEvent- try {- eventData = JSON.parse(event.data)- } catch {- // Drop malformed stream payloads rather than throwing inside the listener- return- }- cache.batch.push(eventData)- if (cache.batch.length >= 10 || performance.now() - (values.lastBatchTimestamp || 0) > 300) {- actions.addEvents(cache.batch)- cache.batch.length = 0- }- },- onError: (error) => {- if (!cache.hasShownLiveStreamErrorToast && props.showLiveStreamErrorToast) {- console.error('Failed to poll events. You likely have no events coming in.', error)- lemonToast.error(`No live events found. Continuing to retry in the background…`, {- icon: <Spinner />,- toastId: ERROR_TOAST_ID,- autoClose: false,- })- cache.hasShownLiveStreamErrorToast = true- }- },- })+ // Managed as a disposable so the long-lived streaming Response is aborted when+ // the tab is hidden and reopened on visibilitychange — an open stream on an idle+ // background tab accumulates off-heap in Blink's partition_alloc/buffer.+ cache.disposables.add(() => {+ cache.batch = []+ const controller = new AbortController()+ void api.stream(url.toString(), {+ headers: {+ Authorization: `Bearer ${values.currentTeam?.live_events_token}`,+ },+ signal: controller.signal,+ onMessage: (event) => {+ lemonToast.dismiss(ERROR_TOAST_ID)+ let eventData: LiveEvent+ try {+ eventData = JSON.parse(event.data)+ } catch {+ // Drop malformed stream payloads rather than throwing inside the listener+ return+ }+ cache.batch.push(eventData)+ if (cache.batch.length >= 10 || performance.now() - (values.lastBatchTimestamp || 0) > 300) {+ actions.addEvents(cache.batch)+ cache.batch.length = 0+ }+ },+ onError: (error) => {+ if (!cache.hasShownLiveStreamErrorToast && props.showLiveStreamErrorToast) {+ console.error('Failed to poll events. You likely have no events coming in.', error)+ lemonToast.error(`No live events found. Continuing to retry in the background…`, {+ icon: <Spinner />,+ toastId: ERROR_TOAST_ID,+ autoClose: false,+ })+ cache.hasShownLiveStreamErrorToast = true+ }+ },+ })+ return () => controller.abort()+ }, 'eventsConnection')},pauseStream: () => {- if (cache.eventSourceController) {- cache.eventSourceController.abort()- }+ cache.disposables.dispose('eventsConnection')},resumeStream: () => {actions.updateEventsConnection()@@ -205,14 +206,9 @@ export const liveEventsLogic = kea<liveEventsLogicType>([}},})),- events(({ actions, cache }) => ({+ events(({ actions }) => ({afterMount: () => {actions.updateEventsConnection()},- beforeUnmount: () => {- if (cache.eventSourceController) {- cache.eventSourceController.abort()- }- },})),])diff --git a/frontend/src/scenes/max/maxLogic.tsx b/frontend/src/scenes/max/maxLogic.tsxindex c51b18d57815..baa866d50516 100644--- a/frontend/src/scenes/max/maxLogic.tsx+++ b/frontend/src/scenes/max/maxLogic.tsx@@ -12,7 +12,6 @@ import { tabUiStateLogic } from 'lib/logic/tabUiStateLogic'import { objectsEqual, uuid } from 'lib/utils'import { sceneLogic } from 'scenes/sceneLogic'import { Scene, SceneTab } from 'scenes/sceneTypes'-import { maxSettingsLogic } from 'scenes/settings/environment/maxSettingsLogic'import { urls } from 'scenes/urls'import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'@@ -164,8 +163,6 @@ export const maxLogic = kea<maxLogicType>(['conversationHistory','conversationHistoryLoading',],- maxSettingsLogic,- ['coreMemory'],// Actions are lazy-loaded. In order to display their names in the UI, we're loading them here.actionsModel({ params: 'include_count=1' }),['actions'],