Skip to content

Commit

Permalink
fix: prevent crashes during isolate disposal
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed Nov 8, 2023
1 parent e954c78 commit 3d70c11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions NativeScript/runtime/Caches.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ class Caches {
return isolateId_;
}

inline void InvalidateIsolate() {
isolateId_ = -1;
}

inline bool IsValid() {
return isolateId_ != -1;
}

void SetContext(v8::Local<v8::Context> context);
v8::Local<v8::Context> GetContext();

Expand Down
4 changes: 3 additions & 1 deletion NativeScript/runtime/Runtime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
// it terminates execution
SpinLock lock(isolatesMutex_);
Runtime::isolates_.erase(std::remove(Runtime::isolates_.begin(), Runtime::isolates_.end(), this->isolate_), Runtime::isolates_.end());
Caches::Get(isolate_)->InvalidateIsolate();
}
this->isolate_->TerminateExecution();

Expand Down Expand Up @@ -306,7 +307,8 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
// speedup lookup by avoiding locking if thread locals match
// note: this can be a problem when the Runtime is deleted in a different thread that it was created
// which could happen under some specific embedding scenarios
if (Isolate::TryGetCurrent() == isolate || (currentRuntime_ != nullptr && currentRuntime_->GetIsolate() == isolate)) {
if ((Isolate::TryGetCurrent() == isolate || (currentRuntime_ != nullptr && currentRuntime_->GetIsolate() == isolate))
&& Caches::Get((Isolate*)isolate)->IsValid()) {
return true;
}
SpinLock lock(isolatesMutex_);
Expand Down

0 comments on commit 3d70c11

Please sign in to comment.