fix(frontend): prevent memory leak from uncleaned storage listener in useFeatureFlag - #1295
Merged
github-actions[bot] merged 1 commit intoJul 27, 2026
Conversation
…eojay-net#1220) useFeatureFlag added a window 'storage' listener on each render (when flag or scrollTargetId changed) but never returned a cleanup function to remove it. Every re-render that changed those deps leaked an additional listener, causing the evaluate function closure to be retained in memory indefinitely. Fix: wrap the window.addEventListener call in a cleanup-returning block so React removes the listener whenever the effect re-runs or the component unmounts. Also extracts evaluate() as a named function so the exact same reference is passed to both addEventListener and removeEventListener (required for correct deregistration). The storage listener also adds runtime reactivity: flag state is now refreshed when another tab or a devtools script modifies localStorage. Regression tests added in hooks/__tests__/useFeatureFlag.test.ts: - Verifies the storage listener is removed on unmount - Verifies re-evaluation fires when a storage event is dispatched Closes leojay-net#1220
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
useFeatureFlagcalledwindow.addEventListener('storage', evaluate)insideuseIsomorphicLayoutEffectbut never returned a cleanup function. Each timeflagorscrollTargetIdchanged, React re-ran the effect — adding a new listener without removing the previous one. Afternre-renders,norphaned listener references accumulated inwindow._eventListeners, each retaining theevaluateclosure (and with itflag,scrollTargetId,setIsEnabled, etc.) in memory.Fix (
src/hooks/useFeatureFlag.ts)evaluate()function so the same reference is passed to bothaddEventListenerandremoveEventListener(required for correct deregistration).() => window.removeEventListener('storage', evaluate)from the effect so React cleans up on unmount and before every re-run triggered by a dep change.The
storagelistener also makes flag state reactive: toggling a flag via devtools or another tab now updates the component without a page reload.Regression tests (
src/hooks/__tests__/useFeatureFlag.test.ts)removes storage listener on unmount so it does not leak— spies onwindow.addEventListener/removeEventListenerand asserts the exact handler registered is also removed when the hook unmounts.re-evaluates the flag when a storage event fires— dispatches aStorageEventafter changing the mock return value and asserts the hook state updates.Closes #1220