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

[ResizeSensor] cancelAnimationFrame for reset when detach is called to prevent infinite loop and memory leak #282

Merged
merged 1 commit into from
Nov 22, 2019

Conversation

gaoruhao
Copy link
Contributor

In ResizeSensor, we use requestAnimationFrame for reset. However, if an element is added to DOM and removed from DOM very quickly (even before the first reset happens), the reset itself will stuck in an infinite loop. It also causes memory leak because the reset function holds the element forever.

In ResizeSensor, we use requestAnimationFrame for reset. However, if an element is added to DOM and removed from DOM very quickly (even before the first reset happens), the reset itself will stuck in an infinite loop. It also causes memory leak because the reset function holds the element forever.
@marcj
Copy link
Owner

marcj commented Nov 22, 2019

Good catch, thanks!

@marcj marcj merged commit 1e40612 into marcj:master Nov 22, 2019
@gaoruhao
Copy link
Contributor Author

@marcj Would you please do a minor version update on NPM? The tests of my project are very unstable due to the bug. I am replying on this fix to save the whole tests.

@marcj
Copy link
Owner

marcj commented Nov 22, 2019

Sure, v1.2.2 is released.

@gaoruhao
Copy link
Contributor Author

Sure, v1.2.2 is released.

Awesome, it saves my life. Thank you. @marcj

@mKlus
Copy link

mKlus commented Dec 5, 2019

@marcj & @gaoruhao Version 1.2.2 broke our website resizing.
I haven't had chance to debug the issue, but when I reviewed the changes between 1.2.1 and 1.2.2 the below code seems to be incorrect to me.

// clean up the unfinished animation frame to prevent a potential endless requestAnimationFrame of reset
if (!lastAnimationFrame) {
  window.cancelAnimationFrame(lastAnimationFrame);
  lastAnimationFrame = 0;
}

Shouldn't it instead be (notice the if (lastAnimationFrame))?

// clean up the unfinished animation frame to prevent a potential endless requestAnimationFrame of reset
if (lastAnimationFrame) {
  window.cancelAnimationFrame(lastAnimationFrame);
  lastAnimationFrame = 0;
}

@marcj
Copy link
Owner

marcj commented Dec 5, 2019

Indeed, that's incorrect, good catch! But it shouldn't break anything since its additional code that due to the bug isn't executed at all. What errors do you get?

@mKlus
Copy link

mKlus commented Dec 6, 2019

@marcj We don't get script error but with this change, the ResizeSensor handler won't get called. Our html5 app is quite simple so I'd expect this problem to be for most of the people.

            this.handleResize(size);
        });

I had a quick look and narrowed the issue down to the below change:
image

The lastAnimationFrame variable is used in reset function so something must have gone wrong there?

@mKlus
Copy link

mKlus commented Dec 15, 2019

@marcj Should I raise new issue for the above?

@mathias999us
Copy link

I am also having the same issue as mKlus with v1.2.2.

marcj added a commit that referenced this pull request Jan 3, 2020
marcj added a commit that referenced this pull request Jan 3, 2020
@marcj
Copy link
Owner

marcj commented Jan 3, 2020

@mathias999us @mKlus Fixed in v1.2.3

@Acinho
Copy link
Contributor

Acinho commented Feb 27, 2020

I am still having this issue with reset getting into infinite loop.
This fixes the issue: #289

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

Successfully merging this pull request may close these issues.

5 participants