Fix gc sweeping binaries - #1
Merged
Merged
Conversation
pedroperrone
force-pushed
the
fix-gc-sweeping-binaries
branch
from
July 22, 2026 18:48
943ca50 to
2f43a21
Compare
pedroperrone
force-pushed
the
fix-gc-sweeping-binaries
branch
from
July 22, 2026 18:52
2f43a21 to
1bc708e
Compare
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.
ArrayInterface.from_tensor/1callsNx.to_binary/1on the tensor, captures the raw memory address of that binary viaget_binary_address/1, and stores the address in the struct. The XGBoost NIF later receives this address as a plain integer embedded in a JSON string and dereferences it directly to read the tensor data.There are two issues with this.
1. Small tensors produce heap binaries with unstable addresses
The BEAM allocates binaries in two ways:
A
{1, 4}float32 tensor (a common input shape for single-sample inference) is only 16 bytes — well below the threshold. The captured address becomes stale as soon as GC compacts the heap, and the NIF reads garbage from it. Because ExUnit runs tests in random order and GC is triggered by heap pressure, whether the bug manifests depends on which tests ran before the prediction call and how much heap they allocated. This is why it's seed-dependent.The fix is to pad the binary to above 64 bytes before capturing its address, forcing a refc binary with a stable off-heap address. XGBoost reads only the bytes corresponding to the tensor's actual shape, so the padding zeros are never accessed.
2. The binary has no live reference during the NIF call
In
inplace_predict, theArrayInterfacestruct is piped directly intoJason.encode!and then discarded — only the resulting JSON string is kept. After that line the struct, and the binary it holds, have no live references. This means the refc binary's reference count can drop to zero and its off-heap memory can be freed while the dirty NIF is still running and reading from that address.The fix is to bind the
ArrayInterfacestruct to a variable before encoding it and keep a reference to its binary alive past the NIF call, so it remains a live GC root for the full duration of the call.