oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.91k stars 2.74k forks source link

Possible memory leak #7377

Open thibaultleouay opened 10 months ago

thibaultleouay commented 10 months ago

What version of Bun is running?

1.0.14

What platform is your computer?

fly.io container

What steps can reproduce the bug?

We are running the bun from the latest docker image.

The code of our apps is here : https://github.com/openstatusHQ/openstatus/tree/822093042ce5b2ea028d51c0f090bdcfbb0cb302/apps/server

We have one machine that dont get garbage collected Here's what we see image

Here's what we expect CleanShot 2023-11-30 at 09 10 39

and it happened twice for the same machine image

Here's a snapshot https://snapshots.raintank.io/dashboard/snapshot/K7G90ajb9deOwkRtePmIAzAi7y9reE9v?orgId=2

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

aqibgatoo commented 10 months ago

We have a same story on our bun service.

image
Jarred-Sumner commented 10 months ago

We will investigate this soon

edwinbrowwn commented 8 months ago

Same issue when running within a container node:20-bookworm-slim. Screenshot 2024-02-08 185402

goldo commented 8 months ago

We got a memory leak with bun 1.0.26 too. image

I will try to narrow it down to a simple example, but we are on a very small project, using mainly: fastify@4.24.3 and prisma@5.9.1 A simple HTTP call to this is leaking:

  fastify.get("/health", async (request) => {
    const count = await request.server.prisma.user.count();
    return { ok: count > 0 };
  });

(Without using prisma in this call, it was OK OFC)

Then I tried to build this code for node, using bun build, to run it in node 20, and it was a leaking a LOT less, and the GC could free the memory regularly, where it couldnt be possible when the same code (built or not) was run with bun.

I made my tests using htop and autocannon with params -c 30 -d 15

goldo commented 3 months ago

The issue is still here on the latest bun version 1.1.20, and we had to make our server self close gracefully when a certain memory limit is reached in order to avoid getting segfault crashes due to memory limits.

Screenshot 2024-07-26 at 19 03 58

goldo commented 2 months ago

Hi, im no expert at all, but I tried to make a minimal reproduction with bun + fastify + prisma with results in readme https://github.com/goldo/bun-memory

Jarred-Sumner commented 2 months ago

This was fixed in https://github.com/oven-sh/bun/pull/12955, and will be available in the next release of Bun. To get it early, run bun upgrade --canary

The memory leak in this issue was caused by creating an AbortError when the HTTP request finished if the body was not yet consumed. This error object was never reachable from the garbage collector, and the garbage collector would take a very long time to ever clean these up because it’d only happen during a full collection

Jarred-Sumner commented 2 months ago

@goldo im not sure yet if the memory leak in this issue is the same as the one you’re running into. Can you open a new issue to track it?

goldo commented 2 months ago

@Jarred-Sumner thanks! I tried my small test project with this bun canary version and the problem seems to be still here. Here is the new issue: https://github.com/oven-sh/bun/issues/12977