-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: Address multiple memory leaks in CodeBlock component #4244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Address multiple memory leaks in CodeBlock component #4244
Conversation
…247, CodeBlock_459, CodeBlock_694)
daniel-lxs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @kiwina, Thank you for your contribution,
I left a couple of comments about code organization, nothing major.
Thank you for taking the time to cleanup the memory leaks, let me know what you think!
| return () => { | ||
| isMountedRef.current = false | ||
| } | ||
| }, []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific reason for having a dedicated useEffect just for managing isMountedRef? This could potentially be integrated into the existing syntax highlighting effect since they have the same lifecycle. Just curious about the design choice here!
| clearTimeout(collapseTimeout2Ref.current) | ||
| } | ||
| } | ||
| }, []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that the cleanup pattern for timeouts is a bit inconsistent. The buttonPositionTimeoutRef is cleaned up in the same effect that sets it (lines 521-526), while the collapse timeouts have a separate cleanup effect here.
Could we consider consolidating these patterns? Having a consistent approach would improve maintainability. What do you think about either:
- Moving all timeout cleanups to their respective effects, or
- Having a single cleanup effect for all timeouts?
Both approaches would work, just wondering if there's a preference!
- Consolidate isMountedRef management into syntax highlighting useEffect - Unify timeout cleanup patterns for consistent maintainability - Maintain all existing memory leak protections - Improve code organization and readability Addresses review comments from daniel-lxs in PR RooCodeInc#4244
daniel-lxs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied the requested changes.
LGTM
* fix: address multiple memory leaks in CodeBlock component (CodeBlock_247, CodeBlock_459, CodeBlock_694) * fix: Address review feedback for CodeBlock memory leak fixes - Consolidate isMountedRef management into syntax highlighting useEffect - Unify timeout cleanup patterns for consistent maintainability - Maintain all existing memory leak protections - Improve code organization and readability Addresses review comments from daniel-lxs in PR #4244 --------- Co-authored-by: Daniel Riccio <[email protected]>
…#4244) * fix: address multiple memory leaks in CodeBlock component (CodeBlock_247, CodeBlock_459, CodeBlock_694) * fix: Address review feedback for CodeBlock memory leak fixes - Consolidate isMountedRef management into syntax highlighting useEffect - Unify timeout cleanup patterns for consistent maintainability - Maintain all existing memory leak protections - Improve code organization and readability Addresses review comments from daniel-lxs in PR RooCodeInc#4244 --------- Co-authored-by: Daniel Riccio <[email protected]>
PR Type
Description
This PR addresses multiple potential memory leaks in the
CodeBlockcomponent located atwebview-ui/src/components/common/CodeBlock.tsxrelated to unmanaged asynchronous operations andsetTimeoutcalls.Specifically, it resolves issues where:
useEffecthook for syntax highlighting (original line 247) could callsetHighlightedCodeon an unmounted component.useEffecthook (original line 456) usingsetTimeout(original line 459) to update button positions did not clear the timeout on unmount or re-render.setTimeoutcalls in the collapse/expand button'sonClickhandler (original lines 694, 699) were not cleared on unmount.The fix implements the following patterns:
isMountedRefis used to track the component's mounted state, preventing state updates on unmounted components for async operations.setTimeoutIDs are stored in refs (buttonPositionTimeoutRef,collapseTimeout1Ref,collapseTimeout2Ref).useEffectcleanup functions to ensure they don't execute after the component unmounts.codeBlockRef.currentare added before DOM manipulations within timeout callbacks.These changes ensure that state updates are not attempted on unmounted components and that timeouts are properly managed, resolving potential memory leaks and eliminating associated React warnings.
Related Issue
#4243
Changes
webview-ui/src/components/common/CodeBlock.tsxto include mounted state checks and proper cleanup for asynchronous operations andsetTimeoutcalls.Testing
Manual testing:
getHighlighter,codeToHtml, or withinsetTimeoutcallbacks to simulate longer async operations.CodeBlockcomponent.CodeBlockcomponent (e.g., by removing its parent, navigating away, or rapidly clicking the collapse/expand button).Important
Fixes memory leaks in
CodeBlockcomponent by managing async operations andsetTimeoutcalls with proper cleanup and mount state checks.CodeBlockcomponent by managing async operations andsetTimeoutcalls.isMountedRefto prevent state updates on unmounted components.setTimeoutIDs stored in refs (buttonPositionTimeoutRef,collapseTimeout1Ref,collapseTimeout2Ref) on unmount.codeBlockRef.currentbefore DOM manipulations in timeout callbacks.useEffecthooks to include cleanup functions for timeouts.highlight()function to checkisMountedRefbefore setting state.onClickhandler for collapse/expand button to clear nested timeouts.CodeBlockto check for React warnings.This description was created by
for 4313d4c. You can customize this summary. It will automatically update as commits are pushed.