Skip to content

Commit

Permalink
fix: delay isolate disposal when isolate is in use
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed Jun 12, 2023
1 parent 1e7c17d commit 5a6c2ee
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion NativeScript/runtime/Runtime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@
SimpleAllocator allocator_;
NSDictionary* AppPackageJson = nil;

void DisposeIsolateWhenPossible(Isolate* isolate) {
// most of the time, this will never delay disposal
// occasionally this can happen when the runtime is destroyed by actions of its own isolate
// as an example: isolate calls exit(0), which in turn destroys the Runtime unique_ptr
// another scenario is when embedding nativescript, if the embedder deletes the runtime as a result of a callback from JS
// in the case of exit(0), the app will die before actually disposing the isolate, which isn't a problem
if (isolate->IsInUse()) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_MSEC)),
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
DisposeIsolateWhenPossible(isolate);
});
} else {
isolate->Dispose();
}
}

void Runtime::Initialize() {
MetaFile::setInstance(RuntimeConfig.MetadataPtr);
}
Expand Down Expand Up @@ -82,7 +98,7 @@
this->isolate_->SetData(Constants::RUNTIME_SLOT, nullptr);
}

this->isolate_->Dispose();
DisposeIsolateWhenPossible(this->isolate_);

currentRuntime_ = nullptr;
}
Expand Down

0 comments on commit 5a6c2ee

Please sign in to comment.