Arawoof06 opened a new pull request, #1543:
URL: https://github.com/apache/arrow-java/pull/1543

   ## 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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to