unhead: Memory Leak

Environment

  • Operating System: Linux
  • Node Version: v20.5.1
  • Nuxt Version: 3.8.2
  • CLI Version: 3.10.0
  • Nitro Version: 2.8.0
  • Package Manager: npm@9.8.0
  • Builder: -
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

image

When i crawled our web, crashes by memory leak

Describe the bug

image

When i crawled our web, crashes by memory leak

Additional context

No response

Logs

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 24 (4 by maintainers)

Most upvoted comments

This should be solved in v2 which is now in alpha. A Nuxt PR is open and will merge in the coming days / weeks. There’s a migration guide here: https://unhead-unjs-io.nuxt.dev/docs/migration

Managed to trigger the memory leak and 100% concisely. I am doing some workaround here in Nuxt to dodge Nuxt dedupe bcs my object is quite quite large. For the info it was working for some time and didn’t cause any memory leaks but i buffed the deps to latest and now it’s exploding. I am using huge payloads od 300kb so as you know the server fails in seconds. Also what is “interesting” is that the memory leak is easily produceable when you hit the server with couple of routes at once. When you just do ab -n 1000 https://localhost:3000/ you don’t really see much except one global unhead entry. When you do simultaneous request the issue really bears its head (Sorry had to)

I think basically what happens after Vue SSR suspense never gets resolved. Just my theory

export const useBigDataFetch = <T extends JSONValue>(
  path: string | (() => string),
  _args?: UseFetchOptions<T>,
) => {
  const url = typeof path === 'string' ? path : path()
  const key = url.replaceAll('/', '_')

  const rawData = useState(key, () => shallowRef(new JSONPayload<T>(key))) // shallow by default

  const error = ref<unknown>()

  const { locale } = useFetchOptions()

  const execute = async (force?: boolean) => {
    if (!force && rawData.value.payload) return
    try {
      const nuxt = useNuxtApp()

      const res = await $casinoInternalFetch<T>(url)
      const data = new JSONPayload<T>(key, res)

      nuxt.runWithContext(() => {
        // embed in html directly
        useServerHead({
          script: [
            {
              tagPosition: 'bodyClose', // near rest of nuxt data
              innerHTML: `window.value.key =` + data.raw,
            },
          ],
        })
      })

      rawData.value = data
    } catch (e) {
      error.value = e
    }
  }

  const data = computed(() => rawData.value.payload)

  watch(locale, () => execute(true))

  return {
    data,
    error,
    execute,
  }
}

I can workaround around this issue easily but still this is the common use case of How not to use useHead that works from version to version.

Just ping me if you need any more data on the issue but schema, nuxt, vue, unhead are used in unhoisted pnpm monorepo.

PS. as a user I would expect the default behavior of what I am doing here to “not work” in non failing way: “produce a lot of undefineds” but I would not like to produce a memory leak.

To anyone experiencing this issue, I would ask that you check in your package-lock.json or yarn.lock that your vue is up to date, (it might not be because of a bug in yarn that does not update sub deps when targeting update of a single package so if only nuxt is a direct dep and is targeted for upgrade, vue will not upgrade)

Please check that you are at least on 3.5.7 which fixes 3 memory leak issues, and if you are not, then please try again after upgrading and report back https://github.com/vuejs/core/blob/main/CHANGELOG.md#357-2024-09-20

I investigated too 😭 😂 I have one or two other theories on suspect code, maybe we can have a look together in the coming days @harlan-zw or @BobbieGoede? If you have time next week, we could meet on Discord to stop the leak 🧹

image I am too having problems with __unhead_injection_handler__ retaining a lot of stuff. (Nuxt v3.13.2, both trying to use i18n and i18n-micro).

BTW, while I was searching for what could be leaking memory in the i18n module I often saw this pattern (Symbol(__DEV__ ? 'LIB_KEY_HERE' : '') which obscures/removes the identifying global key in a production build.

I guess the primary reason for doing this is to reduce the build size, but a side effect is that we don’t see these keys when debugging memory (which I have only done on a production build). Since unhead does not use this pattern, and it uses some memory, it will be one of the first things you’ll see when debugging which doesn’t necessarily mean that it is leaking.

I can explain better than the opener of the bug. He is right. I was wasted a day to debug a similar bug in my code, so I can explain better.

The leak is in the $vm._ of vue, persisted in node memory by the global __unhead_injection_handler__ and its friends, so after the SSR page go down, the memory is not freed and stayed in memory again and again.

image

@Xenossolitarius Does it make any difference if you move const nuxt = useNuxtApp() outside the execute function? Generally composables should only be called from the main body of a composable or setup function.

I did some testing with clinic.js and was not able to replicate any sort of memory leak.

image

This is with ~500 connections doing ~5k requests x2 on a Nuxt app.

Will leave this issue open in case anyone has any further research they can share. (I tried testing with other clinic.js tools but it was failing).

Looks like a vue-router stack? $head is a singleton on the vue VM (like globalProperties), so not sure what you’re showing here exactly.

Not to say there isn’t an issue, but this doesn’t help me identify anything from Unhead’s side.

Also, see comment above.

This is something I do want to take a deeper look into in any case but I’m currently limited with time so unless more people are reporting memory leak issues with Nuxt / Unhead then it’s a low priority as it’s going to be a lengthy investigation.

Thank you for your investigation @BobbieGoede! Some great work on @nuxtjs/i18n 👏