From 9bf5b22633afe4c1acc0c07f26459ccde8a08e29 Mon Sep 17 00:00:00 2001 From: Zak Stucke Date: Sun, 2 Nov 2025 15:24:26 +0200 Subject: [PATCH] Force cleanup even if there are other references to the root owner --- integrations/utils/src/lib.rs | 2 +- reactive_graph/src/owner.rs | 19 +++++++++++++++++++ router/src/static_routes.rs | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/integrations/utils/src/lib.rs b/integrations/utils/src/lib.rs index b4438eed9..880bacb4c 100644 --- a/integrations/utils/src/lib.rs +++ b/integrations/utils/src/lib.rs @@ -121,7 +121,7 @@ pub trait ExtendResponse: Sized { // drop the owner, cleaning up the reactive runtime, // once the stream is over .chain(once(async move { - owner.unset(); + owner.unset_with_forced_cleanup(); Default::default() })), )); diff --git a/reactive_graph/src/owner.rs b/reactive_graph/src/owner.rs index 8ad9a7d66..070af695e 100644 --- a/reactive_graph/src/owner.rs +++ b/reactive_graph/src/owner.rs @@ -340,6 +340,8 @@ impl Owner { } /// Removes this from its state as the thread-local owner and drops it. + /// If there are other holders of this owner, it may not cleanup, if always cleaning up is required, + /// see [`Owner::unset_with_forced_cleanup`]. pub fn unset(self) { OWNER.with_borrow_mut(|owner| { if owner.as_ref().and_then(|n| n.upgrade()) == Some(self) { @@ -348,6 +350,23 @@ impl Owner { }) } + /// Removes this from its state as the thread-local owner and drops it. + /// Unlike [`Owner::unset`], this will always run cleanup on this owner, + /// even if there are other holders of this owner. + pub fn unset_with_forced_cleanup(self) { + OWNER.with_borrow_mut(|owner| { + if owner + .as_ref() + .and_then(|n| n.upgrade()) + .map(|o| o == self) + .unwrap_or(false) + { + mem::take(owner); + } + }); + self.cleanup(); + } + /// Returns the current [`SharedContext`], if any. #[cfg(feature = "hydration")] pub fn current_shared_context( diff --git a/router/src/static_routes.rs b/router/src/static_routes.rs index ab6c60204..9d5bb51ec 100644 --- a/router/src/static_routes.rs +++ b/router/src/static_routes.rs @@ -369,7 +369,7 @@ impl ResolvedStaticPath { eprintln!("{e}"); } } - owner.unset(); + owner.unset_with_forced_cleanup(); } } });