diff --git a/rust/src/articledl.rs b/rust/src/articledl.rs index f995e3deb..202ea6522 100644 --- a/rust/src/articledl.rs +++ b/rust/src/articledl.rs @@ -5505,11 +5505,14 @@ mod tests { let context = crate::parlive::source_write_context(-1_961_502, 40, &dir, b"part40.rar").unwrap(); let limit = crate::parchecker::effective_live_pool_bytes(); assert!(limit > 0); - // The pool is process-global; hold whatever other tests left free. + // Fill a pool of this test's own (the shared one is process-global, and holding all of + // it starved every other live test running in parallel). + let pool = std::sync::Arc::new(crate::parlive::BudgetPool::with_size(limit)); + crate::parchecker::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = Some(pool.clone())); let mut reservation = Vec::new(); let mut chunk = limit; while chunk != 0 { - while let Some(lease) = crate::parlive::LIVE_BUDGET.acquire(chunk, limit) { + while let Some(lease) = pool.acquire(chunk, limit) { reservation.push(lease); } chunk /= 2; @@ -5519,6 +5522,7 @@ mod tests { assert!(file.flush_and_close()); assert_eq!(std::fs::read(path).unwrap(), b"ordinary"); drop(reservation); + crate::parchecker::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = None); crate::parlive::forget_download_writes(-1_961_502); std::fs::remove_dir_all(dir).unwrap(); } diff --git a/rust/src/parchecker.rs b/rust/src/parchecker.rs index b2517b6fc..c3d1ed0cb 100644 --- a/rust/src/parchecker.rs +++ b/rust/src/parchecker.rs @@ -12,7 +12,7 @@ use crate::parlive::{ download_writes, reserve_control, resources, ControlLease, SessionBudgetLease, - SessionGeneration, WriteCoordinator, LIVE_BUDGET, + SessionGeneration, WriteCoordinator, }; use std::collections::{HashMap, HashSet, VecDeque}; use std::io::{Read, Seek, SeekFrom}; @@ -401,8 +401,17 @@ fn lease_pool_bytes(lease: &SessionBudgetLease) -> u64 { lease.pool().size().unwrap_or_else(effective_live_pool_bytes) } +#[cfg(test)] +thread_local! { + /// A test's own budget pool for sessions started on its thread (see `live_lifecycle_tests`): + /// the daemon's shared pool is one process-wide 500 MB that parallel tests would exhaust. + pub(crate) static TEST_BUDGET_POOL: std::cell::RefCell>> = + const { std::cell::RefCell::new(None) }; +} + fn acquire_session_budget(bytes: u64, limit: u64) -> Result { - LIVE_BUDGET + let pool = crate::parlive::live_pool(); + pool .acquire(bytes, limit) .ok_or(RepairError::StreamingMemoryLimit { context: "shared live PAR reservation", diff --git a/rust/src/parchecker/live_lifecycle_tests.rs b/rust/src/parchecker/live_lifecycle_tests.rs index 43d4e2e7e..178e4a561 100644 --- a/rust/src/parchecker/live_lifecycle_tests.rs +++ b/rust/src/parchecker/live_lifecycle_tests.rs @@ -17,6 +17,10 @@ impl Fixture { NEXT_FIXTURE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed) )); std::fs::create_dir(&dir).unwrap(); + // Sessions this test starts draw on a pool of its own, not the shared 500 MB one. + super::TEST_BUDGET_POOL.with(|p| { + *p.borrow_mut() = Some(Arc::new(BudgetPool::with_size(500 << 20))); + }); for f in std::fs::read_dir( Path::new(env!("CARGO_MANIFEST_DIR")).join("../tests/testdata/parchecker"), ) @@ -33,6 +37,7 @@ impl Fixture { } impl Drop for Fixture { fn drop(&mut self) { + super::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = None); if !std::thread::panicking() { let _ = std::fs::remove_dir_all(&self.0); } diff --git a/rust/src/parlive.rs b/rust/src/parlive.rs index 523b53d61..633ef10e2 100644 --- a/rust/src/parlive.rs +++ b/rust/src/parlive.rs @@ -200,6 +200,17 @@ impl SessionBudgetLease { pub(crate) static LIVE_BUDGET: LazyLock> = LazyLock::new(|| Arc::new(BudgetPool::default())); +/// The pool live reservations draw on: the daemon's shared one, or in a test the pool the +/// test installed for its thread (`parchecker::TEST_BUDGET_POOL`), so tests that fill or +/// share it cannot starve each other. +pub(crate) fn live_pool() -> Arc { + #[cfg(test)] + if let Some(pool) = crate::parchecker::TEST_BUDGET_POOL.with(|p| p.borrow().clone()) { + return pool; + } + Arc::clone(&LIVE_BUDGET) +} + #[derive(Default)] struct ControlAccounting { bytes: u64, @@ -294,7 +305,7 @@ fn reserve_control_inner( // Ordinary-write root control is accounted (so live reservations yield to // it) but never refused by the live caps. if pool_optional { - let budget = LIVE_BUDGET.acquire(bytes, pool_limit); + let budget = live_pool().acquire(bytes, pool_limit); used.bytes = used.bytes.saturating_add(bytes); *used.jobs.entry(nzb_id).or_insert(0) += bytes; return Ok(ControlLease { @@ -319,7 +330,7 @@ fn reserve_control_inner( MIB, )); } - let budget = LIVE_BUDGET.acquire(bytes, pool_limit); + let budget = live_pool().acquire(bytes, pool_limit); if budget.is_none() { return Err(limit_error("shared live PAR reservation", bytes, pool_limit)); } diff --git a/rust/src/parlive/physical_ownership_tests.rs b/rust/src/parlive/physical_ownership_tests.rs index 4c7ccd264..75ce3e5d6 100644 --- a/rust/src/parlive/physical_ownership_tests.rs +++ b/rust/src/parlive/physical_ownership_tests.rs @@ -363,10 +363,13 @@ fn exhausted_live_budget_does_not_bypass_a_foreign_certified_source() { let gate = f.seal(&path); let limit = crate::parchecker::effective_live_pool_bytes(); assert!(limit > 0); + // Drain a pool of this test's own: holding all of the shared one starves parallel tests. + let pool = Arc::new(BudgetPool::with_size(limit)); + crate::parchecker::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = Some(Arc::clone(&pool))); let mut held = Vec::new(); let mut chunk = 1024 * 1024u64; while chunk != 0 { - while let Some(lease) = LIVE_BUDGET.acquire(chunk, limit) { held.push(lease); } + while let Some(lease) = pool.acquire(chunk, limit) { held.push(lease); } chunk /= 2; } let other_job = NEXT_JOB.fetch_sub(1, Ordering::Relaxed); @@ -380,6 +383,7 @@ fn exhausted_live_budget_does_not_bypass_a_foreign_certified_source() { assert_eq!(std::fs::read(path).unwrap(), b"certified"); assert!(gate.is_current_generation(SessionGeneration(1))); drop(held); + crate::parchecker::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = None); forget_download_writes(other_job); f.finish(); }