From ca087319251da0740a26ab2afee3700a91af739f Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Mon, 5 Sep 2022 11:20:26 +0200 Subject: [PATCH 1/3] task: ignore failure to set TLS in LocalSet Drop --- tokio/src/task/local.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tokio/src/task/local.rs b/tokio/src/task/local.rs index a5bd1bb8835..3786cf36669 100644 --- a/tokio/src/task/local.rs +++ b/tokio/src/task/local.rs @@ -633,6 +633,37 @@ impl LocalSet { f() }) } + + /// This method is like `with`, but it just calls `f` without setting the thread-local if that + /// fails. + fn with_if_possible(&self, f: impl FnOnce() -> T) -> T { + let mut f = Some(f); + + let res = CURRENT.try_with(|ctx| { + struct Reset<'a> { + ctx_ref: &'a Cell>>, + val: Option>, + } + impl<'a> Drop for Reset<'a> { + fn drop(&mut self) { + self.ctx_ref.replace(self.val.take()); + } + } + let old = ctx.replace(Some(self.context.clone())); + + let _reset = Reset { + ctx_ref: ctx, + val: old, + }; + + (f.take().unwrap())() + }); + + match res { + Ok(res) => res, + Err(_access_error) => (f.take().unwrap())(), + } + } } cfg_unstable! { @@ -744,7 +775,7 @@ impl Default for LocalSet { impl Drop for LocalSet { fn drop(&mut self) { - self.with(|| { + self.with_if_possible(|| { // Shut down all tasks in the LocalOwnedTasks and close it to // prevent new tasks from ever being added. self.context.owned.close_and_shutdown_all(); From a57e64a3a3951588f8a5176d93bec977fa0d58d7 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Mon, 12 Sep 2022 23:25:58 +0200 Subject: [PATCH 2/3] Add test --- tokio/tests/task_local_set.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tokio/tests/task_local_set.rs b/tokio/tests/task_local_set.rs index b6245acf77a..eb4de1c5d95 100644 --- a/tokio/tests/task_local_set.rs +++ b/tokio/tests/task_local_set.rs @@ -311,6 +311,26 @@ fn join_local_future_elsewhere() { }); } +// Tests for +#[tokio::test(flavor = "multi_thread")] +async fn localset_in_thread_local() { + thread_local! { + static LOCAL_SET: LocalSet = LocalSet::new(); + } + + // holds runtime thread until end of main fn. + let (_tx, rx) = oneshot::channel::<()>(); + let handle = tokio::runtime::Handle::current(); + + std::thread::spawn(move || { + LOCAL_SET.with(|local_set| { + handle.block_on(local_set.run_until(async move { + let _ = rx.await; + })) + }); + }); +} + #[test] fn drop_cancels_tasks() { use std::rc::Rc; From 1a10b2c12f73812c098682c7e7fb993fb2beb112 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Mon, 12 Sep 2022 23:28:57 +0200 Subject: [PATCH 3/3] Scope out wasi --- tokio/tests/task_local_set.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tokio/tests/task_local_set.rs b/tokio/tests/task_local_set.rs index eb4de1c5d95..e3c0c65f712 100644 --- a/tokio/tests/task_local_set.rs +++ b/tokio/tests/task_local_set.rs @@ -312,6 +312,7 @@ fn join_local_future_elsewhere() { } // Tests for +#[cfg(not(tokio_wasi))] // Wasi doesn't support threads #[tokio::test(flavor = "multi_thread")] async fn localset_in_thread_local() { thread_local! {