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

Memory leak on Nuxt SSR #14389

Open
tilenpirih opened this issue Oct 7, 2024 · 4 comments
Open

Memory leak on Nuxt SSR #14389

tilenpirih opened this issue Oct 7, 2024 · 4 comments
Labels
bug Something isn't working memory leak node.js Compatibility with Node.js APIs

Comments

@tilenpirih
Copy link

What version of Bun is running?

1.1.29

What platform is your computer?

windows WSL - docker

What steps can reproduce the bug?

  1. Create empty nuxt project. You can also clone my repository of a minimal reproduction.
  2. Create dockerfile
FROM oven/bun:1.1.29 AS base

WORKDIR /usr/src/app
FROM base AS build
ENV NODE_ENV=production

COPY . /usr/src/app
WORKDIR /usr/src/app
RUN bun install
RUN bun run build

FROM base AS app

COPY --from=build /usr/src/app/.output /prod/app
WORKDIR /prod/app

EXPOSE 3000

CMD [ "bun", "run", "server/index.mjs" ]
  1. Run project in the docker
    docker build -t memoryLeak .
    and
    docker run -p 3000:3000 memoryLeak

  2. Visit the page multiple times. You can use a tool like OHA to make a lot of requests. And run a command
    oha -c 2 -n 10000 --disable-keepalive http://localhost:3000

  3. in the other console type
    docker stats [container name]
    You can see memory usage
    Image
    after 10000 requests
    Image
    after 100000 requests
    Image

What is the expected behavior?

If you edit dockerfile to run app with a Node instead of bun

FROM node:20-alpine AS base
WORKDIR /usr/src/app
FROM base AS build
ENV NODE_ENV=production
COPY . /usr/src/app
RUN npm install
RUN npm run build

FROM base AS app
COPY --from=build /usr/src/app/.output /prod/app
WORKDIR /prod/app

EXPOSE 3000
CMD [ "node", "server/index.mjs" ]

Memory usage in node after 100000 requests
Image

The difference is obvious.

What do you see instead?

No response

Additional information

In package.json in scripts you can put
"debug:bun": "bun run build && bun --inspect .output/server/index.mjs"

and when you run bun run debug:bun in console you will see a URL for inspection. It could help with debugging the process, creating snapshots, etc...

@tilenpirih tilenpirih added bug Something isn't working needs triage labels Oct 7, 2024
@tilenpirih tilenpirih changed the title Memory leak on SSR Nuxt Memory leak on Nuxt SSR Oct 7, 2024
@Jarred-Sumner
Copy link
Collaborator

My guess is the socket object in our node:http imlementation needs to do some additional cleanup work that it is not currently doing.

In #14384:

 protectedObjectTypeCounts: {
    Promise: 20001,
    UnlinkedProgramCodeBlock: 32,
    UnlinkedModuleProgramCodeBlock: 26,
    Function: 9,
    DebugHTTPServer: 1,
    GlobalObject: 1,
    Timeout: 1,
  },

In main:

{
  objectTypeCounts: {
    Object: 575954,
    Function: 436286,
    Array: 361073,
    string: 232655,
    JSLexicalEnvironment: 173841,
    BufferList: 40003,
    GetterSetter: 28880,
    Promise: 24955,
    "Immutable Butterfly": 23656,
    Headers: 20002,
    Request: 20001,
    Response: 20001,
    ProxyObject: 18242,
    FunctionRareData: 13504,
    AsyncFunction: 13189,

Note how in both cases, the Promise and the Headers/Request/Response are all still there. Something is keeping it alive forever. It's probably not emitting the close event.

@tilenpirih
Copy link
Author

Any estimations when this could be resolved? IMO memory leaks and stability should be very high priority.

@scottix
Copy link

scottix commented Oct 8, 2024

Does it make any difference if you set nitro preset in your nuxt config

  nitro: {
    preset: 'bun'
  },

@tilenpirih
Copy link
Author

tilenpirih commented Oct 8, 2024

This is ram usage after 100000 requests and it seems in a normal range. It's still 10x more compared to npm so there is probably still some room for improvements. But atleast it's solve me headaches with memory leaks for now.
Image

@nektro nektro added node.js Compatibility with Node.js APIs memory leak and removed needs triage labels Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working memory leak node.js Compatibility with Node.js APIs
Projects
None yet
Development

No branches or pull requests

4 participants