Merge "Fix a lock level violation with background compaction enabled."
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index f2919e8..7235729 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -1331,11 +1331,6 @@
       if (collector_type == collector_type_) {
         return;
       }
-      if (Runtime::Current()->IsShuttingDown(self)) {
-        // Don't allow heap transitions to happen if the runtime is shutting down since these can
-        // cause objects to get finalized.
-        return;
-      }
       // GC can be disabled if someone has a used GetPrimitiveArrayCritical but not yet released.
       if (!copying_transition || disable_moving_gc_count_ == 0) {
         // TODO: Not hard code in semi-space collector?
@@ -1345,6 +1340,12 @@
     }
     usleep(1000);
   }
+  if (Runtime::Current()->IsShuttingDown(self)) {
+    // Don't allow heap transitions to happen if the runtime is shutting down since these can
+    // cause objects to get finalized.
+    FinishGC(self, collector::kGcTypeNone);
+    return;
+  }
   tl->SuspendAll();
   switch (collector_type) {
     case kCollectorTypeSS: