fix(frontend): prevent memory leak in useIdempotentAction.ts (#1221) - #1293
Merged
github-actions[bot] merged 1 commit intoJul 27, 2026
Conversation
…IdempotentAction.ts (leojay-net#1221) Root cause: the useEffect cleanup only set isMountedRef.current = false. The inFlightActions Map (Map<string, Promise<unknown>>) was never cleared, so every Promise stored in it — together with the closures they captured (state setters, refs, hook internals) — was retained in memory until the Promise settled, even after the component unmounted. Fix: call inFlightActions.current.clear() in the effect cleanup so in-flight Promise references are released on unmount. Regression test added: starts an action, unmounts while the Promise is still pending, then resolves it — verifies no stale-state update occurs and that a fresh hook instance behaves normally.
|
@Mercy017 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Root cause
useIdempotentAction'suseEffectcleanup only setisMountedRef.current = false. TheinFlightActionsref (Map<string, Promise<unknown>>) was never cleared on unmount.Any Promise stored in that Map — together with the closures it captured (state setters,
isProcessingRef,lastExecutionTime, etc.) — was retained in memory until the Promise settled. On high-churn routes where the hook's component mounts and unmounts frequently, these retained closures accumulate and prevent GC of the entire component subtree.Fix (
src/hooks/useIdempotentAction.ts)Added
inFlightActions.current.clear()to theuseEffectcleanup so in-flight Promise references are released the moment the component unmounts, allowing the GC to reclaim them.return () => { isMountedRef.current = false; + inFlightActions.current.clear(); };Regression test (
src/hooks/__tests__/useIdempotentAction.test.ts)New test
"regression: clears inFlightActions on unmount …":inFlightActions.Closes #1221
Closes #1220