[vm/finalizer] Stop background compiler before sorting classes.

Prevent background compiler from adding deferred classes or canonicalizing
new types while classes are being sorted and type hashes are modified.
This should fix issue #34627 and its duplicate #35690.


Change-Id: I3e52602e77ad6b80b58f5292721c01eea22abfb0
Reviewed-on: https://dart-review.googlesource.com/c/90300
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Régis Crelier <regis@google.com>
diff --git a/runtime/tests/vm/vm.status b/runtime/tests/vm/vm.status
index 251fd9a..0f8ce61 100644
--- a/runtime/tests/vm/vm.status
+++ b/runtime/tests/vm/vm.status
@@ -144,9 +144,6 @@
 cc/PrintJSON: Crash
 cc/Service_TokenStream: Crash
 
-[ ($compiler == dartk || $compiler == dartkb) && $runtime == vm ]
-dart/appjit_cha_deopt_test: Pass, RuntimeError # Issue 34627
-
 # Enabling of dartk for sim{arm,arm64,dbc64} revelaed these test failures, which
 # are to be triaged.  Isolate tests are skipped on purpose due to the usage of
 # batch mode.
diff --git a/runtime/vm/class_finalizer.cc b/runtime/vm/class_finalizer.cc
index 09fa482..847a4a1 100644
--- a/runtime/vm/class_finalizer.cc
+++ b/runtime/vm/class_finalizer.cc
@@ -1760,6 +1760,11 @@
   Thread* T = Thread::Current();
   Zone* Z = T->zone();
   Isolate* I = T->isolate();
+
+  // Prevent background compiler from adding deferred classes or canonicalizing
+  // new types while classes are being sorted and type hashes are modified.
+  BackgroundCompiler::Stop(I);
+
   ClassTable* table = I->class_table();
   intptr_t num_cids = table->NumCids();
   intptr_t* old_to_new_cid = new intptr_t[num_cids];