Skip to content

Commit 81e9e5b

Browse files
committed
!Send + Sync without unsafe!
thanks @MikailBag for the suggestion! Signed-off-by: Eliza Weisman <[email protected]>
1 parent 7eef5d3 commit 81e9e5b

File tree

1 file changed

+2
-19
lines changed

1 file changed

+2
-19
lines changed

tracing/src/span.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ pub(crate) struct Inner {
380380
#[must_use = "once a span has been entered, it should be exited"]
381381
pub struct Entered<'a> {
382382
span: &'a Span,
383-
_not_send: PhantomData<*mut ()>,
383+
_not_send: PhantomData<crate::stdlib::sync::MutexGuard<'static, ()>>,
384384
}
385385

386386
/// `log` target for all span lifecycle (creation/enter/exit/close) records.
@@ -1235,24 +1235,6 @@ impl Clone for Inner {
12351235

12361236
// ===== impl Entered =====
12371237

1238-
/// # Safety
1239-
///
1240-
/// Technically, `Entered` _can_ implement both `Send` *and* `Sync` safely. It
1241-
/// doesn't, because it has a `PhantomData<*mut ()>` field, specifically added
1242-
/// in order to make it `!Send`.
1243-
///
1244-
/// Sending an `Entered` guard between threads cannot cause memory unsafety.
1245-
/// However, it *would* result in incorrect behavior, so we add a
1246-
/// `PhantomData<*mut ()>` to prevent it from being sent between threads. This
1247-
/// is because it must be *dropped* on the same thread that it was created;
1248-
/// otherwise, the span will never be exited on the thread where it was entered,
1249-
/// and it will attempt to exit the span on a thread that may never have entered
1250-
/// it. However, we still want them to be `Sync` so that a struct holding an
1251-
/// `Entered` guard can be `Sync`.
1252-
///
1253-
/// Thus, this is totally safe.
1254-
unsafe impl<'a> Sync for Entered<'a> {}
1255-
12561238
impl<'a> Drop for Entered<'a> {
12571239
#[inline]
12581240
fn drop(&mut self) {
@@ -1313,4 +1295,5 @@ mod test {
13131295

13141296
trait AssertSync: Sync {}
13151297
impl AssertSync for Span {}
1298+
impl<'a> AssertSync for Entered<'a> {}
13161299
}

0 commit comments

Comments
 (0)