mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
## Summary Fix several memory leaks in the compression libraries: - **NativeBrotli/NativeZstd reset()** - Each call to `reset()` allocated a new encoder/decoder without freeing the previous one - **NativeBrotli/NativeZstd init() error paths** - If `setParams()` failed after `stream.init()` succeeded, the instance was leaked - **NativeZstd init()** - If `setPledgedSrcSize()` failed after context creation, the context was leaked - **ZlibCompressorArrayList** - After `deflateInit2_()` succeeded, if `ensureTotalCapacityPrecise()` failed with OOM, zlib internal state was never freed - **NativeBrotli close()** - Now sets state to null to prevent potential double-free (defensive) - **LibdeflateState** - Added `deinit()` for API consistency ## Test plan - [x] Added regression test that calls `reset()` 100k times and measures memory growth - [x] Test shows memory growth dropped from ~600MB to ~10MB for Brotli - [x] Verified no double-frees by tracing code paths - [x] Existing zlib tests pass (except pre-existing timeout in debug build) Before fix (system bun 1.3.3): ``` Memory growth after 100000 reset() calls: 624.38 MB (BrotliCompress) Memory growth after 100000 reset() calls: 540.63 MB (BrotliDecompress) ``` After fix: ``` Memory growth after 100000 reset() calls: 11.84 MB (BrotliCompress) Memory growth after 100000 reset() calls: 0.16 MB (BrotliDecompress) ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com>