@@ -581,9 +581,6 @@ class IsolateSpawnState {
581581 IsolateGroup* group);
582582 ~IsolateSpawnState ();
583583
584- Isolate* isolate () const { return isolate_; }
585- void set_isolate (Isolate* value) { isolate_ = value; }
586-
587584 Dart_Port parent_port () const { return parent_port_; }
588585 Dart_Port origin_id () const { return origin_id_; }
589586 Dart_Port on_exit_port () const { return on_exit_port_; }
@@ -608,7 +605,6 @@ class IsolateSpawnState {
608605 IsolateGroup* isolate_group () const { return isolate_group_; }
609606
610607 private:
611- Isolate* isolate_ = nullptr ;
612608 Dart_Port parent_port_;
613609 Dart_Port origin_id_ = ILLEGAL_PORT;
614610 Dart_Port on_exit_port_;
@@ -880,12 +876,14 @@ class SpawnIsolateTask : public ThreadPool::Task {
880876 return ;
881877 }
882878
883- state_->set_isolate (child);
884879 if (state_->origin_id () != ILLEGAL_PORT) {
885880 // origin_id is set to parent isolate main port id when spawning via
886881 // spawnFunction.
887882 child->set_origin_id (state_->origin_id ());
888883 }
884+ bool errors_are_fatal = state_->errors_are_fatal ();
885+ Dart_Port on_error_port = state_->on_error_port ();
886+ Dart_Port on_exit_port = state_->on_exit_port ();
889887
890888 bool success = true ;
891889 {
@@ -895,18 +893,22 @@ class SpawnIsolateTask : public ThreadPool::Task {
895893 HandleScope hs (thread);
896894
897895 success = EnqueueEntrypointInvocationAndNotifySpawner (thread);
896+
897+ // Destruction of [IsolateSpawnState] may cause destruction of [Message]
898+ // which make need to delete persistent handles, so explicitly delete it
899+ // now while we are in the right safepoint state.
900+ state_ = nullptr ;
898901 }
899902
900903 if (!success) {
901- state_ = nullptr ;
902904 Dart_ShutdownIsolate ();
903905 return ;
904906 }
905907
906908 // All preconditions are met for this to always succeed.
907909 char * error = nullptr ;
908- if (!Dart_RunLoopAsync (state_-> errors_are_fatal (), state_-> on_error_port () ,
909- state_-> on_exit_port (), &error)) {
910+ if (!Dart_RunLoopAsync (errors_are_fatal, on_error_port, on_exit_port ,
911+ &error)) {
910912 FATAL (" Dart_RunLoopAsync() failed: %s. Please file a Dart VM bug report." ,
911913 error);
912914 }
@@ -1030,12 +1032,13 @@ class SpawnIsolateTask : public ThreadPool::Task {
10301032 // isolate group).
10311033 if (has_current_isolate) {
10321034 ASSERT (IsolateGroup::Current () == state_->isolate_group ());
1035+ TransitionNativeToVM transition (Thread::Current ());
10331036 state_ = nullptr ;
10341037 } else if (state_->isolate_group () != nullptr ) {
10351038 ASSERT (IsolateGroup::Current () == nullptr );
10361039 const bool kBypassSafepoint = false ;
10371040 const bool result = Thread::EnterIsolateGroupAsHelper (
1038- state_->isolate_group (), Thread::kUnknownTask , kBypassSafepoint );
1041+ state_->isolate_group (), Thread::kSpawnTask , kBypassSafepoint );
10391042 ASSERT (result);
10401043 state_ = nullptr ;
10411044 Thread::ExitIsolateGroupAsHelper (kBypassSafepoint );
0 commit comments