From 5a6c2ee5efa0c557c94ae56da0d3b3a31911d1b8 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 12 Jun 2023 10:09:44 -0300 Subject: [PATCH] fix: delay isolate disposal when isolate is in use --- NativeScript/runtime/Runtime.mm | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/NativeScript/runtime/Runtime.mm b/NativeScript/runtime/Runtime.mm index 63f61924..3f132be0 100644 --- a/NativeScript/runtime/Runtime.mm +++ b/NativeScript/runtime/Runtime.mm @@ -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); } @@ -82,7 +98,7 @@ this->isolate_->SetData(Constants::RUNTIME_SLOT, nullptr); } - this->isolate_->Dispose(); + DisposeIsolateWhenPossible(this->isolate_); currentRuntime_ = nullptr; }