Free RustCallStatus pointer after every Rust call#38
Merged
1egoman merged 1 commit intoMay 12, 2026
Conversation
The call-status pointer allocated by createPointer in makeRustCall was never released. ffi-rs's createPointer Box-allocates non-StackStruct values via Box::into_raw(Box::new(generate_c_struct(...))), and the JsExternal wrapper does not free that memory on garbage collection per ffi-rs's documented contract. Neither restorePointer nor unwrapPointer free anything either. Result: every synchronous Rust call leaked the RustCallStatus struct plus its wrapper Box, and method calls leaked twice (once for clonePointer, once for the call itself). Wrap the call site in try/finally and call freePointer with the same struct paramsType so the allocation is reclaimed regardless of whether the call returned normally or threw.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every synchronous call into Rust through
makeRustCallleaks the call-status struct that gets passed in. Wrap the call site intry/finallyand reclaim the pointer withfreePointer.Why this is a leak
createCallStatuscallsffi-rs'screatePointer({ paramsType: [DataType_UniffiRustCallStatus], ... }).DataType_UniffiRustCallStatusdoes not setffiTypeTag: DataType.StackStruct(only the innererror_buffield does), so ffi-rs takes the heap-allocation branch inget_value_pointer:That produces two heap allocations: the struct itself (via
generate_c_struct) and a wrapperBox<*mut c_void>. Neither is owned by JS. From the ffi-rs README: "JsExternal objects do NOT automatically free memory upon garbage collection. UsefreePointerto free the pointer when it is no longer in use."restorePointeris read-only, andunwrapPointeronly produces a new external around the inner pointer — neither reclaims the original allocation.Net effect: every sync FFI call leaks roughly the size of
UniffiRustCallStatusplus a pointer-sized wrapper. Method calls leak it twice (once forclonePointer, once for the actual call). Constructors,uniffiDestroy, and the FinalizationRegistry-driven free path each leak it once. For long-running or high-throughput consumers this is unbounded growth.How the fix works
makeRustCallnow wraps the call site intry/finallyand callsfreePointeron$callStatusin thefinallyblock, using the sameDataType_UniffiRustCallStatusparamsType andPointerType.RsPointerso ffi-rs takes the matching free path (free_rs_pointer_memoryObject branch: dealloc the struct memory and reclaim the wrapper). Thefinallyensures the pointer is freed whether the Rust call returned normally, the call status indicated an error anduniffiCheckCallStatusthrew, or any other exception propagated through the body.