From 15712018da9b13165bb5c5a72c32478c700d578e Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Mon, 5 Jun 2023 18:36:48 +0200 Subject: [PATCH] rt: `Scoped` should not be `Sync` (#5765) If the `Scoped` type is `Sync`, then you can call `set` from two threads in parallel. Since it accesses `inner` without synchronization, this is a data race. This is a soundness issue for the `Scoped` type, but since this is an internal API and we don't use it incorrectly anywhere, no harm is done. --- tokio/src/runtime/context/scoped.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tokio/src/runtime/context/scoped.rs b/tokio/src/runtime/context/scoped.rs index cc9465824..7b202a16c 100644 --- a/tokio/src/runtime/context/scoped.rs +++ b/tokio/src/runtime/context/scoped.rs @@ -6,8 +6,6 @@ pub(super) struct Scoped { pub(super) inner: Cell<*const T>, } -unsafe impl Sync for Scoped {} - impl Scoped { pub(super) const fn new() -> Scoped { Scoped { @@ -52,7 +50,7 @@ impl Scoped { if val.is_null() { f(None) } else { - unsafe { f(Some(&*(val as *const T))) } + unsafe { f(Some(&*val)) } } } }