From 2fe454238dab2bd6b61cdef199654b214b973364 Mon Sep 17 00:00:00 2001 From: John Spray Date: Thu, 3 Aug 2023 15:44:14 +0100 Subject: [PATCH] pageserver: warn on unexpected refs to Tenant after shutdown This should help to validate that our shutdown logic is working the way we expect. --- pageserver/src/tenant/mgr.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pageserver/src/tenant/mgr.rs b/pageserver/src/tenant/mgr.rs index 25c5e3f2e0..23eab85477 100644 --- a/pageserver/src/tenant/mgr.rs +++ b/pageserver/src/tenant/mgr.rs @@ -274,12 +274,15 @@ async fn shutdown_all_tenants0(tenants: &tokio::sync::RwLock) { let ordering = std::sync::atomic::Ordering::Relaxed; let joined_other = std::sync::atomic::AtomicBool::new(false); + let tenant_weak = Arc::downgrade(&tenant); let mut shutdown = std::pin::pin!(async { let freeze_and_flush = true; let res = { let (_guard, shutdown_progress) = completion::channel(); - tenant.shutdown(shutdown_progress, freeze_and_flush).await + // Always safe to upgrade this weak ptr, because the outer code block + // holds an Arc to it past the point it awaits this async block. + tenant_weak.upgrade().unwrap().shutdown(shutdown_progress, freeze_and_flush).await }; if let Err(other_progress) = res { @@ -304,6 +307,13 @@ async fn shutdown_all_tenants0(tenants: &tokio::sync::RwLock) { } }; + // Shutdown should have stopped all the background tasks that referenced + // the tenant: when we drop our ref it should drop the Tenant. If that isn't + // the case it's a bug. + if Arc::into_inner(tenant).is_none() { + warn!("Tenant still has references after shutdown"); + } + debug!("tenant successfully stopped"); } .instrument(info_span!("shutdown", %tenant_id)),