Skip to content

Commit 67e9daf

Browse files
edusperonirigor789
andauthored
fix(workers): invalidate cached isolates on dispose (#1704)
Co-authored-by: Igor Randjelovic <[email protected]>
1 parent d6d085e commit 67e9daf

File tree

12 files changed

+88
-0
lines changed

12 files changed

+88
-0
lines changed

test-app/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ add_library(
147147
src/main/cpp/DirectBuffer.cpp
148148
src/main/cpp/FieldAccessor.cpp
149149
src/main/cpp/File.cpp
150+
src/main/cpp/IsolateDisposer.cpp
150151
src/main/cpp/JEnv.cpp
151152
src/main/cpp/DesugaredInterfaceCompanionClassNameResolver.cpp
152153
src/main/cpp/JType.cpp

test-app/runtime/src/main/cpp/ArgConverter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,12 @@ Local<String> ArgConverter::ConvertToV8UTF16String(Isolate* isolate, const u16st
293293
return String::NewFromTwoByte(isolate, ((const uint16_t*) utf16string.data())).ToLocalChecked();
294294
}
295295

296+
void ArgConverter::onDisposeIsolate(Isolate* isolate) {
297+
auto itFound = s_type_long_operations_cache.find(isolate);
298+
if (itFound != s_type_long_operations_cache.end()) {
299+
delete itFound->second;
300+
s_type_long_operations_cache.erase(itFound);
301+
}
302+
}
303+
296304
std::map<Isolate*, ArgConverter::TypeLongOperationsCache*> ArgConverter::s_type_long_operations_cache;

test-app/runtime/src/main/cpp/ArgConverter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class ArgConverter {
4343

4444
static v8::Local<v8::String> ConvertToV8UTF16String(v8::Isolate* isolate, const std::u16string& utf16string);
4545

46+
static void onDisposeIsolate(v8::Isolate* isolate);
47+
4648
private:
4749

4850
// TODO: plamen5kov: rewrite logic for java long number operations in javascript (java long -> javascript number operations check)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Created by Eduardo Speroni on 3/4/22.
3+
//
4+
5+
#include "IsolateDisposer.h"
6+
#include "ArgConverter.h"
7+
#include "MetadataNode.h"
8+
#include "V8GlobalHelpers.h"
9+
#include <console/Console.h>
10+
11+
12+
namespace tns {
13+
void disposeIsolate(v8::Isolate *isolate) {
14+
tns::ArgConverter::onDisposeIsolate(isolate);
15+
tns::MetadataNode::onDisposeIsolate(isolate);
16+
tns::V8GlobalHelpers::onDisposeIsolate(isolate);
17+
tns::Console::onDisposeIsolate(isolate);
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Created by Eduardo Speroni on 3/4/22.
3+
//
4+
5+
#ifndef TEST_APP_ISOLATEDISPOSER_H
6+
#define TEST_APP_ISOLATEDISPOSER_H
7+
#include "v8.h"
8+
9+
namespace tns {
10+
void disposeIsolate(v8::Isolate* isolate);
11+
}
12+
13+
#endif //TEST_APP_ISOLATEDISPOSER_H

test-app/runtime/src/main/cpp/MetadataNode.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,32 @@ std::string MetadataNode::GetJniClassName(MetadataEntry entry) {
21762176
return fullClassName;
21772177
}
21782178

2179+
void MetadataNode::onDisposeIsolate(Isolate* isolate) {
2180+
{
2181+
auto it = s_metadata_node_cache.find(isolate);
2182+
if (it != s_metadata_node_cache.end()) {
2183+
delete it->second;
2184+
s_metadata_node_cache.erase(it);
2185+
}
2186+
}
2187+
{
2188+
auto it = s_arrayObjectTemplates.find(isolate);
2189+
if (it != s_arrayObjectTemplates.end()) {
2190+
delete it->second;
2191+
s_arrayObjectTemplates.erase(it);
2192+
}
2193+
}
2194+
{
2195+
for (auto it = s_treeNode2NodeCache.begin(); it != s_treeNode2NodeCache.end(); it++) {
2196+
auto it2 = it->second->m_poCtorCachePerIsolate.find(isolate);
2197+
if(it2 != it->second->m_poCtorCachePerIsolate.end()) {
2198+
delete it2->second;
2199+
it->second->m_poCtorCachePerIsolate.erase(it2);
2200+
}
2201+
}
2202+
}
2203+
}
2204+
21792205
string MetadataNode::TNS_PREFIX = "com/tns/gen/";
21802206
MetadataReader MetadataNode::s_metadataReader;
21812207
std::map<std::string, MetadataNode*> MetadataNode::s_name2NodeCache;

test-app/runtime/src/main/cpp/MetadataNode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class MetadataNode {
5858
static MetadataNode* GetOrCreate(const std::string& className);
5959

6060
static std::string GetTypeMetadataName(v8::Isolate* isolate, v8::Local<v8::Value>& value);
61+
62+
static void onDisposeIsolate(v8::Isolate* isolate);
6163
private:
6264
struct MethodCallbackData;
6365

test-app/runtime/src/main/cpp/Runtime.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "sys/system_properties.h"
3030
#include "ManualInstrumentation.h"
3131
#include <snapshot_blob.h>
32+
#include "IsolateDisposer.h"
3233

3334
#ifdef APPLICATION_IN_DEBUG
3435
#include "JsV8InspectorClient.h"
@@ -810,6 +811,7 @@ bool Runtime::RunExtraCode(Isolate* isolate, Local<Context> context, const char*
810811
void Runtime::DestroyRuntime() {
811812
s_id2RuntimeCache.erase(m_id);
812813
s_isolate2RuntimesCache.erase(m_isolate);
814+
tns::disposeIsolate(m_isolate);
813815
}
814816

815817
Local<Context> Runtime::GetContext() {

test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,7 @@ bool tns::V8SetPrivateValue(Isolate* isolate, const Local<Object>& obj, const Lo
141141

142142
return res.FromMaybe(false);
143143
}
144+
145+
void tns::V8GlobalHelpers::onDisposeIsolate(Isolate* isolate) {
146+
isolateToPersistentSmartJSONStringify.erase(isolate);
147+
}

test-app/runtime/src/main/cpp/V8GlobalHelpers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ v8::Local<v8::String> JsonStringifyObject(v8::Isolate* isolate, v8::Handle<v8::V
1313
bool V8GetPrivateValue(v8::Isolate* isolate, const v8::Local<v8::Object>& obj, const v8::Local<v8::String>& propName, v8::Local<v8::Value>& out);
1414

1515
bool V8SetPrivateValue(v8::Isolate* isolate, const v8::Local<v8::Object>& obj, const v8::Local<v8::String>& propName, const v8::Local<v8::Value>& value);
16+
17+
namespace V8GlobalHelpers {
18+
void onDisposeIsolate(v8::Isolate* isolate);
19+
}
1620
}
1721

1822
#endif /* V8GLOBALHELPERS_H_ */

0 commit comments

Comments
 (0)