--- a/src/pool/inner.rs +++ b/src/pool/inner.rs @@ -1,3 +1,9 @@ +// Local CranK pool patch: this file is patched from the upstream `sqlx-core` +// 0.9.0 source. The changes reserve `num_idle` before publishing an idle +// connection and bound each pool maintenance scan by the pool capacity. +// See ../../../../docs/dependency-patches/sqlx-core-0.9.0.md for provenance, +// the exact patch and the regression tests. Upstream copyright and license +// notices are unchanged. use super::connection::{Floating, Idle, Live}; use crate::connection::ConnectOptions; use crate::connection::Connection; @@ -206,15 +212,15 @@ let Floating { inner: idle, guard } = floating.into_idle(); - if self.idle_conns.push(idle).is_err() { + let published = publish_idle_connection(&self.num_idle, move || { + // NOTE: we need to make sure we drop the permit *after* we push to the idle queue + // don't decrease the size + self.idle_conns.push(idle).map(|()| guard.release_permit()) + }); + + if published.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(); - - self.num_idle.fetch_add(1, Ordering::AcqRel); } /// Try to atomically increment the pool size for a new connection. @@ -438,6 +444,37 @@ } } +/// 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 reserved *before* +/// `publish` so a concurrent `pop_idle` cannot underflow it between the queue +/// push and the counter update. If `publish` fails (the idle queue rejects the +/// connection), the reservation is rolled back before the error is returned to +/// the caller. +fn publish_idle_connection(num_idle: &AtomicUsize, publish: F) -> Result<(), E> +where + F: FnOnce() -> Result<(), E>, +{ + num_idle.fetch_add(1, Ordering::AcqRel); + + if let Err(error) = publish() { + num_idle.fetch_sub(1, Ordering::AcqRel); + return Err(error); + } + + Ok(()) +} + +/// Bounds the number of idle connections inspected per maintenance cycle. +/// +/// `num_idle` can transiently exceed the number of connections actually queued +/// during a publication race, so the maintenance scan is capped by the pool +/// capacity to keep each cycle bounded. +fn maintenance_batch_size(num_idle: usize, capacity: usize) -> usize { + cmp::min(num_idle, capacity) +} + impl Drop for PoolInner { fn drop(&mut self) { self.mark_closed(); @@ -545,7 +582,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 +662,106 @@ } } } + +#[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() { + // Production `release()` returns the rejected `Idle` as the error of + // this callback; a string error exercises the same helper contract + // without constructing a database connection. + 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" + ); + } +}