Fix memory leak in tooltip children cleanup #1258
Draft
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.
Description
Fixes #1476 - Memory leak where React children rendered in tooltips were not being properly cleaned up, causing DOM nodes to accumulate when tooltips were shown and hidden repeatedly.
Root Cause
The issue was caused by three sources of memory leaks in the
Tooltip
component:ResizeObserver timeout accumulation: The ResizeObserver callback created multiple
setTimeout
calls, but only the last timeout ID was tracked. When tooltips were rapidly shown/hidden, older timeouts continued running, holding references to DOM nodes and closures.Untracked handleShow timeouts: The
handleShow
function created a 10mssetTimeout
on every call without tracking or cleanup. Rapid show/hide cycles accumulated multiple timeouts.Async promise state updates: The
computeTooltipPosition
promise could resolve after component unmount, attempting state updates on unmounted components.Changes
All changes are in
src/components/Tooltip/Tooltip.tsx
:ResizeObserver Effect
Set
instead of a single variablerendered
state before creating observermounted.current
check before callingupdateTooltipPosition
content
from dependencies (was causing unnecessary effect re-runs)handleShow Timeout Tracking
tooltipShowTimerRef
to track the setTimeoutPosition Calculation Promises
mounted.current
check inhandleTooltipPosition
promise callbackImpact
These changes ensure:
Testing
To verify the fix, rapidly hover on and off a tooltip trigger element multiple times. Browser DevTools should show:
Example test case:
The fix is backward compatible and doesn't change any public APIs or behavior.
Original prompt
Fixes #1242
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.