fix: Address memory leaks in ChatView component (ChatView_1113, ChatView_1236) #4248
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.
PR Type
Description
This PR addresses multiple potential memory leaks in the
ChatViewcomponent (webview-ui/src/components/chat/ChatView.tsx) related to unmanaged asynchronous operations andsetTimeoutcalls.1. Auto-Scroll Timeout Leak (ChatView_1113):
useEffecthook (original line 1111) that schedules asetTimeoutforscrollToBottomSmooth()did not clear the timeout.useEffect's cleanup function.2. Auto-Approve Async Delay Leak (ChatView_1236):
autoApprovefunction (called byuseEffectat original line 939) uses anawait new Promise(setTimeout)forwriteDelayMs. Subsequent state updates could occur on an unmounted component.isMountedRefis added toChatViewComponentand checked before state updates withinautoApproveafter the delay. State updates are now grouped under a singleisMountedRef.currentcheck.These changes ensure that asynchronous operations and timeouts are properly managed, preventing state updates on unmounted components and resolving potential memory leaks.
Related Issue
#4247
Changes
webview-ui/src/components/chat/ChatView.tsx:setTimeout.isMountedRefand checks before state updates in theautoApprovefunction.Testing
For Auto-Scroll Leak (ChatView_1113):
disableAutoScrollRef.currentis false.groupedMessages.lengthor quickly unmountChatView(e.g., close the webview).scrollToBottomSmooth. With the fix, this should not occur.For Auto-Approve Leak (ChatView_1236):
autoApprovewith awriteDelayMs > 0(e.g., an auto-approved file write operation).ChatViewcomponent beforewriteDelayMselapses.setSendingDisabled,setClineAsk, orsetEnableButtonson an unmounted component. With the fix, these should not occur.Important
Fixes memory leaks in
ChatView.tsxby managing asynchronous operations and timeouts, ensuring no state updates on unmounted components.ChatView.tsxby managing asynchronous operations andsetTimeoutcalls.setTimeoutnow cleared inuseEffectcleanup.autoApprovefunction checksisMountedRefbefore state updates after delay.isMountedRefto track component mount status.isMountedRef.currentcheck inautoApprove.groupedMessageschanges.writeDelayMselapses.This description was created by
for 480bf87. You can customize this summary. It will automatically update as commits are pushed.