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

fix(nuxt): global pinia instance leads to whole nuxt context not being garbage collected #2915

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from

Conversation

MirkoJa
Copy link

@MirkoJa MirkoJa commented Feb 15, 2025

I discovered this during testing of a memory leak in our nuxt application. We were providing our apollo client in a plugin as well and it would never be garbage collected.

Relatively simple to test.

  • setup a nuxt project with pinia (not even a store necessary).
  • build the project
  • run it with
node --inspect --expose-gc  .output/server/index.mjs
  • use e.g. chrome to take a node heap snapshot
  • with pinia the third object there will appear -> without pinia i doesn't appear as it is garbace collected

Reason:
The reason behind it is, that calling setActivePinia(pinia) writes it in a global variable, which is not cleared after ssr is done -> the same reference is passed to the nuxt plugin provide return. Keeping the activePinia instance leads to the whole nuxt context not being garbage collected.

grafik
In the Screenshot you can see that the activePinia keeps everything alive even after ssr is done.

Sadly I have no idea how to write a test for that.

(This is my first open source contribution so i might have no idea what i am doing :D)

Copy link

netlify bot commented Feb 15, 2025

Deploy Preview for pinia-official canceled.

Name Link
🔨 Latest commit 83e626a
🔍 Latest deploy log https://app.netlify.com/sites/pinia-official/deploys/67b086942345070008e213b0

@posva
Copy link
Member

posva commented Feb 17, 2025

Nice find, thank you! In practice, this shouldn't matter since there is always 1 instance of pinia, so it won't create a memory leak, only holding of the memory. That being said, unsetting the global pinia instance on server after the app is sent seems reasonable!

@MirkoJa
Copy link
Author

MirkoJa commented Feb 17, 2025

The only thing i currently don't understand is if it makes any difference to link the reference to the nuxt payload when initializing the plugin. like this nuxtApp.payload.pinia = toRaw(pinia.state.value) vs. doing it in the app:rendered hook.

@posva
Copy link
Member

posva commented Feb 17, 2025

It shouldn't make a difference but doing it in app:rendered could be better.

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

Successfully merging this pull request may close these issues.

2 participants