Skip to content

Commit 7a4469e

Browse files
committed
fix(runtime): tell a runtime's thread by its own thread id
A single thread-local marker named one isolate per thread, so a second runtime started on the same thread replaced it, and either one's teardown cleared it for both. Each runtime now records its thread where it binds its event loop, and the check compares it with the calling thread through the isolate's runtime.
1 parent b0d95ef commit 7a4469e

3 files changed

Lines changed: 17 additions & 15 deletions

File tree

‎NativeScript/runtime/DataWrapper.h‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ class WorkerInspectorClient;
2020

2121
namespace tns {
2222

23-
// The isolate whose runtime runs on this thread (its event loop's thread),
24-
// while the runtime is bound here. JS can also run on other threads, any that
25-
// take the isolate's Locker.
26-
inline thread_local v8::Isolate* runtimeThreadIsolate = nullptr;
27-
28-
inline bool OnRuntimeThreadOf(v8::Isolate* isolate) {
29-
return isolate != nullptr && runtimeThreadIsolate == isolate;
30-
}
23+
// Whether the calling thread is the one `isolate`'s runtime runs on (its event
24+
// loop's thread). JS can also run on other threads, any that take the
25+
// isolate's Locker. Several runtimes may share a thread. Defined in Runtime.mm.
26+
bool OnRuntimeThreadOf(v8::Isolate* isolate);
3127

3228
class PrimitiveDataWrapper;
3329
struct ObjectWeakCallbackState;

‎NativeScript/runtime/Runtime.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef Runtime_h
22
#define Runtime_h
33

4+
#include <pthread.h>
45
#include <atomic>
56
#include <optional>
67

@@ -51,6 +52,8 @@ class Runtime {
5152

5253
inline CFRunLoopRef RuntimeLoop() { return runtimeLoop_; }
5354

55+
inline bool IsOnRuntimeThread() const { return pthread_equal(runtimeThread_, pthread_self()) != 0; }
56+
5457
/*
5558
* Scheduler bound to this runtime's runloop. Producers on any thread post;
5659
* entries run only on the runtime's home thread.
@@ -192,6 +195,7 @@ class Runtime {
192195
std::unique_ptr<ModuleInternal> moduleInternal_;
193196
int workerId_;
194197
CFRunLoopRef runtimeLoop_;
198+
pthread_t runtimeThread_;
195199
std::shared_ptr<EventLoop> eventLoop_;
196200
// Drains unhandled promise rejections once per runloop turn
197201
// (kCFRunLoopBeforeWaiting). Torn down before isolate disposal in ~Runtime.

‎NativeScript/runtime/Runtime.mm‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ void DisposeIsolateWhenPossible(Isolate* isolate, int gateId) {
266266
#endif
267267
Isolate* isolate = Isolate::New(create_params);
268268
runtimeLoop_ = CFRunLoopGetCurrent();
269+
runtimeThread_ = pthread_self();
269270
// v8 already asked for this isolate's task runner during Isolate::New, so
270271
// the registry may hold an unbound loop - or a stopped one, when a worker
271272
// isolate reuses a disposed isolate's address. Refresh, then attach to this
@@ -278,16 +279,12 @@ void DisposeIsolateWhenPossible(Isolate* isolate, int gateId) {
278279
eventLoop_->BindToCurrentThread();
279280
isolate->SetData(Constants::RUNTIME_SLOT, this);
280281
isolate_ = isolate;
281-
tns::runtimeThreadIsolate = isolate;
282282
}
283283

284284
Runtime::~Runtime() {
285285
if (!initialized_) {
286286
// Never published: no other thread can know this isolate, and it holds no
287287
// caches, gate or wrappers.
288-
if (tns::runtimeThreadIsolate == isolate_) {
289-
tns::runtimeThreadIsolate = nullptr;
290-
}
291288
isolate_->Dispose();
292289
NativeScriptPlatform::Instance()->IsolateDisposed(isolate_, eventLoop_);
293290
currentRuntime_ = nullptr;
@@ -373,9 +370,6 @@ void DisposeIsolateWhenPossible(Isolate* isolate, int gateId) {
373370
Caches::Remove(this->isolate_);
374371

375372
this->isolate_->SetData(Constants::RUNTIME_SLOT, nullptr);
376-
if (tns::runtimeThreadIsolate == this->isolate_) {
377-
tns::runtimeThreadIsolate = nullptr;
378-
}
379373

380374
// Last, and under the Locker: from here on nothing reads a wrapper through
381375
// a JS object, so threads that never pinned may free theirs without the
@@ -395,6 +389,14 @@ void DisposeIsolateWhenPossible(Isolate* isolate, int gateId) {
395389
currentRuntime_ = nullptr;
396390
}
397391

392+
bool OnRuntimeThreadOf(v8::Isolate* isolate) {
393+
if (isolate == nullptr) {
394+
return false;
395+
}
396+
Runtime* runtime = Runtime::GetRuntime(isolate);
397+
return runtime != nullptr && runtime->IsOnRuntimeThread();
398+
}
399+
398400
Runtime* Runtime::GetRuntime(v8::Isolate* isolate) {
399401
return static_cast<Runtime*>(isolate->GetData(Constants::RUNTIME_SLOT));
400402
}

0 commit comments

Comments
 (0)