fix: Prevent memory leaks in useResumableSSE by clearing handler maps on stream completion and cleanup

This commit is contained in:
Danny Avila 2025-12-12 01:15:25 -05:00
parent 1b2d3f30ef
commit 08052844cf
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956

View file

@ -151,6 +151,8 @@ export default function useResumableSSE(
setIsSubmitting(false); setIsSubmitting(false);
setShowStopButton(false); setShowStopButton(false);
} }
// Clear handler maps on stream completion to prevent memory leaks
clearStepMaps();
(startupConfig?.balance?.enabled ?? false) && balanceQuery.refetch(); (startupConfig?.balance?.enabled ?? false) && balanceQuery.refetch();
sse.close(); sse.close();
setStreamId(null); setStreamId(null);
@ -369,6 +371,7 @@ export default function useResumableSSE(
contentHandler, contentHandler,
resetContentHandler, resetContentHandler,
syncStepMessage, syncStepMessage,
clearStepMaps,
messageHandler, messageHandler,
errorHandler, errorHandler,
setIsSubmitting, setIsSubmitting,
@ -476,6 +479,8 @@ export default function useResumableSSE(
sseRef.current.close(); sseRef.current.close();
sseRef.current = null; sseRef.current = null;
} }
// Clear handler maps to prevent memory leaks and stale state
clearStepMaps();
// Reset UI state on cleanup - useResumeOnLoad will restore if needed // Reset UI state on cleanup - useResumeOnLoad will restore if needed
setIsSubmitting(false); setIsSubmitting(false);
setShowStopButton(false); setShowStopButton(false);