--- a/src/pool/inner.rs +++ b/src/pool/inner.rs @@ -206,15 +206,21 @@ let Floating { inner: idle, guard } = floating.into_idle(); - if self.idle_conns.push(idle).is_err() { - panic!("BUG: connection queue overflow in release()"); - } + let published = publish_idle_connection(&self.num_idle, move || { + if self.idle_conns.push(idle).is_err() { + panic!("BUG: connection queue overflow in release()"); + } - // NOTE: we need to make sure we drop the permit *after* we push to the idle queue - // don't decrease the size - guard.release_permit(); + // NOTE: we need to make sure we drop the permit *after* we push to the idle queue + // don't decrease the size + guard.release_permit(); - self.num_idle.fetch_add(1, Ordering::AcqRel); + Ok::<(), std::convert::Infallible>(()) + }); + + if let Err(never) = published { + match never {} + } } /// Try to atomically increment the pool size for a new connection. @@ -438,6 +444,29 @@ } } +/// Reserves the idle-connection slot for a publication and runs `publish`. +/// +/// `publish` performs the actual publication (pushing onto `idle_conns` and +/// releasing the semaphore permit). The `num_idle` counter is updated *after* +/// `publish` returns, matching upstream ordering. +fn publish_idle_connection(num_idle: &AtomicUsize, publish: F) -> Result<(), E> +where + F: FnOnce() -> Result<(), E>, +{ + publish()?; + num_idle.fetch_add(1, Ordering::AcqRel); + Ok(()) +} + +/// Bounds the number of idle connections inspected per maintenance cycle. +/// +/// The maintenance task iterates `0..num_idle`; upstream returns the raw +/// counter. +fn maintenance_batch_size(num_idle: usize, capacity: usize) -> usize { + let _ = capacity; + num_idle +} + impl Drop for PoolInner { fn drop(&mut self) { self.mark_closed(); @@ -545,7 +574,11 @@ // and if we have fewer than min_connections after reaping a connection, // open a new one immediately. Note that other connections may be popped from // the queue in the meantime - that's fine, there is no harm in checking more - for _ in 0..pool.num_idle() { + let batch_size = maintenance_batch_size( + pool.num_idle(), + pool.options.max_connections as usize, + ); + for _ in 0..batch_size { if let Some(conn) = pool.try_acquire() { if is_beyond_idle_timeout(&conn, &pool.options) || is_beyond_max_lifetime(&conn, &pool.options) @@ -621,3 +654,101 @@ } } } + +#[cfg(test)] +mod tests { + use super::{maintenance_batch_size, publish_idle_connection}; + use crossbeam_queue::ArrayQueue; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::{Arc, Barrier}; + use std::thread; + + #[test] + fn maintenance_batch_size_bounds_scan_by_capacity() { + let capacity = 8; + + assert_eq!(maintenance_batch_size(0, capacity), 0); + assert_eq!(maintenance_batch_size(1, capacity), 1); + assert_eq!(maintenance_batch_size(capacity, capacity), capacity); + assert_eq!(maintenance_batch_size(capacity + 1, capacity), capacity); + assert_eq!(maintenance_batch_size(usize::MAX, capacity), capacity); + } + + #[test] + fn publish_reserves_idle_counter_before_connection_is_visible() { + const ITEM: usize = 7; + + let queue: Arc> = Arc::new(ArrayQueue::new(4)); + let num_idle = Arc::new(AtomicUsize::new(0)); + let published = Arc::new(Barrier::new(2)); + let popped = Arc::new(Barrier::new(2)); + + let producer = { + let queue = Arc::clone(&queue); + let num_idle = Arc::clone(&num_idle); + let published = Arc::clone(&published); + let popped = Arc::clone(&popped); + thread::spawn(move || { + publish_idle_connection(num_idle.as_ref(), move || { + queue.push(ITEM).expect("queue accepts published connection"); + // The consumer can only pop once the connection is queued. + published.wait(); + // Keep the closure open until the concurrent pop has happened so the + // counter is observed at the exact moment the connection was visible. + popped.wait(); + Ok::<(), ()>(()) + }) + .expect("publication succeeds"); + }) + }; + + let consumer = { + let queue = Arc::clone(&queue); + let num_idle = Arc::clone(&num_idle); + let published = Arc::clone(&published); + let popped = Arc::clone(&popped); + thread::spawn(move || { + published.wait(); + let item = queue.pop().expect("published connection is visible"); + let previous = num_idle.fetch_sub(1, Ordering::AcqRel); + // Do not assert here: on the buggy ordering the producer would still be + // waiting for this barrier and an early panic would deadlock the test. + popped.wait(); + (item, previous) + }) + }; + + let (item, previous) = consumer.join().expect("consumer does not panic"); + producer.join().expect("producer does not panic"); + + assert_eq!(item, ITEM); + assert!( + previous > 0, + "idle counter must be reserved before the connection is visible, saw {previous}" + ); + assert_eq!( + previous, 1, + "a single publication must reserve exactly one idle slot" + ); + assert_eq!( + num_idle.load(Ordering::Acquire), + 0, + "idle counter must settle back to zero" + ); + } + + #[test] + fn publish_rolls_back_idle_counter_when_publication_fails() { + let num_idle = AtomicUsize::new(0); + + let result: Result<(), &'static str> = + publish_idle_connection(&num_idle, || Err("publish failed")); + + assert_eq!(result, Err("publish failed")); + assert_eq!( + num_idle.load(Ordering::Acquire), + 0, + "failed publication must not leak an idle slot" + ); + } +}