Fix race in thread list destructor
The ThreadList::Contains wasn't being guarded by the thread list
lock, which could cause a corrupted thread list if another thread
unregistered itself while std::find was searching the thread list.
Bug: 17896374
(cherry picked from commit b90132cb5132eaeb4dbfca1e63d79b4005dacec5)
Change-Id: I8f6f979f365ca00ac0655e04eb26020d2ad7d6ee
diff --git a/runtime/thread_list.cc b/runtime/thread_list.cc
index 53c2859..35411e2 100644
--- a/runtime/thread_list.cc
+++ b/runtime/thread_list.cc
@@ -51,7 +51,13 @@
// Detach the current thread if necessary. If we failed to start, there might not be any threads.
// We need to detach the current thread here in case there's another thread waiting to join with
// us.
- if (Contains(Thread::Current())) {
+ bool contains = false;
+ {
+ Thread* self = Thread::Current();
+ MutexLock mu(self, *Locks::thread_list_lock_);
+ contains = Contains(self);
+ }
+ if (contains) {
Runtime::Current()->DetachCurrentThread();
}