Skip to content

Clear event listeners and timers before component unmounts - #3991

Merged
kikuomax merged 1 commit into
buefy:devfrom
Arooba-git:fix-memory-leak
Jan 18, 2024
Merged

kikuomax merged 1 commit into
buefy:devfrom
Arooba-git:fix-memory-leak

Conversation

@Arooba-git

Copy link
Copy Markdown
Contributor

Hi 👋 , thank you for this library!

We found that some components used event listeners and timing events, but did not remove them before unmounting. This means an accumulation of listeners and timers each time a component is mounted, on top of the previous ones, leading to a memory leak. This PR fixes this issue :)

@netlify

netlify Bot commented Jan 15, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for buefy-org ready!

Name Link
🔨 Latest commit 36725d0
🔍 Latest deploy log https://app.netlify.com/sites/buefy-org/deploys/65a940b780a1650008d02f14
😎 Deploy Preview https://deploy-preview-3991--buefy-org.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@wesdevpro

Copy link
Copy Markdown
Member

@Arooba-git thanks for identifying this problem and fixing it. @kikuomax and I will review these changes as soon as possible. Thanks again!

@wesdevpro wesdevpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@kikuomax all these changes make sense to me. However, I have not tested for any memory leaks and or if there are no longer any memory leaks. Is there a way for us to test that?

Comment thread src/components/carousel/Carousel.vue Outdated
},
beforeDestroy() {
this.pauseTimer()
clearInterval(this.timer)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Arooba-git This is unnecessary, because pauseTimer clears this.timer. Please see

pauseTimer() {
this.isPause = true
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oops I do see that now.

@wesdevpro
wesdevpro self-requested a review January 16, 2024 01:44
@Arooba-git

Arooba-git commented Jan 16, 2024 •

Copy link
Copy Markdown
Contributor Author

. Is there a way for us to test that?

@kikuomax Good that you pointed that out. Yes, actually we use Memlab to gauge heap size and memory leaks in applications. Memlab requires us to write a scenario file as mentioned in their documentation. I created a scenario file for this project as well. Sadly currently memlab has a limitation of a protocolTimeout error for some cases, this one falls into that. I have reported the issue, they are working to fix within this week. After that I will attach the memory footprint results before and after the leak fixes in this project.

Comment thread src/components/sidebar/Sidebar.vue Resolved
Comment thread src/components/slider/SliderThumb.vue Resolved
Comment thread docs/components/TheNavbar.vue Resolved
Comment thread docs/templates/Documentation.vue Resolved
@Arooba-git

Copy link
Copy Markdown
Contributor Author

I have reported the issue, they are working to fix within this week.

I heard back from Memlab developer regarding the timeout and page crash issue I was facing when executing their tool on the scenario file I wrote for Buefy.
Screen Shot 2024-01-18 at 1 22 13 PM

This is what their developer has to say:

My guess is that either 1) 1.4GB of memory is too much for Chrome to capture the heap snapshot (you could try taking the 1.4GB heap snapshot manually in Chrome DevTools to see if the Browser DevTools times out) or 2) the connection between Puppeteer and Chrome was not designed to handle such a large heap snapshot.

Moreover, if a web app uses more than 1GB JS heap, it has some serious memory issues. I would recommend testing on a scenario where the memory leak is less severe.

The code patterns fixed in this PR are some of the common sources of memory leaks, as per our research on Github and StackOverflow history. Nevertheless, if I manage to resolve the Memlab crash issue and provide concrete heap/leak reduction after these fixes, I will let you know as soon as is possible..

@kikuomax

Copy link
Copy Markdown
Collaborator

@Arooba-git What kind of scenario are you running? 1.4GB of heap usage by Buefy does not make sense to me. If just browsing Buefy docs used 1.4GB of heap, we should have gotten a lot of serious problem reports.

@kikuomax

Copy link
Copy Markdown
Collaborator

@Arooba-git By the way, could you update your PR if my comments make sense to you?

@Arooba-git

Copy link
Copy Markdown
Contributor Author

What kind of scenario are you running?

TBH It's a pretty normal scenario file that follows puppeteer syntax, similar to what I have been using for multiple other projects 🤔 . I have attached it here for reference:

test-scenario-buefy.js.zip

@Arooba-git

Arooba-git commented Jan 18, 2024 •

Copy link
Copy Markdown
Contributor Author

@Arooba-git By the way, could you update your PR if my comments make sense to you?

Sure, totally makes sense. I'll update it by today 👍

@kikuomax

Copy link
Copy Markdown
Collaborator

What kind of scenario are you running?

TBH It's a pretty normal scenario file that follows puppeteer syntax, similar to what I have been using for multiple other projects 🤔 . I have attached it here for reference:

test-scenario-buefy.js.zip

@Arooba-git Does the script suppose the Buefy docs app running at http://localhost:8080; i.e., running npm run dev?

@Arooba-git

Copy link
Copy Markdown
Contributor Author

Does the script suppose the Buefy docs app running at http://localhost:8080; i.e., running npm run dev?

Yes, we need to first run the app and pass the URL to Memlab's script accordingly. In my case (when I created this script), the app was running on the said address.

@Arooba-git

Arooba-git commented Jan 18, 2024 •

Copy link
Copy Markdown
Contributor Author

@Arooba-git By the way, could you update your PR if my comments make sense to you?

Updated 👍

@wesdevpro
wesdevpro requested a review from kikuomax January 18, 2024 18:41

@kikuomax kikuomax left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me!

@kikuomax

Copy link
Copy Markdown
Collaborator

@Arooba-git @wesdevpro I am going to merge this PR and close the conversation. Please update us in the new issue #3992.

@kikuomax
kikuomax merged commit ffa5c01 into buefy:dev Jan 18, 2024
kikuomax pushed a commit that referenced this pull request Feb 9, 2024
fix(lib): potential leak and call after unmount
- `NumberInput` clears the timeout on unmount to make sure it is not called after unmount
- `Sidebar` clears the timeout on unmount to make sure it is not called after unmount
- `SliderThumb` removes listeners in case they might be left in overlooked edge cases

fix(docs): dangling listeners
- `TheNabver` and `Documentation` removes listeners on unmount
kikuomax pushed a commit to kikuomax/buefy that referenced this pull request Feb 11, 2024
…y#3991)

fix(lib): potential leak and call after unmount
- `NumberInput` clears the timeout on unmount to make sure it is not called after unmount
- `Sidebar` clears the timeout on unmount to make sure it is not called after unmount
- `SliderThumb` removes listeners in case they might be left in overlooked edge cases

fix(docs): dangling listeners
- `TheNabver` and `Documentation` removes listeners on unmount

chore: migrate to Vue 3
- Replaces `beforeDestroy` with `beforeUnmount`
kikuomax pushed a commit to ntohq/buefy-next that referenced this pull request Feb 12, 2024
…y#3991)

fix(lib): potential leak and call after unmount
- `NumberInput` clears the timeout on unmount to make sure it is not called after unmount
- `Sidebar` clears the timeout on unmount to make sure it is not called after unmount
- `SliderThumb` removes listeners in case they might be left in overlooked edge cases

fix(docs): dangling listeners
- `TheNabver` and `Documentation` removes listeners on unmount

chore: migrate to Vue 3
- Replaces `beforeDestroy` with `beforeUnmount`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

No open projects
Status: Completed ✅

Development

Successfully merging this pull request may close these issues.

3 participants