diff --git a/sentry-core/src/hub_impl.rs b/sentry-core/src/hub_impl.rs index f897cd40..299c4ecf 100644 --- a/sentry-core/src/hub_impl.rs +++ b/sentry-core/src/hub_impl.rs @@ -2,21 +2,20 @@ use std::cell::{Cell, RefCell}; use std::marker::PhantomData; use std::sync::{Arc, LazyLock, MutexGuard, PoisonError, RwLock}; use std::thread; +use std::thread::ThreadId; use crate::Scope; use crate::{scope::Stack, Client, Hub}; -static PROCESS_HUB: LazyLock<(Arc, thread::ThreadId)> = LazyLock::new(|| { - ( - Arc::new(Hub::new(None, Arc::new(Default::default()))), - thread::current().id(), - ) +static PROCESS_HUB: LazyLock = LazyLock::new(|| ProcessHub { + hub: Arc::new(Hub::new(None, Arc::new(Default::default()))), + thread: thread::current().id(), }); thread_local! { static THREAD_HUB: (RefCell>, Cell) = ( - RefCell::new(Arc::new(Hub::new_from_top(&PROCESS_HUB.0))), - Cell::new(PROCESS_HUB.1 == thread::current().id()) + RefCell::new(Arc::new(Hub::new_from_top(&PROCESS_HUB.hub))), + Cell::new(PROCESS_HUB.thread == thread::current().id()) ); } @@ -154,7 +153,7 @@ impl Hub { /// This is similar to [`Hub::current`] but instead of picking the /// current thread's hub it returns the main thread's hub instead. pub fn main() -> Arc { - PROCESS_HUB.0.clone() + PROCESS_HUB.hub.clone() } /// Invokes the callback with the default hub. @@ -167,7 +166,7 @@ impl Hub { { THREAD_HUB.with(|(hub, is_process_hub)| { if is_process_hub.get() { - f(&PROCESS_HUB.0) + f(&PROCESS_HUB.hub) } else { // Bind `hub` as `hub.borrow().clone()`. // It is essential we drop `hub.borrow()` before the callback, otherwise we will @@ -217,6 +216,14 @@ impl Hub { } } +/// Helper struct for storing the [`PROCESS_HUB`]. +struct ProcessHub { + /// The process's main hub. + hub: Arc, + /// The thread on which the main hub was initialized. + thread: ThreadId, +} + #[cfg(test)] mod tests { use super::*;