Repository navigation
GH-906: Don't revive a released BufferLedger in retain - #1543
Merged
Merged
Conversation
Arawoof06
requested review from
jbonofre,
laurentgo,
lidavidm and
wgtmac
as code owners
December 2, 2026 08:08
jbonofre
approved these changes
Dec 2, 2026
jbonofre
left a comment
Member
There was a problem hiding this comment.
LGTM, thanks for the fix!
Checking the count before the compare-and-set closes the revive-after-release hole cleanly, and a retain that races with the final release fails and throws as expected.
This was referenced Dec 2, 2026
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.
What's Changed
BufferLedger.retain(int)bumpsbufRefCntwith an unconditionalgetAndAddand checks the previous value afterwards. When the ledger has already dropped to zero, soAllocationManager.releasehas run and the chunk is freed, the add leaves the count at the increment and then throws, which means the rejected retain is itself what makes the dead ledger look alive.That count is the only thing standing between a released
ArrowBufand the freed memory behind it.ArrowBuf.ensureAccessible, reached fromchk/checkIndexDon every bounds-checked get and set, just asks the reference manager for the count. So after one rejectedretain(),getInt/setInton that buffer stop throwing and read and write a chunk the allocator has already given back, and the nextretain()returns normally because the previous value is now 1, so the caller ends up holding what it believes is a live reference. On a server that retains and releases buffers from request threads that shows up as stale or corrupted data far away from the call that was rejected, which is what #906 reports hitting in a long running service.The count is now read and checked before the new value is published, with
compareAndSet, so a released ledger is never momentarily positive. Undoing the increment on the error path, as suggested in the issue, still leaves a window where a secondretain()sees the temporary value; per the issue thread the compare-and-set did not show up in the reporter's benchmarks. A live ledger behaves as before, including theIllegalArgumentExceptiontype, which now carries a message.testRetainOnReleasedBufferIsRejectedRepeatedlyinTestBaseAllocatorreleases a buffer, then checks that everyretainis rejected, that the count stays 0, and thatsetIntstill throws. It fails onmainat the first count assertion. Thememory,vectorandalgorithmsuites pass.Closes #906.