Skip to content

Bun memory leak with simple fetch polling script #28427

Closed

Description

@copilotenstar

What version of Bun is running?

1.3.11

What platform is your computer?

Linux 6.1.0-18-amd64 x86_64 unknown

What steps can reproduce the bug?

const RSS_URL = "https://www.bluetracker.gg/rss/diablo4/";
const POLL_INTERVAL_MS = 31000;
const LAST_GUID_FILE = "./last_guid.txt";
const RUN_DURATION_MS = 1 * 3600 * 1000; // 1 hour

type BluePost = {
  guid: string;
  title: string;
  description: string;
  link: string;
  pubDate: string;
};

let lastSeenGuid: string | null = null;

async function loadLastGuid(): Promise<string | null> {
  try {
    const file = Bun.file(LAST_GUID_FILE);
    if (await file.exists()) {
      return await file.text();
    }
  } catch (e) {
    console.log(`Failed to load last guid: ${e}`);
  }
  return null;
}

async function saveLastGuid(guid: string): Promise<void> {
  try {
    await Bun.write(LAST_GUID_FILE, guid);
  } catch (e) {
    console.log(`Failed to save last guid: ${e}`);
  }
}

async function webhook(post: BluePost) {
  const json = {
    username: "Blues",
    attachments: [{fallback: "N/A", title: post.title, title_link: post.link, text: post.description}],
  };
  const url = "https://removed.com/";
  try {
    await fetch(url, {
      method: "POST",
      body: JSON.stringify(json),
      headers: {
        "Content-Type": "application/json"
      }
    });
  } catch (e) {
    console.log(`webhook err: ${e}`);
  }
}

function extractTag(block: string, tag: string): string | null {
  const match = block.match(
    new RegExp(`<${tag}>([\\s\\S]*?)<\\/${tag}>`, "i")
  );
  return match ? match[1].trim() : null;
}

function getLatestItem(xml: string): BluePost | null {
  const itemMatch = xml.match(/<item>([\s\S]*?)<\/item>/i);
  if (!itemMatch) return null;
  const item = itemMatch[1];
  return {
    guid: extractTag(item, "guid") ?? "",
    title: extractTag(item, "title") ?? "",
    description: extractTag(item, "description") ?? "",
    link: extractTag(item, "link") ?? "",
    pubDate: extractTag(item, "pubDate") ?? "",
  };
}

async function poll() {
  try {
    const res = await fetch(RSS_URL);
    if (!res.ok) {
      throw new Error(`RSS fetch failed: ${res.status}`);
    }
    const xml = await res.text();
    const latest = getLatestItem(xml);
    
    if (!latest?.guid) return;
    
    if (latest.guid !== lastSeenGuid) {
      lastSeenGuid = latest.guid;
      await saveLastGuid(latest.guid);
      await webhook(latest);
    }
  } catch (err) {
    console.error("Polling error:", err);
  }
}

async function main() {
  lastSeenGuid = await loadLastGuid();
  
  const startTime = Date.now();
  
  while (Date.now() - startTime < RUN_DURATION_MS) {
    await poll();
    await Bun.sleep(POLL_INTERVAL_MS);
  }
  
  process.exit(0);
}

main();

Running this simple script (Made by Claude) causes memory to leak (my server with 8gb ram eventually OOM'd). I had to ask Claude to add a 1 hour max run duration to it.

Webhooks are almost never fired (maybe once a week at most) so it's likely just related to the fetch() polling.

To observe memory leak easier, simply change POLL_INTERVAL_MS to 1000.

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

Activity

  1. robobun commented on Mar 22, 2026

    @robobun
    Collaborator

    Unable to reproduce this. What I tried:

    I tested a script closely mimicking yours (local HTTP server + fetch polling with regex XML parsing) on Bun 1.3.11 (Linux) with up to 50,000 iterations.

    Results:

    Test Iterations HeapUsed Growth RSS Growth
    Full script, no explicit GC 50,000 0.6 → 3.5 MB 42 → 54 MB
    Full script, with Bun.gc(true) every 5K iters 50,000 Stable ~0.48 MB Stable ~49 MB
    Fetch-only, with GC 50,000 Stable ~0.30 MB Stable ~49 MB
    Node.js v24 comparison 10,000 17 → 61 MB 73 → 236 MB

    No memory leak was found. When garbage collection runs, all memory is fully reclaimed. The slow heap growth without explicit GC is normal — JSC's garbage collector doesn't trigger aggressively when there's low memory pressure. Bun actually uses significantly less memory than Node.js for the same workload.

    Your script polls every 31 seconds (~116 polls/hour). At the observed growth rate without GC (~3.5 MB per 50K polls), it would take millions of polls to approach 8GB — far longer than the 1-hour runtime.

    A couple of things to investigate on your end:

    1. There was a recent fix on main for an HTTPS fetch memory leak with SSL config caching (commit 05327256e5). Since you're fetching from https://www.bluetracker.gg/, this may have affected you. Try updating to the latest Bun.
    2. Check if something else on the server was consuming memory (other processes, swap pressure, etc.).

    If you can still reproduce the OOM after updating Bun, a minimal repro with memory snapshots (e.g., logging process.memoryUsage() every poll) would help narrow it down.

    If you can provide a minimal reproduction (exact command, Bun version, platform), please update the issue.


    Automated investigation. Reopen with more context if this is wrong.

  2. ReD-GuardiaN commented on Apr 1, 2026

    @ReD-GuardiaN

    The automated investigation above is incorrect. The bot tested with a local HTTP server, not real remote HTTPS fetches, which doesn't exercise the same Blob/body buffering code path.

    I've reproduced this issue independently on Bun 1.3.11 (Windows) with a minimal script that fetches 50 remote files as Blobs, clears all references, then calls Bun.gc(true) multiple times with 8 seconds of wait time.

    Results on the same machine:

    Bun 1.3.11 Node v25.6.0
    Peak 184.2 MB 125.3 MB
    After GC 184.3 MB 75.3 MB
    Released 0% 73%

    Bun releases zero memory after dereferencing all Blobs and forcing GC. Node correctly frees ~73%.

    This is the same underlying bug as #12941 (which was prematurely closed). I've opened a new issue with a clean single-file repro that works with both bun repro.js and node --expose-gc repro.js for direct comparison.

  3. alii commented on May 13, 2026

    @alii
    Member

    Couldn't reproduce this on Bun 1.3.13 (linux x64). Closing — if you're still hitting it on the latest release, please drop a fresh repro and we'll reopen.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions