Skip to content

Commit b178f31

Browse files
committed
Don't forever break thread state if trying to access global dispatch once and failing
1 parent 6b1ef55 commit b178f31

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

tracing-core/src/dispatcher.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ use alloc::sync::{Arc, Weak};
149149

150150
#[cfg(feature = "alloc")]
151151
use core::ops::Deref;
152+
use std::ops::DerefMut;
152153

153154
/// `Dispatch` trace data to a [`Subscriber`].
154155
#[derive(Clone)]
@@ -370,11 +371,11 @@ where
370371
.try_with(|state| {
371372
if let Some(entered) = state.enter() {
372373
println!("[SPAN STACK] Found default dispatch on tid={:?}, pid={:?}", std::thread::current().id(), std::process::id());
373-
return f(&*entered.current());
374+
return entered.with_current(&mut f);
374375
}
375376
println!("[SPAN STACK] Got none dispatch on tid={:?}, pid={:?}", std::thread::current().id(), std::process::id());
376377

377-
f(&Dispatch::none())
378+
(&mut f)(&Dispatch::none())
378379
})
379380
.unwrap_or_else(|e| {
380381
println!("[SPAN STACK] Got none dispatch on tid={:?}, pid={:?}, {e}", std::thread::current().id(), std::process::id());
@@ -396,7 +397,7 @@ pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> {
396397
CURRENT_STATE
397398
.try_with(|state| {
398399
let entered = state.enter()?;
399-
Some(f(&*entered.current()))
400+
Some(entered.with_current_once(f))
400401
})
401402
.ok()?
402403
}
@@ -813,23 +814,36 @@ impl State {
813814
}
814815
}
815816

817+
818+
816819
// ===== impl Entered =====
817820

818821
#[cfg(feature = "std")]
819822
impl<'a> Entered<'a> {
820823
#[inline]
821-
fn current(&self) -> RefMut<'a, Dispatch> {
822-
let default = self.0.default.borrow_mut();
823-
RefMut::map(default, |default| {
824-
println!("[SPAN STACK] Entered has current dispatch={}, tid={:?}, pid={:?}", default.is_some(), std::thread::current().id(), std::process::id());
825-
default.get_or_insert_with(|| {
826-
println!("[SPAN STACK] Entered current is none, tid={:?}, pid={:?}", std::thread::current().id(), std::process::id());
827-
get_global().cloned().unwrap_or_else(|| {
828-
println!("[SPAN STACK] Entered current was none, global was none, using none-dispatcher tid={:?}, pid={:?}", std::thread::current().id(), std::process::id());
829-
Dispatch::none()
830-
})
831-
})
832-
})
824+
fn with_current<T, F: FnMut(&Dispatch) -> T>(&self, func: &mut F) -> T {
825+
let mut default = self.0.default.borrow_mut();
826+
if let Some(d) = default.as_ref() {
827+
func(d)
828+
} else if let Some(global) = get_global() {
829+
default.replace(global.clone());
830+
func(global)
831+
} else {
832+
func(&Dispatch::none())
833+
}
834+
}
835+
836+
#[inline]
837+
fn with_current_once<T, F: FnOnce(&Dispatch) -> T>(&self, func: F) -> T {
838+
let mut default = self.0.default.borrow_mut();
839+
if let Some(d) = default.as_ref() {
840+
func(d)
841+
} else if let Some(global) = get_global() {
842+
default.replace(global.clone());
843+
func(global)
844+
} else {
845+
func(&Dispatch::none())
846+
}
833847
}
834848
}
835849

0 commit comments

Comments
 (0)