fix(useAsyncEffect): call cleanup when component unmounts before async effect resolves #351
Open
eunwoo-levi wants to merge 3 commits intotoss:mainfrom
Open
Conversation
… resolves The cleanup function returned from the async effect was silently lost if the component unmounted before the Promise resolved. Added an `isCleaned` flag so the cleanup is invoked immediately after the Promise settles in that case. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Covers the case where the component unmounts before the async effect's Promise resolves — the previously missing scenario that allowed the race condition bug to go undetected. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes useAsyncEffect so that a cleanup function returned from an async effect is still executed even if the component unmounts (or deps change) before the promise resolves.
Changes:
- Track an
isCleanedflag to ensure late-resolving cleanup functions are invoked after unmount. - Add a regression test covering “unmount before promise resolves”.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/core/src/hooks/useAsyncEffect/useAsyncEffect.ts | Ensures async-provided cleanup still runs if unmount happens before the promise settles. |
| packages/core/src/hooks/useAsyncEffect/useAsyncEffect.spec.ts | Adds a regression test for early unmount with delayed async resolution. |
| @@ -24,12 +24,17 @@ import { DependencyList, useEffect } from 'react'; | |||
| export function useAsyncEffect(effect: () => Promise<void | (() => void)>, deps?: DependencyList) { | |||
| useEffect(() => { | |||
Comment on lines
29
to
34
| effect().then(result => { | ||
| cleanup = result; | ||
| if (isCleaned) { | ||
| cleanup?.(); | ||
| } | ||
| }); |
Comment on lines
24
to
25
| export function useAsyncEffect(effect: () => Promise<void | (() => void)>, deps?: DependencyList) { | ||
| useEffect(() => { |
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.
Overview
useAsyncEffectdrops the cleanup function when the component unmounts before thePromiseresolves.Root Cause
cleanupis assigned inside.then(), but React's cleanup runs synchronously on unmount.If unmount fires before the
Promiseresolves,cleanupis stillundefined— the user's cleanup function is permanently lost.Fix
Added an
isCleanedflag. If unmount fires before the Promise resolves, the cleanup is called immediately after the Promise settles.Changes
Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?