Skip to content

Bun leaks memory in Workers #5709

Closed
Closed

Description

@jack-weilage

What version of Bun is running?

1.0.3+f77df12894e7952ea58605432bf9f6d252fff273

What platform is your computer?

Linux 5.15.90.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

Create a main file which starts up a worker, waits until it completes its work, then logs memory usage.

async function run() {
  for (let i = 0; i < 100; i++) {
    await new Promise((resolve) => {
      const worker = new Worker('./worker.ts')

      const on_message = () => {
        worker.removeEventListener('message', on_message)
        resolve(1)
      }

      worker.addEventListener('message', on_message)
      worker.postMessage('start')
    })

    console.log(process.memoryUsage().rss)
  }
}

run()

And a worker which allocates a large amount of memory

function on_message() {
  const arrays = []
  for (let i = 0; i < 1_000_000; i++) {
    arrays.push(new Array(100).fill(i))
  }

  self.postMessage(arrays.length)
  self.removeEventListener('message', on_message)
}

self.addEventListener('message', on_message)

Then run main with bun run main.ts

What is the expected behavior?

Memory usage should spike as the worker begins, then drop sometime afterwards as garbage collection is run. The script should continue running until the loop completes.

When a (functionally) identical script and worker (code below) is run via Node, memory usage stays around the same every call.

import { Worker } from 'worker_threads'

async function run() {
  for (let i = 0; i < 100; i++) {
    await new Promise((resolve) => {
      const worker = new Worker('./node/worker.js')

      const on_message = () => {
        worker.removeListener('message', on_message)
        resolve(1)
      }

      worker.addListener('message', on_message)
      worker.postMessage('start')
    })
    console.log(i)
  }
}

run()
import { parentPort } from 'worker_threads'

function on_message() {
  const arrays = []
  for (let i = 0; i < 1_000_000; i++) {
    arrays.push(new Array(100).fill(i))
  }

  parentPort.postMessage(arrays.length)
  parentPort.removeListener('message', on_message)
}

parentPort.addListener('message', on_message)

Results in node:

> node node/main.js
971173888
983752704
979537920
988639232
986144768
979836928
984797184
986599424
985792512
^C

What do you see instead?

Memory usage continues to rise, eventually crashing (saturates 8gb of RAM and 2gb of swap) at around the 8th iteration.

Results in Bun:

> bun bun/main.ts
1050456064
2005811200
2962059264
3915100160
4867907584
5819809792
6765035520
^C

Additional information

  • Adding smol to either bun run or new Worker() has no effect.
  • I fixed this by calling Bun.gc(true) immediately after self.postMessage() in a personal project, but couldn't reproduce the results here.
  • I don't have enough knowledge of swap memory to know if this is unexpected, but after crashing, the swap usage often grows.

Activity

  1. changed the title [-]Bun runs out of memory when using Workers[/-] [+]Bun doesn't clean up memory used by Workers[/+] on Sep 19, 2023
  2. changed the title [-]Bun doesn't clean up memory used by Workers[/-] [+]Bun leaks memory in Workers[/+] on Sep 19, 2023
  3. AriPerkkio commented on Sep 19, 2023

    @AriPerkkio

    I think I've run into similar issue when using Bun's Worker's. This error message is being thrown and Bun crashes when memory leaks enough:

    FATAL ERROR: JavaScript garbage collection failed because thread_get_state returned an error (268435459). This is probably the result of running inside Rosetta, which is not supported.
    /Users/jarred/actions-runner/_work/WebKit/WebKit/Source/WTF/wtf/posix/ThreadingPOSIX.cpp(497) : size_t WTF::Thread::getRegisters(const WTF::ThreadSuspendLocker &, WTF::PlatformRegisters &)
    
  4. Electroid commented on Sep 19, 2023

    @Electroid
    Contributor

    Thanks for reporting, this is a known issue that we will be fixing.

  5. jack-weilage commented on Sep 19, 2023

    @jack-weilage
    Author
    FATAL ERROR: JavaScript garbage collection failed because thread_get_state returned an error (268435459). This is probably the result of running inside Rosetta, which is not supported.
    /Users/jarred/actions-runner/_work/WebKit/WebKit/Source/WTF/wtf/posix/ThreadingPOSIX.cpp(497) : size_t WTF::Thread::getRegisters(const WTF::ThreadSuspendLocker &, WTF::PlatformRegisters &)
    

    Interesting! In my case, Bun never returned an error before crashing.

  6. jack-weilage commented on Oct 4, 2023

    @jack-weilage
    Author

    Any follow-up on when this might be fixed? This is preventing me from using Workers in bun.

  7. jack-weilage commented on Dec 29, 2023

    @jack-weilage
    Author

    I just re-ran the reproduction code and the memory leak seems to have been mostly fixed somewhere between v1.0.11 and v1.0.12. Memory usage used to double every iteration, but now (in my testing) memory usage went from 1013653504 to 1034555392 after 1000 iterations. That averages out to ~2000 per iteration, as compared to ~1000000000 per iteration when I originally reported.

  8. jack-weilage commented on Dec 29, 2023

    @jack-weilage
    Author

    Should this issue be closed (the massive memory leak is fixed), or kept open (there still seems to be a tiny leak left over)?

  9. dgertych-monterail commented on Mar 18, 2024

    @dgertych-monterail

    It's still valid for me for the most recent released version (1.0.33) even for canary :(
    The memory is growing on each worker passage, it's not free at all even if the worker has been terminated.

  10. MeguminSama commented on Apr 2, 2024

    @MeguminSama

    I was running into issues possibly caused by #5659 so I tried to use workers as a workaround, hoping that when the worker gets terminated, the memory might be freed. Unfortunately, this doesn't seem to be the case.

  11. kingkong404 commented on May 8, 2024

    @kingkong404

    Currently a major issue impacting us using Bun in production.

  12. rafrafek commented on Jul 10, 2024

    @rafrafek

    Collab with Deno maybe? denoland/deno#18414

  13. 5 remaining items

  14. kahy9 commented on Nov 6, 2025

    @kahy9

    Still valid, please fix that

  15. zengyun261 commented on Mar 31, 2026

    @zengyun261

    The issue remains unresolved on Windows 11 x64.

  16. 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

Labels

Workeruses the web or node worker apiatwbugSomething isn't workingmemory usagenode.jsCompatibility with Node.js APIs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions