Skip to content

Commit 710c9c4

Browse files
committed
fix: release persistent handle
Signed-off-by: Hosung Kim [email protected]
1 parent e22abcb commit 710c9c4

File tree

13 files changed

+79
-31
lines changed

13 files changed

+79
-31
lines changed

deps/node/src/lwnode/lwnode-public.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ Runtime::~Runtime() {
142142
int Runtime::Start(int argc, char** argv, std::promise<void>&& promise) {
143143
LWNODE_PERF_LOG("[Runtime::Start]");
144144
LWNODE_DEV_LOG("[Runtime] version:", LWNODE_VERSION_TAG);
145+
#if defined(NDEBUG)
146+
LWNODE_DEV_LOG("[Runtime] release mode");
147+
#else
148+
LWNODE_DEV_LOG("[Runtime] debug mode");
149+
#endif
145150

146151
internal_->runner_.SetInitPromise(std::move(promise));
147152
std::pair<bool, int> init_result = internal_->Init(argc, argv);

deps/node/src/node.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,11 +1109,13 @@ std::pair<bool, int> InitializeNode(int argc, char** argv, NodeMainInstance** ma
11091109
}
11101110

11111111
void DisposeNode(NodeMainInstance* main_instance) {
1112-
delete main_instance;
1113-
11141112
LWNode::MessageLoop::GetInstance()->dispose();
11151113

1114+
main_instance->Dispose();
1115+
11161116
TearDownOncePerProcess();
1117+
1118+
delete main_instance;
11171119
}
11181120
#endif
11191121

deps/node/src/node_main_instance.cc

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <memory>
22

3-
#include "node_main_instance.h"
43
#include "node_internals.h"
4+
#include "node_main_instance.h"
55
#include "node_options-inl.h"
66
#include "node_v8_platform-inl.h"
77
#include "util-inl.h"
@@ -92,18 +92,18 @@ NodeMainInstance::NodeMainInstance(
9292
}
9393

9494
void NodeMainInstance::Dispose() {
95-
CHECK(!owns_isolate_);
96-
platform_->DrainTasks(isolate_);
97-
}
98-
99-
NodeMainInstance::~NodeMainInstance() {
10095
if (!owns_isolate_) {
10196
return;
10297
}
98+
99+
platform_->DrainTasks(isolate_);
100+
103101
platform_->UnregisterIsolate(isolate_);
104102
isolate_->Dispose();
105103
}
106104

105+
NodeMainInstance::~NodeMainInstance() {}
106+
107107
int NodeMainInstance::Run() {
108108
Locker locker(isolate_);
109109
Isolate::Scope isolate_scope(isolate_);
@@ -158,8 +158,7 @@ int NodeMainInstance::Run() {
158158
struct sigaction act;
159159
memset(&act, 0, sizeof(act));
160160
for (unsigned nr = 1; nr < kMaxSignal; nr += 1) {
161-
if (nr == SIGKILL || nr == SIGSTOP || nr == SIGPROF)
162-
continue;
161+
if (nr == SIGKILL || nr == SIGSTOP || nr == SIGPROF) continue;
163162
act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL;
164163
CHECK_EQ(0, sigaction(nr, &act, nullptr));
165164
}
@@ -197,12 +196,12 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code) {
197196
CHECK(!context.IsEmpty());
198197
Context::Scope context_scope(context);
199198

200-
DeleteFnPtr<Environment, FreeEnvironment> env { CreateEnvironment(
201-
isolate_data_.get(),
202-
context,
203-
args_,
204-
exec_args_,
205-
EnvironmentFlags::kDefaultFlags) };
199+
DeleteFnPtr<Environment, FreeEnvironment> env{
200+
CreateEnvironment(isolate_data_.get(),
201+
context,
202+
args_,
203+
exec_args_,
204+
EnvironmentFlags::kDefaultFlags)};
206205

207206
if (*exit_code != 0) {
208207
return env;

deps/node/src/node_main_lw.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ int main(int argc, char* argv[]) {
7272
setvbuf(stderr, nullptr, _IONBF, 0);
7373

7474
lwnode::Runtime runtime;
75-
std::pair<bool, int> init_result; // early_return, exit_code
7675

7776
// FIXME: Fix Runtime::Init() call to ensure environment initialization
7877
// before running the loop, Runtime::Run(). This workaround passes a

include/lwnode/lwnode-version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818

1919
#define LWNODE_VERSION_MAJOR 1
2020
#define LWNODE_VERSION_MINOR 0
21-
#define LWNODE_VERSION_PATCH 1
22-
#define LWNODE_VERSION_TAG "v1.0.1"
21+
#define LWNODE_VERSION_PATCH 2
22+
#define LWNODE_VERSION_TAG "v1.0.2"

packaging/lwnode.spec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ echo $CFLAGS
126126

127127
# building lwnode executable
128128

129-
CFLAGS+=" -fno-lto -DMAX_HEAP_SECTS=7680 "
130-
CXXFLAGS+=" -fno-lto -DMAX_HEAP_SECTS=7680 "
129+
CFLAGS+=" -g -fno-lto -DMAX_HEAP_SECTS=7680 "
130+
CXXFLAGS+=" -g -fno-lto -DMAX_HEAP_SECTS=7680 "
131131
LDFLAGS+=" -fno-lto "
132132

133133
./tools/envinfo.sh
@@ -141,7 +141,7 @@ LDFLAGS+=" -fno-lto "
141141
--arch='%{tizen_arch}' \
142142
%{?lib_type_config} %{?asan_config} \
143143
%{?external_libs_config} %{?jsengine_config} \
144-
--debug --escargot-debugger
144+
--debug
145145
ninja -C %{target_src} %{target}
146146
%undefine target_src
147147
%endif

src/api-scripts.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal(
291291
esResourceName = VAL(*source->resource_name)->value()->asString();
292292
}
293293

294-
ContextRef* esPureContext = ContextRef::create(lwIsolate->vmInstance());
294+
PersistentRefHolder<Escargot::ContextRef> esPureContext =
295+
ContextRef::create(lwIsolate->vmInstance());
295296
ScriptParserRef* parser = esPureContext->scriptParser();
296297
ScriptParserRef::InitializeScriptResult result = parser->initializeScript(
297298
esSource, esResourceName, source->GetResourceOptions().IsModule());
@@ -303,6 +304,8 @@ MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal(
303304

304305
lwIsolate->SetPendingExceptionAndMessage(r.error.get(), r.stackTrace);
305306
lwIsolate->ReportPendingMessages();
307+
308+
esPureContext.release();
306309
return MaybeLocal<UnboundScript>();
307310
}
308311

@@ -418,12 +421,15 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
418421
// note: expand API_HANDLE_EXCEPTION and add the resource name
419422
if (!result.isSuccessful()) {
420423
Evaluator::EvaluatorResult r;
421-
ContextRef* esPureContext = ContextRef::create(lwIsolate->vmInstance());
424+
PersistentRefHolder<Escargot::ContextRef> esPureContext =
425+
ContextRef::create(lwIsolate->vmInstance());
422426
r.error = ExceptionHelper::createErrorObject(
423427
esPureContext, result.parseErrorCode, result.parseErrorMessage);
424428

425429
lwIsolate->SetPendingExceptionAndMessage(r.error.get(), r.stackTrace);
426430
lwIsolate->ReportPendingMessages();
431+
432+
esPureContext.release();
427433
return MaybeLocal<Function>();
428434
}
429435

src/api/context.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ ContextWrap::ContextWrap(IsolateWrap* isolate,
6868
EscargotShim::Global::initGlobalObject(this);
6969

7070
RegisteredExtension::applyAll(context_);
71+
72+
isolate->AddContextClenupHook(this);
7173
}
7274

7375
void ContextWrap::initDebugger() {
@@ -109,6 +111,10 @@ void ContextWrap::Exit() {
109111
isolate_->popContext(this);
110112
}
111113

114+
void ContextWrap::Dispose() {
115+
context_.release();
116+
}
117+
112118
IsolateWrap* ContextWrap::GetIsolate() {
113119
return isolate_;
114120
}

src/api/context.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class ContextWrap : public ValueWrap {
3838
void Exit();
3939
IsolateWrap* GetIsolate();
4040

41+
void Dispose();
42+
4143
Escargot::ContextRef* get() { return context_; }
4244

4345
void SetEmbedderData(int index, ValueWrap* value);
@@ -70,7 +72,7 @@ class ContextWrap : public ValueWrap {
7072
void* getEmbedderData(int index);
7173

7274
IsolateWrap* isolate_ = nullptr;
73-
Escargot::ContextRef* context_ = nullptr;
75+
Escargot::PersistentRefHolder<Escargot::ContextRef> context_;
7476
Escargot::ObjectRef* bindingObject_ = nullptr;
7577
Escargot::ValueRef* security_token_ = nullptr;
7678

src/api/engine.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,10 @@ void Engine::dispose() {
409409
gcHeap_.release();
410410
GC_invoke_finalizers();
411411

412-
Globals::finalize();
413412
disposeExternalStrings();
413+
414+
MemoryUtil::gcFull();
415+
Globals::finalize();
414416
LWNODE_CALL_TRACE_GC_END();
415417
}
416418

src/api/isolate.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ IsolateWrap::IsolateWrap() {
278278
SymbolRef::create(StringRef::createFromUTF8(PRIVATE_VALUES.data(),
279279
PRIVATE_VALUES.length())));
280280

281-
threadManager_ = new ThreadManager();
281+
thread_manager_ = new ThreadManager();
282282

283283
// NOTE: check lock_gc_release(); is needed (and where)
284284
// lock_gc_release();
@@ -307,6 +307,10 @@ IsolateWrap::IsolateWrap() {
307307

308308
IsolateWrap::~IsolateWrap() {
309309
LWNODE_CALL_TRACE_ID(ISOWRAP, "free: %p", this);
310+
vmInstance_.release();
311+
312+
s_currentIsolate = nullptr;
313+
s_previousIsolate = nullptr;
310314
}
311315

312316
IsolateWrap* IsolateWrap::New() {
@@ -320,7 +324,15 @@ void IsolateWrap::Dispose() {
320324
// NOTE: check unlock_gc_release(); is needed (and where)
321325
// unlock_gc_release();
322326

327+
328+
privateValuesSymbol_.release();
323329
global_handles()->dispose();
330+
331+
delete thread_manager_;
332+
thread_manager_ = nullptr;
333+
334+
ReleaseContexts();
335+
324336
RegisteredExtension::unregisterAll();
325337

326338
state_ = State::Disposed;
@@ -857,4 +869,15 @@ void IsolateWrap::ReportPromiseReject(
857869
}
858870
}
859871

872+
void IsolateWrap::AddContextClenupHook(ContextWrap* context) {
873+
contexts_.push_back(context);
874+
}
875+
876+
void IsolateWrap::ReleaseContexts() {
877+
for (const auto& context : contexts_) {
878+
context->Dispose();
879+
}
880+
contexts_.clear();
881+
}
882+
860883
} // namespace EscargotShim

src/api/isolate.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,23 @@ class IsolateWrap final : public v8::internal::Isolate {
271271
Escargot::ValueRef* value,
272272
Escargot::VMInstanceRef::PromiseRejectEvent event);
273273

274-
ThreadManager* thread_manager() { return threadManager_; }
274+
ThreadManager* thread_manager() { return thread_manager_; }
275275

276276
void PerformMicrotaskCheckpoint() {
277277
v8::MicrotasksScope::PerformCheckpoint(toV8(this));
278278
}
279279

280280
State getState() { return state_; }
281281

282+
void AddContextClenupHook(ContextWrap* context);
283+
void ReleaseContexts();
284+
282285
private:
283286
IsolateWrap();
284287

285288
void InitializeGlobalSlots();
286289

290+
GCVector<ContextWrap*> contexts_;
287291
GCVector<GCManagedObject*> eternals_;
288292
GCMap<BackingStoreRef*, int, BackingStoreComparator> backingStoreCounter_;
289293

@@ -302,12 +306,12 @@ class IsolateWrap final : public v8::internal::Isolate {
302306
v8::ArrayBuffer::Allocator* array_buffer_allocator_ = nullptr;
303307
std::shared_ptr<v8::ArrayBuffer::Allocator> array_buffer_allocator_shared_;
304308

305-
VMInstanceRef* vmInstance_ = nullptr;
309+
PersistentRefHolder<VMInstanceRef> vmInstance_;
306310

307311
PersistentRefHolder<IsolateWrap> release_lock_;
308312
ValueWrap* globalSlot_[internal::Internals::kRootIndexSize]{};
309313

310-
ThreadManager* threadManager_ = nullptr;
314+
ThreadManager* thread_manager_ = nullptr;
311315

312316
v8::PromiseRejectCallback promise_reject_callback_{nullptr};
313317

src/execution/v8threads.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace EscargotShim {
2626

27-
class ThreadManager : public gc {
27+
class ThreadManager {
2828
public:
2929
void Lock();
3030
void Unlock();

0 commit comments

Comments
 (0)