Skip to content

Memory Leak When Streaming Responses via fetch in Bun ≥ 1.1.27 #18488

Closed

Description

@dmdb

What version of Bun is running?

1.1.27

What platform is your computer?

No response

What steps can reproduce the bug?

We’ve observed a memory leak when running our thumbnail proxy app under Bun 1.1.27+. The same app in 1.1.26 maintains steady memory usage. In newer versions, memory steadily grows until our container’s OOM killer terminates the process.

Application Workflow
1. Receive an HTTP GET request (via Bun.serve + Hono.js).
2. Perform a fetch to an external CDN to retrieve a thumbnail image.
3. Pipe that Response body back to the client with streaming (using Hono’s stream).
4. On non-200 fetch responses, we call response.body?.cancel() to free resources.
5. On success, we rely on the streaming to complete, and then the fetch response is closed naturally (i.e., once streaming ends).

import { Hono } from 'hono';
import { stream } from 'hono/streaming';

const app = new Hono();

app.get('/proxy', async (c) => {
  const url = 'https://example.com/image.jpeg'; // CDN resource
  let response: Response;
  try {
    response = await fetch(url);
  } catch (error) {
    return c.text('Fetch error', 502);
  }

  if (!response.ok) {
    await response.body?.cancel();
    return c.text('Bad response from CDN', 502);
  }

  // Example of streaming the successful response back
  return stream(c, async (streamWriter) => {
    try {
      return streamWriter.pipe(response.body!);
    } catch (error) {
      await response.body?.cancel();
      throw error;
    }
  });
});

// Start server
Bun.serve({
  fetch: app.fetch,
  port: 3000,
});

What is the expected behavior?

• Memory usage should not grow significantly over time.
• After numerous thumbnail requests, the memory usage should stabilize or at least get reclaimed eventually by GC.

What do you see instead?

• Under Bun 1.1.27+, memory grows steadily with every new request.
• It never stabilizes nor is reclaimed by GC, eventually hitting OOM.
• Reverting to Bun 1.1.26 resolves the issue.

Additional information

Here is chart of memory consumption of 2 pods running, before and after container bun version upgrade 1.1.26->1.1.27

Image

Activity

  1. Jarred-Sumner commented on Mar 26, 2025

    @Jarred-Sumner
    Collaborator

    Have you tried in Bun v1.2.5?

  2. dmdb commented on Mar 26, 2025

    @dmdb
    Author

    Have you tried in Bun v1.2.5?

    1.2.5, 1.2.6, 1.2 tried today - same situation

  3. ScottUK commented on Mar 29, 2025

    @ScottUK

    Could be related to what I saw here?
    #16529

    I've been running a project that sees this same issue which is essentially a proxy. Streams a response from a different site back to the client.

    I've essentially used every version of Bun from 1.1.45 up to 1.2.6, can try 1.2.7 today.

    Memory usage looks like this: (It's steady, the different peaks just represent peak usage times for the app as it's in production)
    Image

  4. mags-sully commented on Apr 12, 2025

    @mags-sully

    @ScottUK have you tried 1.2.9?

  5. ScottUK commented on Apr 12, 2025

    @ScottUK

    Just upgraded services to 1.2.9, will know within a few hours.

  6. ScottUK commented on Apr 12, 2025

    @ScottUK

    Can confirm still happening.

  7. drag0n-app commented on Oct 11, 2025

    @drag0n-app

    @ScottUK Bun 1.3 has been released. Please test it again.

  8. reyzzz commented on Oct 23, 2025

    @reyzzz

    I am on Bun 1.3 and have almost the same code as Scott. The only difference is, I am using got that uses fetch under the hood.
    I am still experiencing the problem.
    I am on Debian 12.

    It has been a long time with this problem, is there anything we can do to help?

  9. viktorlarsson commented on Nov 9, 2025

    @viktorlarsson

    Still happening on 1.3.0

  10. drag0n-app commented on Nov 14, 2025

    @drag0n-app

    I am on Bun 1.3 and have almost the same code as Scott. The only difference is, I am using got that uses fetch under the hood. I am still experiencing the problem. I am on Debian 12.

    It has been a long time with this problem, is there anything we can do to help?

    This problem has existed for years, that's why I'm still using Node.js :(

  11. Jarred-Sumner commented on Jan 13, 2026

    @Jarred-Sumner
    Collaborator

    This was likely fixed in Bun v1.3.6 - there was a memory leak in node:zlib (which could impact got) and there was an edgecase involving proxies that could cause ReadableStream from a request body to not get collected.

    if you continue to run into this, please file a new issue with a reproduction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions