Skip to content

Bun.S3File.arrayBuffer() retains RSS and reaches OOM in a 1 GiB Linux container despite forced GC #29083

Closed

Description

@sorenbs

What version of Bun is running?

1.3.11

What platform is your computer?

macOS host: Darwin 24.6.0 arm64 arm, Linux container: Linux 6.17.8-orbstack-00308-g8f9c941121b1 aarch64 unknown

What steps can reproduce the bug?

I’m seeing large RSS growth from repeated Bun.S3File.arrayBuffer() calls in a minimal Linux container repro.

run.sh
repro.ts

What is the expected behavior?

Expected behavior

Repeated Bun.S3File.arrayBuffer() calls on the same object should not cause sustained RSS growth toward OOM after forced GC. RSS should remain bounded or return close to baseline after the temporary buffers are no longer referenced.

What do you see instead?

Observed behavior

The repro prints structured output like:

REPRO_START {...}
REPRO_BASELINE {...}
REPRO_PROGRESS {...}
REPRO_PROGRESS {...}
REPRO_THRESHOLD_REACHED {...}

On Bun 1.3.11, it reliably crosses the configured 900 MiB RSS threshold inside the 1 GiB container.

Example output:

REPRO_START {"bun_version":"1.3.11","platform":"linux","arch":"arm64","payload_mib":8,"target_rss_mib":900,"cgroup_memory_limit_bytes":1073741824,...}
REPRO_BASELINE {"rss_bytes":65523712,...}
REPRO_PROGRESS {"iteration":88,"rss_bytes":877965312,...}
REPRO_THRESHOLD_REACHED {"iteration":95,"rss_bytes":945467392,"target_rss_bytes":943718400,...}

The repro forces Bun.gc(true) after every iteration, but RSS still climbs.

The printed fields include:

  • rss_bytes
  • heap_total_bytes
  • heap_used_bytes
  • external_bytes
  • array_buffers_bytes
  • cgroup_memory_limit_bytes
  • cgroup_memory_current_bytes

In my runs, RSS grows far beyond what the JS-managed memory suggests.

Actual OOM verification

If I continue beyond the threshold:

PAYLOAD_MIB=8 TARGET_RSS_MIB=900 MAX_ITERATIONS=4096 REPORT_EVERY=4 EXIT_ON_THRESHOLD=0 bash run.sh

the same Bun 1.3.11 repro reaches an actual OOM kill in the 1 GiB container:

DOCKER_EXIT=137

The last printed iterations before the kill showed RSS still climbing past the container limit boundary, for example:

REPRO_THRESHOLD_REACHED {"iteration":100,"rss_bytes":1011945472,...}
REPRO_THRESHOLD_REACHED {"iteration":103,"rss_bytes":1037668352,...}
REPRO_PROGRESS {"iteration":108,"rss_bytes":1044312064,...}
DOCKER_EXIT=137

Additional information

Why this matters

This pattern maps directly to real application code that does:

return new Uint8Array(await body.arrayBuffer());

In a long-lived Bun server, this appears to accumulate anonymous RSS even when:

  • JS heap remains much smaller
  • buffers are not intentionally retained
  • GC is forced repeatedly

Related issues

This repro is narrower than those reports because it isolates the built-in Bun S3 API directly:

  • Bun.S3Client
  • Bun.S3File.arrayBuffer()

Additional note

I checked Bun’s public type surface and did not find an explicit cleanup/finalizer API such as close(), destroy(), dispose(), or finalize() on S3File or S3Client for this code path.

Activity

  1. robobun commented on Apr 9, 2026

    @robobun
    Collaborator

    🔧 c02d478 (comment-only path fix) — debian-13-x64-asan-test-bun shows 7 retry successes followed by 1 later failure. Classic flake on a retry; my commit was a single-word path change in a comment, which cannot cause a test failure. claude[bot] also flagged a pre-existing multipart cancel-race leak in startMultiPartRequestResult; they explicitly marked it non-blocking and "fine to leave for a follow-up", so leaving it for a separate PR to keep this ones scope from ballooning further.

  2. github-actions commented on Apr 9, 2026

    @github-actions
    Contributor

    Found 3 possible duplicate issues:

    1. Memory leak when downloading file (both @google-cloud/storage and Bun's S3) #20487
    2. Blob/ArrayBuffer memory not reclaimed by GC after dereferencing #28741
    3. Memory is not getting freed after reading multiple files (Node:fs and Bun.File) #15020

    This issue will be automatically closed as a duplicate in 3 days.

    • If your issue is a duplicate, please close it and 👍 the existing issue instead
    • To prevent auto-closure, add a comment or 👎 this comment

    🤖 Generated with Claude Code

  3. sorenbs commented on Apr 9, 2026

    @sorenbs
    Author

    We addressed this issue by implementing a simple R3 client over fetch

  4. added 3 commits that reference this issue on Apr 9, 2026
    331fe65
    afa7eff
    763cac4
  5. piazzatron commented on Apr 14, 2026

    @piazzatron

    I suspect this issue is not limited to Bun.S3File.arrayBuffer(), but rather may be a more systemic GC issue with Bun's handling of arrayBuffers. See #28741

  6. added 2 commits that reference this issue on May 3, 2026
    cc2c8d3
    98d6dce
  7. added 2 commits that reference this issue on May 4, 2026
    c99f32f
    1bb937c
  8. sorenbs commented on May 18, 2026

    @sorenbs
    Author

    This seem to be fixed by the Rust rewrite 🎉

    Codex:

    Stable (1.3.14) reproduces both threshold crossing and actual OOM kill. I’ll run the same two commands on canary now with no script changes.

    Canary behavior is materially different: with the same default script, RSS stayed near ~100MB and never hit the 900MB threshold. I’m now running the 4096-iteration OOM variant on canary to confirm it remains bounded.

    Full report from Codex

    Issue Reproduced Report: #29083

    Source

    I saved exact copies here:

    SHA-256:

    • run.sh: 11bc91f9b6df726235a5c16f583afbdd334966034c4d18a7063388d293c7c91b
    • repro.ts: ac1b34c179967b2a79b6304bd00e04210376fe96ac025c15a35b9b1f633e9dd4

    Test Environment

    • Date: 2026-05-18
    • Host: Darwin 24.6.0 arm64 arm
    • Container memory limit: --memory=1g --memory-swap=1g
    • MinIO-backed S3 endpoint created by the provided run.sh

    Versions Tested

    • Latest stable tag: oven/bun:latest
      • bun --version: 1.3.14
      • bun --revision: 1.3.14+0d9b296af
      • image digest: sha256:e10577f0db68676a7024391c6e5cb4b879ebd17188ab750cf10024a6d700e5c4
    • Latest canary tag: oven/bun:canary
      • bun --version: 1.3.14
      • bun --revision: 1.3.14-canary.1+172afa532
      • image digest: sha256:a33d83fb11037dc2b4849cbe8af01333b2a469fdf66202d2cb7ed3f9a459def0
    • Tag reference: Docker Hub oven/bun tags

    Stable (oven/bun:latest) Results

    1) Issue default repro command

    cd /Users/sorenschmidt/code/bun-mem/issue-29083
    BUN_IMAGE=oven/bun:latest ./run.sh

    Observed:

    • Threshold hit at iteration 96
    • rss_bytes=945,741,824 (901.9 MiB) vs target 900 MiB
    • JS heap/external remained small relative to RSS growth
    • Exit code: 0 (expected because EXIT_ON_THRESHOLD=1)

    Log: stable-default.log

    2) Issue OOM verification command

    cd /Users/sorenschmidt/code/bun-mem/issue-29083
    BUN_IMAGE=oven/bun:latest PAYLOAD_MIB=8 TARGET_RSS_MIB=900 \
    MAX_ITERATIONS=4096 REPORT_EVERY=4 EXIT_ON_THRESHOLD=0 ./run.sh

    Observed:

    • First threshold crossing at iteration 97 (905.0 MiB)
    • Continued growth up to rss_bytes=1,111,687,168 (1060.2 MiB) before kill
    • Max cgroup current seen: 1,072,103,424 bytes (1022.4 MiB)
    • Exit code: 137 (OOM kill)

    Log: stable-4096-report4.log

    Canary (oven/bun:canary) Results

    1) Default repro command

    cd /Users/sorenschmidt/code/bun-mem/issue-29083
    BUN_IMAGE=oven/bun:canary ./run.sh

    Observed:

    • No threshold crossing
    • Peak sampled RSS: 103,833,600 (99.0 MiB)
    • Completed full 256 iterations
    • Exit code: 1 with REPRO_THRESHOLD_NOT_REACHED (expected for this script path)

    Log: canary-default.log

    2) Exact issue OOM verification command

    cd /Users/sorenschmidt/code/bun-mem/issue-29083
    BUN_IMAGE=oven/bun:canary PAYLOAD_MIB=8 TARGET_RSS_MIB=900 \
    MAX_ITERATIONS=4096 REPORT_EVERY=4 EXIT_ON_THRESHOLD=0 ./run.sh

    Observed:

    • No threshold crossing across all 4096 iterations
    • Peak sampled RSS: 124,084,224 (118.3 MiB)
    • Peak sampled cgroup current: 165,421,056 (157.8 MiB)
    • Exit code: 1 with REPRO_THRESHOLD_NOT_REACHED (no OOM)

    Log: canary-4096-report4.log

    Conclusion

    • On latest stable (1.3.14+0d9b296af), issue #29083 is still reproducible and reaches OOM in the 1 GiB container.
    • On latest canary (1.3.14-canary.1+172afa532), the same repro does not reproduce the leak pattern; RSS remains bounded and no OOM occurs, including the 4096-iteration run.

    If you want, I can now draft a concise comment for issue #29083 with these exact version/revision/digest details and the stable vs canary comparison.

    References

  9. alii commented on May 18, 2026

    @alii
    Member

    Great news! Closing this since it sounds like canary fixes it. Please leave a comment if you see otherwise in future, can re-open. Thank you!

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