Skip to content

Commit 3f3825a

Browse files
authored
fix: Address memory leaks in ChatView component (ChatView_1113, ChatView_1236) (#4248)
* chore: Staging ChatView.tsx after reset to main * fix: Address memory leaks in ChatView (ChatView_1113, ChatView_1236)
1 parent 8cc8914 commit 3f3825a

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

webview-ui/src/components/chat/ChatView.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
6060
{ isHidden, showAnnouncement, hideAnnouncement },
6161
ref,
6262
) => {
63+
const isMountedRef = useRef(true)
6364
const [audioBaseUri] = useState(() => {
6465
const w = window as any
6566
return w.AUDIO_BASE_URI || ""
@@ -158,6 +159,13 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
158159
clineAskRef.current = clineAsk
159160
}, [clineAsk])
160161

162+
useEffect(() => {
163+
isMountedRef.current = true
164+
return () => {
165+
isMountedRef.current = false
166+
}
167+
}, [])
168+
161169
const isProfileDisabled = useMemo(
162170
() => !!apiConfiguration && !ProfileValidator.isProfileAllowed(apiConfiguration, organizationAllowList),
163171
[apiConfiguration, organizationAllowList],
@@ -1109,10 +1117,14 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
11091117
)
11101118

11111119
useEffect(() => {
1120+
let timerId: NodeJS.Timeout | undefined
11121121
if (!disableAutoScrollRef.current) {
1113-
setTimeout(() => scrollToBottomSmooth(), 50)
1114-
// Don't cleanup since if visibleMessages.length changes it cancels.
1115-
// return () => clearTimeout(timer)
1122+
timerId = setTimeout(() => scrollToBottomSmooth(), 50)
1123+
}
1124+
return () => {
1125+
if (timerId) {
1126+
clearTimeout(timerId)
1127+
}
11161128
}
11171129
}, [groupedMessages.length, scrollToBottomSmooth])
11181130

@@ -1234,16 +1246,21 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
12341246
// Add delay for write operations.
12351247
if (lastMessage.ask === "tool" && isWriteToolAction(lastMessage)) {
12361248
await new Promise((resolve) => setTimeout(resolve, writeDelayMs))
1249+
if (!isMountedRef.current) {
1250+
return
1251+
}
12371252
}
12381253

12391254
vscode.postMessage({ type: "askResponse", askResponse: "yesButtonClicked" })
12401255

12411256
// This is copied from `handlePrimaryButtonClick`, which we used
12421257
// to call from `autoApprove`. I'm not sure how many of these
12431258
// things are actually needed.
1244-
setSendingDisabled(true)
1245-
setClineAsk(undefined)
1246-
setEnableButtons(false)
1259+
if (isMountedRef.current) {
1260+
setSendingDisabled(true)
1261+
setClineAsk(undefined)
1262+
setEnableButtons(false)
1263+
}
12471264
}
12481265
}
12491266
autoApprove()

0 commit comments

Comments
 (0)