Skip to content

Commit 5a6c2ee

Browse files
committed
fix: delay isolate disposal when isolate is in use
1 parent 1e7c17d commit 5a6c2ee

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

NativeScript/runtime/Runtime.mm

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@
3434
SimpleAllocator allocator_;
3535
NSDictionary* AppPackageJson = nil;
3636

37+
void DisposeIsolateWhenPossible(Isolate* isolate) {
38+
// most of the time, this will never delay disposal
39+
// occasionally this can happen when the runtime is destroyed by actions of its own isolate
40+
// as an example: isolate calls exit(0), which in turn destroys the Runtime unique_ptr
41+
// another scenario is when embedding nativescript, if the embedder deletes the runtime as a result of a callback from JS
42+
// in the case of exit(0), the app will die before actually disposing the isolate, which isn't a problem
43+
if (isolate->IsInUse()) {
44+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_MSEC)),
45+
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
46+
DisposeIsolateWhenPossible(isolate);
47+
});
48+
} else {
49+
isolate->Dispose();
50+
}
51+
}
52+
3753
void Runtime::Initialize() {
3854
MetaFile::setInstance(RuntimeConfig.MetadataPtr);
3955
}
@@ -82,7 +98,7 @@
8298
this->isolate_->SetData(Constants::RUNTIME_SLOT, nullptr);
8399
}
84100

85-
this->isolate_->Dispose();
101+
DisposeIsolateWhenPossible(this->isolate_);
86102

87103
currentRuntime_ = nullptr;
88104
}

0 commit comments

Comments
 (0)