invakid404
01/14/2026, 9:39 AMraw_ptr_wrapper abstraction, but the leak only really appears to be blatantly obvious when interacting with `FunctionLog`/`OnTick` callbacks, so I'm not quite sure), but something definitely is.
Here's a small reproduction that appears to consistently trigger a memory leak (+ other disturbing memory management-related behavior) on my darwin arm64 machine:
https://github.com/invakid404/baml-memory-leak-repro
The repo includes a fake OpenAI server to simulate a large streaming response, the logs I applied to the BAML src that made me think this is an actual memory leak and not just the OS being lazy with memory reclamation, and a main.go file that triggers the leak.
Formal warning, if you do apply the logs from the included patch, be prepared to be greeted by a lot of logs
I have nowhere near enough context about the project to be able to put a pin in it, so I'm hoping you can help me out a bit here.
Initially observed on 0.214.0 (we're still stuck on an older version due to multiple panic conditions that I reported as well, hopefully resolved in latest by now), confirmed to still be reproducible on latest canary.
(PS: the only reason I went on this witch hunt is because one of our Go services that interacts with BAML was and is going up in memory slowly but surely until it OOMs, so this is very much an issue in practice as well, although it happens much more slowly with real LLM requests; the extreme SSE chunk sizes in the fake server are simply there to make the issue obvious)Vaibhav (BAML 🐑)
01/14/2026, 9:41 AMVaibhav (BAML 🐑)
01/14/2026, 9:41 AMVaibhav (BAML 🐑)
01/14/2026, 9:43 AM[Periodic GC] Alloc: 0 MB, Sys: 19 MB, HeapInuse: 2 MB, HeapReleased: 8 MBinvakid404
01/14/2026, 9:52 AMinvakid404
01/14/2026, 9:53 AMgo run main.go, memory stays stable for me as wellinvakid404
01/14/2026, 9:53 AMgo build -o baml-test && ./baml-test, it appears to be a different storyinvakid404
01/14/2026, 9:53 AMVaibhav (BAML 🐑)
01/14/2026, 9:53 AMinvakid404
01/14/2026, 9:54 AMinvakid404
01/14/2026, 9:54 AMVaibhav (BAML 🐑)
01/14/2026, 9:54 AMVaibhav (BAML 🐑)
01/14/2026, 9:54 AMinvakid404
01/14/2026, 9:54 AMVaibhav (BAML 🐑)
01/14/2026, 9:55 AMinvakid404
01/14/2026, 9:55 AMinvakid404
01/14/2026, 9:55 AMVaibhav (BAML 🐑)
01/14/2026, 9:55 AMVaibhav (BAML 🐑)
01/14/2026, 9:55 AMinvakid404
01/14/2026, 9:56 AMVaibhav (BAML 🐑)
01/14/2026, 9:56 AMVaibhav (BAML 🐑)
01/14/2026, 9:56 AMinvakid404
01/14/2026, 9:56 AMinvakid404
01/14/2026, 9:56 AMVaibhav (BAML 🐑)
01/14/2026, 9:57 AMVaibhav (BAML 🐑)
01/14/2026, 9:57 AMinvakid404
01/14/2026, 10:01 AMVaibhav (BAML 🐑)
01/14/2026, 10:02 AMVaibhav (BAML 🐑)
01/14/2026, 10:02 AMVaibhav (BAML 🐑)
01/14/2026, 10:45 AMVaibhav (BAML 🐑)
01/14/2026, 10:45 AMVaibhav (BAML 🐑)
01/14/2026, 10:45 AMVaibhav (BAML 🐑)
01/14/2026, 10:46 AMVaibhav (BAML 🐑)
01/14/2026, 11:53 AMpython3 analyze_ffi.py rust.log go.log
Loaded 299328 events from rust.log
Loaded 296040 events from go.log
Total: 595368 FFI events, 20 callback events, 0 error events
=== FFI Lifecycle Analysis ===
Rust side:
FFI_CREATE: 2770
FFI_WRAP_ARC: 95910
FFI_GIVE_GO: 98680 <- Rust gives ptr to Go
FFI_GO_RELEASE: 98680 <- Rust destructor called
FFI_FREE: 3288 <- Arc fully freed
Go side:
GO_RECEIVE: 98680 <- Go receives object
GO_DESTRUCTOR_START: 98680 <- Go finalizer starts
GO_DESTRUCTOR_OK: 98680 <- Go finalizer succeeds
GO_DESTRUCTOR_ERROR: 0 <- Go finalizer fails
=== NO RUST-SIDE LEAKS ===
All FFI_GIVE_GO have matching FFI_GO_RELEASE.
=== NO GO-SIDE LEAKS ===
All GO_RECEIVE have matching GO_DESTRUCTOR.
=== LEAK COMPARISON ===
Leaked on Rust side only: 0 pointers
Leaked on Go side only: 0 pointers
Leaked on both sides: 0 pointers
Callback map:
Total ADDs: 10
Total DELs: 10
Final map size: 0
=== SUMMARY ===
GO OK: All 98680 objects were properly finalized
RUST OK: All 98680 pointers properly releasedVaibhav (BAML 🐑)
01/14/2026, 11:54 AMVaibhav (BAML 🐑)
01/14/2026, 11:54 AMVaibhav (BAML 🐑)
01/14/2026, 11:56 AMinvakid404
01/14/2026, 12:40 PMinvakid404
01/14/2026, 12:40 PMVaibhav (BAML 🐑)
01/14/2026, 12:56 PMVaibhav (BAML 🐑)
01/14/2026, 1:04 PMVaibhav (BAML 🐑)
01/15/2026, 6:12 AMVaibhav (BAML 🐑)
01/15/2026, 6:13 AM.Close() methods on all FFI handles that devs can then write defer obj.Close() withinvakid404
01/15/2026, 9:01 AMinvakid404
01/15/2026, 9:02 AMVaibhav (BAML 🐑)
01/15/2026, 3:08 PMaaronv (BAML)
01/22/2026, 8:58 AM