Skip to content

GH-906: Don't revive a released BufferLedger in retain - #1543

Merged
jbonofre merged 1 commit into
apache:mainfrom
Arawoof06:buffer-ledger-retain-refcount
Dec 2, 2026
Merged

jbonofre merged 1 commit into
apache:mainfrom
Arawoof06:buffer-ledger-retain-refcount

Conversation

@Arawoof06

Copy link
Copy Markdown
Contributor

What's Changed

BufferLedger.retain(int) bumps bufRefCnt with an unconditional getAndAdd and checks the previous value afterwards. When the ledger has already dropped to zero, so AllocationManager.release has 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 ArrowBuf and the freed memory behind it. ArrowBuf.ensureAccessible, reached from chk/checkIndexD on every bounds-checked get and set, just asks the reference manager for the count. So after one rejected retain(), getInt/setInt on that buffer stop throwing and read and write a chunk the allocator has already given back, and the next retain() 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 second retain() 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 the IllegalArgumentException type, which now carries a message.

testRetainOnReleasedBufferIsRejectedRepeatedly in TestBaseAllocator releases a buffer, then checks that every retain is rejected, that the count stays 0, and that setInt still throws. It fails on main at the first count assertion. The memory, vector and algorithm suites pass.

Closes #906.

@jbonofre jbonofre added the bug-fix PRs that fix a big. label Dec 2, 2026
@apache apache deleted a comment from github-actions Bot Dec 2, 2026
@github-actions github-actions Bot added this to the 20.1.0 milestone Dec 2, 2026

@jbonofre jbonofre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jbonofre
jbonofre merged commit 8a8851b into apache:main Dec 2, 2026
21 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix PRs that fix a big.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling retain on a closed ArrowBuf revives it, into an undefined state

2 participants