From 0f44adf5f696ac73dcdcb1fb2b91b87553c57443 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Tue, 11 Sep 2018 22:48:06 +0200 Subject: [PATCH] reactor: use LocalKey::try_with in sharded RW lock (#628) @jonhoo reported a panic in the call to `LocalKey::with`, which occurs when the reactor is dropped in the middle of TLS teardown. This PR changes the call to `LocalKey::try_with` and handles the case when the thread-local value has already been destroyed. --- tokio-reactor/src/sharded_rwlock.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tokio-reactor/src/sharded_rwlock.rs b/tokio-reactor/src/sharded_rwlock.rs index 090896134..6b6207b59 100644 --- a/tokio-reactor/src/sharded_rwlock.rs +++ b/tokio-reactor/src/sharded_rwlock.rs @@ -150,11 +150,12 @@ impl<'a, T> DerefMut for RwLockWriteGuard<'a, T> { /// Returns a `usize` that identifies the current thread. /// -/// Each thread is associated with an 'index'. While there are no particular guarantees, indices -/// usually tend to be consecutive numbers between 0 and the number of running threads. +/// Each thread is associated with an 'index'. Indices usually tend to be consecutive numbers +/// between 0 and the number of running threads, but there are no guarantees. During TLS teardown +/// the associated index might change. #[inline] pub fn thread_index() -> usize { - REGISTRATION.with(|reg| reg.index) + REGISTRATION.try_with(|reg| reg.index).unwrap_or(0) } /// The global registry keeping track of registered threads and indices.