Skip to content

Commit 63c81fe

Browse files
askoaaskoa
andauthored
Add check to avoid underflow in memory manager (#4351)
* Add check to avoid underflow in memory manager * incorporate PR comment Co-authored-by: askoa <askoa@local>
1 parent 0d65394 commit 63c81fe

File tree

1 file changed

+11
-1
lines changed
  • datafusion/core/src/execution/memory_manager

1 file changed

+11
-1
lines changed

datafusion/core/src/execution/memory_manager/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl MemoryManager {
330330

331331
fn max_mem_for_requesters(&self) -> usize {
332332
let trk_total = self.get_tracker_total();
333-
self.pool_size - trk_total
333+
self.pool_size.saturating_sub(trk_total)
334334
}
335335

336336
/// Grow memory attempt from a consumer, return if we could grant that much to it
@@ -651,4 +651,14 @@ mod tests {
651651
let config = MemoryManagerConfig::try_new_limit(100000, 0.1).unwrap();
652652
assert_eq!(config.pool_size(), 10000);
653653
}
654+
655+
#[tokio::test]
656+
async fn test_memory_manager_underflow() {
657+
let config = MemoryManagerConfig::try_new_limit(100, 0.5).unwrap();
658+
let manager = MemoryManager::new(config);
659+
manager.grow_tracker_usage(100);
660+
661+
manager.register_requester(&MemoryConsumerId::new(1));
662+
assert!(!manager.can_grow_directly(20, 0));
663+
}
654664
}

0 commit comments

Comments
 (0)