diff --git a/NativeScript/runtime/Runtime.mm b/NativeScript/runtime/Runtime.mm index 3e15883f..67c429a5 100644 --- a/NativeScript/runtime/Runtime.mm +++ b/NativeScript/runtime/Runtime.mm @@ -393,8 +393,14 @@ void DisposeIsolateWhenPossible(Isolate* isolate, int gateId) { } void Runtime::Init(Isolate* isolate, bool isWorker) { - std::shared_ptr cache = - Caches::Init(isolate, nextIsolateId.fetch_add(1, std::memory_order_relaxed)); + std::shared_ptr cache; + { + // CreateIsolate has already registered the isolate, so NotifyMemoryPressure + // can read its Caches slot from another thread. It reads under the registry + // lock, so the slot is published under it too. + SpinLock lock(isolatesMutex_); + cache = Caches::Init(isolate, nextIsolateId.fetch_add(1, std::memory_order_relaxed)); + } // Before anything can build an IsolateWrapper for this isolate. IsolateGates::Open(cache->getGateId()); cache->isWorker = isWorker;