You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "cetra3 (via GitHub)" <gi...@apache.org> on 2026/07/08 02:18:33 UTC
[PR] Fix pool claim race condition [arrow-rs]
cetra3 opened a new pull request, #10301:
URL: https://github.com/apache/arrow-rs/pull/10301
# Which issue does this PR close?
Closes https://github.com/apache/arrow-rs/issues/10139
# Rationale for this change
This adjusts the internals of the memory pool reservation so that there isn't any double accounting when replacing memory with `claim`
# What changes are included in this PR?
Adds a new struct in `pool.rs` which is used to track reservations on buffers. This is essentially a newtype lift of the existing structure, but with a few specialised methods to ensure that claim accounts correctly.
# Are these changes tested?
Yes a new test has been added
# Are there any user-facing changes?
No this is just some memory pool accounting internals
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "cetra3 (via GitHub)" <gi...@apache.org>.
cetra3 commented on PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#issuecomment-4997458202
Yes that is essentially what this PR does. But instead of duplicating that exact same bit of code everywhere, we lift it into a new struct that does it correctly every time & makes it easy for other things to get it right.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey merged PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on code in PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#discussion_r3608845225
##########
arrow-buffer/src/bytes.rs:
##########
@@ -110,22 +107,15 @@ impl Bytes {
/// Register this [`Bytes`] with the provided [`MemoryPool`], replacing any prior reservation.
#[cfg(feature = "pool")]
pub(crate) fn claim(&self, pool: &dyn MemoryPool) {
- *self.reservation.lock().unwrap() = Some(pool.reserve(self.capacity()));
+ self.reservation.claim(pool, self.capacity());
}
/// Resize the memory reservation of this buffer
///
/// This is a no-op if this buffer doesn't have a reservation.
#[cfg(feature = "pool")]
fn resize_reservation(&self, new_size: usize) {
- let mut guard = self.reservation.lock().unwrap();
- if let Some(mut reservation) = guard.take() {
- // Resize the reservation
- reservation.resize(new_size);
-
- // Put it back
- *guard = Some(reservation);
- }
Review Comment:
nice
##########
arrow-buffer/src/pool.rs:
##########
@@ -23,15 +23,15 @@
//! is as follows:
//!
//! ```text
-//! (pool tracker) (resizable)
+//! (pool tracker) (resizable)
//! ┌──────────────────┐ fn reserve() ┌─────────────────────────┐
-//! │ trait MemoryPool │─────────────►│ trait MemoryReservation │
Review Comment:
Nit: there are a couple of spots in this PR where nothing has actually changed but a diff is still being shown. We should generally try to avoid that.
##########
arrow-buffer/src/pool.rs:
##########
@@ -186,4 +257,54 @@ mod tests {
drop(reservation2);
assert_eq!(pool.used(), 0);
}
+
+ /// A [`MemoryPool`] that records the peak usage observed at the instant
+ /// each reservation is taken, letting a single-threaded test witness the
+ /// transient double-count that [`TrackedReservation::claim`] must avoid.
+ #[derive(Debug, Default)]
+ struct PeakPool {
+ inner: TrackingMemoryPool,
+ peak: AtomicUsize,
+ }
+
+ impl MemoryPool for PeakPool {
+ fn reserve(&self, size: usize) -> Box<dyn MemoryReservation> {
+ let reservation = self.inner.reserve(size);
+ self.peak.fetch_max(self.inner.used(), Ordering::Relaxed);
+ reservation
+ }
+
+ fn available(&self) -> isize {
+ self.inner.available()
+ }
+
+ fn used(&self) -> usize {
+ self.inner.used()
+ }
+
+ fn capacity(&self) -> usize {
+ self.inner.capacity()
+ }
+ }
+
+ #[test]
+ fn test_claim_reclaims_before_reserving() {
Review Comment:
nice regression test
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#discussion_r3902558186
##########
arrow-buffer/src/pool.rs:
##########
@@ -23,9 +23,9 @@
//! is as follows:
//!
//! ```text
-//! (pool tracker) (resizable)
+//! (pool tracker) (resizable)
//! ┌──────────────────┐ fn reserve() ┌─────────────────────────┐
-//! │ trait MemoryPool │─────────────►│ trait MemoryReservation │
+//! │ trait MemoryPool │────────────►│ trait MemoryReservation │
Review Comment:
```suggestion
//! │ trait MemoryPool │─────────────►│ trait MemoryReservation │
```
##########
arrow-buffer/src/pool.rs:
##########
@@ -52,20 +52,20 @@ pub trait MemoryReservation: Debug + Send + Sync {
/// tell if the buffer is shared or not.
///
/// ```text
-/// Array A Array B
+/// Array A Array B
/// ┌────────────┐ ┌────────────┐
/// │ slices... │ │ slices... │
/// │────────────│ │────────────│
/// │ Arc<Bytes> │ │ Arc<Bytes> │ (shared buffer)
-/// └─────▲──────┘ └───────▲────┘
-/// │ │
-/// │ Bytes │
-/// │ ┌─────────────┐ │
-/// │ │ data... │ │
-/// │ │─────────────│ │
-/// └──│ Memory │──┘ (tracked with a memory pool)
-/// │ Reservation │
-/// └─────────────┘
+/// └─────▲─────┘ └───────▲───┘
Review Comment:
```suggestion
/// └─────▲──────┘ └───────▲────┘
```
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "jhorstmann (via GitHub)" <gi...@apache.org>.
jhorstmann commented on PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#issuecomment-5342307239
I just started looking into the memory pool api, and am wondering which usecases would require the arrays to change owners. Maybe a second `claim` call with a different (non ptr-eq) `MemoryPool` should just not be allowed?
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "cetra3 (via GitHub)" <gi...@apache.org>.
cetra3 commented on PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#issuecomment-5348694363
> I just started looking into the memory pool api, and am wondering which usecases would require the arrays to change owners. Maybe a second claim call with a different (non ptr-eq) MemoryPool should just not be allowed?
This is driven by datafusion and attributing where the recordbatch lives within the execution tree. If you can't change the owner then you don't know what part of the tree is currently holding onto the memory. I.e, is it the `DataSourceExec` reading the parquet, or is it the `SortExec` that is bundling it up?
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#issuecomment-5503659758
thanks all
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#issuecomment-5468857483
with #10759 merged this needs to be merged up/rebased on main
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#issuecomment-5012398297
> Yes that is essentially what this PR does. But instead of duplicating that exact same bit of code everywhere, we lift it into a new struct that does it correctly every time & makes it easy for other things to get it right.
this PR makes those call sites much more readable
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "cetra3 (via GitHub)" <gi...@apache.org>.
cetra3 commented on code in PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#discussion_r3620294241
##########
arrow-buffer/src/pool.rs:
##########
@@ -147,6 +147,77 @@ impl MemoryReservation for Tracker {
}
}
+/// This is a wrapper for the reservation so we can standardize on changing
+/// and avoid race conditions in memory accounting
+#[derive(Debug, Default)]
+pub struct TrackedReservation {
Review Comment:
I've moved this to `pub(crate)`. If people find it useful we can adjust but it is really an internal only thing
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "cetra3 (via GitHub)" <gi...@apache.org>.
cetra3 commented on PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#issuecomment-5300680330
I've rebased to current `main` so this is ready from my side
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#issuecomment-4998000065
> Yes that is essentially what this PR does. But instead of duplicating that exact same bit of code everywhere, we lift it into a new struct that does it correctly every time & makes it easy for other things to get it right.
@cetra3 okay nice. Ill try and take a look tomorrow
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#discussion_r3617230606
##########
arrow-buffer/src/pool.rs:
##########
@@ -147,6 +147,77 @@ impl MemoryReservation for Tracker {
}
}
+/// This is a wrapper for the reservation so we can standardize on changing
+/// and avoid race conditions in memory accounting
+#[derive(Debug, Default)]
+pub struct TrackedReservation {
Review Comment:
Is this `pub` in the sense that it can be used outside this crate? If so I think we should document it more, and be clear if we need to expand the public API -- if it is meant to be internal, perhaps we can mark it as `pub(crate)`
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix pool claim race condition [arrow-rs]
Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on PR #10301:
URL: https://github.com/apache/arrow-rs/pull/10301#issuecomment-5300448662
is there any update on this?
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org