Move code around in shutdown sequence.

Delete thread pools before detaching the current thread in case tasks
getting deleted need to have access to Thread::Current.

Test: test.py
Change-Id: Icdd222e817dd5b79649cd6d79ca6352683a4de1e
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 1fc434e..84ec7ef 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -449,11 +449,8 @@
     callbacks_->NextRuntimePhase(RuntimePhaseCallback::RuntimePhase::kDeath);
   }
 
-  if (attach_shutdown_thread) {
-    DetachCurrentThread(/* should_run_callbacks= */ false);
-    self = nullptr;
-  }
-
+  // Delete thread pools before detaching the current thread in case tasks
+  // getting deleted need to have access to Thread::Current.
   heap_->DeleteThreadPool();
   if (oat_file_manager_ != nullptr) {
     oat_file_manager_->DeleteThreadPool();
@@ -461,6 +458,11 @@
   DeleteThreadPool();
   CHECK(thread_pool_ == nullptr);
 
+  if (attach_shutdown_thread) {
+    DetachCurrentThread(/* should_run_callbacks= */ false);
+    self = nullptr;
+  }
+
   // Make sure our internal threads are dead before we start tearing down things they're using.
   GetRuntimeCallbacks()->StopDebugger();
   // Deletion ordering is tricky. Null out everything we've deleted.