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.
This commit is contained in:
Stjepan Glavina 2018-09-11 22:48:06 +02:00 committed by Toby Lawrence
parent 19d5565442
commit 0f44adf5f6

View File

@ -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.