From 3f3825a219af741f20f71e0ead3d6d6eb836a79d Mon Sep 17 00:00:00 2001 From: kiwina Date: Thu, 5 Jun 2025 05:34:34 +0800 Subject: [PATCH] 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) --- webview-ui/src/components/chat/ChatView.tsx | 29 ++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/webview-ui/src/components/chat/ChatView.tsx b/webview-ui/src/components/chat/ChatView.tsx index d114701e18..52a3026e8a 100644 --- a/webview-ui/src/components/chat/ChatView.tsx +++ b/webview-ui/src/components/chat/ChatView.tsx @@ -60,6 +60,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction { + const isMountedRef = useRef(true) const [audioBaseUri] = useState(() => { const w = window as any return w.AUDIO_BASE_URI || "" @@ -158,6 +159,13 @@ const ChatViewComponent: React.ForwardRefRenderFunction { + isMountedRef.current = true + return () => { + isMountedRef.current = false + } + }, []) + const isProfileDisabled = useMemo( () => !!apiConfiguration && !ProfileValidator.isProfileAllowed(apiConfiguration, organizationAllowList), [apiConfiguration, organizationAllowList], @@ -1109,10 +1117,14 @@ const ChatViewComponent: React.ForwardRefRenderFunction { + let timerId: NodeJS.Timeout | undefined if (!disableAutoScrollRef.current) { - setTimeout(() => scrollToBottomSmooth(), 50) - // Don't cleanup since if visibleMessages.length changes it cancels. - // return () => clearTimeout(timer) + timerId = setTimeout(() => scrollToBottomSmooth(), 50) + } + return () => { + if (timerId) { + clearTimeout(timerId) + } } }, [groupedMessages.length, scrollToBottomSmooth]) @@ -1234,6 +1246,9 @@ const ChatViewComponent: React.ForwardRefRenderFunction setTimeout(resolve, writeDelayMs)) + if (!isMountedRef.current) { + return + } } vscode.postMessage({ type: "askResponse", askResponse: "yesButtonClicked" }) @@ -1241,9 +1256,11 @@ const ChatViewComponent: React.ForwardRefRenderFunction