Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Promise race condition #2220

Merged
merged 2 commits into from
Jul 12, 2023
Merged

Fixing Promise race condition #2220

merged 2 commits into from
Jul 12, 2023

Conversation

mattgperry
Copy link
Collaborator

@mattgperry mattgperry requested a review from shuangq July 10, 2023 13:25
@@ -181,7 +183,7 @@ export const AnimatePresence: React.FunctionComponent<
const insertionIndex = presentKeys.indexOf(key)

let exitingComponent = component
if (!exitingComponent) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is just Prettier

@@ -65,7 +65,6 @@ export function animateValue<V = number>({
* WAAPI-compatible behaviour.
*/
const updateFinishedPromise = () => {
resolveFinishedPromise && resolveFinishedPromise()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above needs to be updated as well.

@@ -280,7 +280,7 @@ export function animateValue<V = number>({
playState = "finished"
onComplete && onComplete()
stopAnimationDriver()
updateFinishedPromise()
resolveFinishedPromise()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate a little more on the race condition? Why do we stop updating the finished promise in finish()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reasons slightly beyond my comprehension, but at least related to adding queueMicrotasks (which is when Promises are resolved), swapping to a new Promise as soon as we resolve the old one messes with Promise.all - seemingly at random. As noted in Onne's linked ticket, even adding a console.log can affect timings in a way that more consistently fixes the observed bug. So instead we swap to a new Promise later - if and when we start a new animation.

Copy link
Contributor

@shuangq shuangq Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swapping to a new Promise as soon as we resolve the old one messes with Promise.all

Would it have the same impact when cancel() is called?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't see a bug related to this but it's added there because it's been removed from updateFinishedPromise()

Copy link
Contributor

@shuangq shuangq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment why we skip it in finish(), I think it requires context to understand why the code is written this way.

@mattgperry mattgperry added the automerge Land this PR label Jul 12, 2023
@mergetron mergetron bot removed the autorebasing label Jul 12, 2023
@mergetron mergetron bot merged commit 5f9c48f into main Jul 12, 2023
1 check passed
@mergetron mergetron bot deleted the fix/exit branch July 12, 2023 11:41
@pofixifop
Copy link

i still have issues with exiting components in quick succession getting stuck, and never getting to the proper final component, unless i comment out all exit props

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Land this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] layoutId and AnimatePresence together fail to unmount content
3 participants