From 21d07ada08391e7854edd9d54cd1bc8f11fb5e0f Mon Sep 17 00:00:00 2001 From: jjuzna Date: Wed, 18 May 2022 11:11:55 +0200 Subject: [PATCH 1/2] Added stop() method to java object. It breaks the internal indefinite loop and allows Node process to quit without SIGTERM. Solves bug #539 - The process refuses to quit. --- src/java.cpp | 5 +++++ src/java.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/java.cpp b/src/java.cpp index fb5cfbd5..24f61e85 100644 --- a/src/java.cpp +++ b/src/java.cpp @@ -106,6 +106,7 @@ void uvAsyncCb_dynamicProxyJsCall(uv_async_t *handle) { Nan::SetPrototypeMethod(t, "getStaticFieldValue", getStaticFieldValue); Nan::SetPrototypeMethod(t, "setStaticFieldValue", setStaticFieldValue); Nan::SetPrototypeMethod(t, "instanceOf", instanceOf); + Nan::SetPrototypeMethod(t, "stop", stop); Nan::Set(target, Nan::New("Java").ToLocalChecked(), Nan::GetFunction(t).ToLocalChecked()); @@ -1239,6 +1240,10 @@ NAN_METHOD(Java::instanceOf) { info.GetReturnValue().Set(Nan::New(res)); } +NAN_METHOD(Java::stop) { + uv_close((uv_handle_t *)&uvAsync_dynamicProxyJsCall, NULL); +} + template std::string to_string(T value) { std::ostringstream os; diff --git a/src/java.h b/src/java.h index 7f03657f..f3a0bc9d 100644 --- a/src/java.h +++ b/src/java.h @@ -56,6 +56,7 @@ class Java : public Nan::ObjectWrap { static NAN_METHOD(getStaticFieldValue); static NAN_METHOD(setStaticFieldValue); static NAN_METHOD(instanceOf); + static NAN_METHOD(stop); static NAN_GETTER(AccessorProhibitsOverwritingGetter); static NAN_SETTER(AccessorProhibitsOverwritingSetter); v8::Local ensureJvm(); From 2f8bf0b16c2f1364034ec253a7eca692b55ad8b1 Mon Sep 17 00:00:00 2001 From: Jernej Juzna Date: Thu, 28 Jul 2022 13:32:43 +0200 Subject: [PATCH 2/2] Moved start of default loop on first Java instance creation. --- src/java.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/java.cpp b/src/java.cpp index 24f61e85..990f5a9a 100644 --- a/src/java.cpp +++ b/src/java.cpp @@ -22,6 +22,7 @@ #endif threadId v8ThreadId; +bool isDefaultLoopRunning = false; std::queue queue_dynamicProxyJsCallData; uv_mutex_t uvMutex_dynamicProxyJsCall; @@ -78,9 +79,9 @@ void uvAsyncCb_dynamicProxyJsCall(uv_async_t *handle) { Nan::HandleScope scope; v8ThreadId = my_getThreadId(); + isDefaultLoopRunning = false; //init as false uv_mutex_init(&uvMutex_dynamicProxyJsCall); - uv_async_init(uv_default_loop(), &uvAsync_dynamicProxyJsCall, uvAsyncCb_dynamicProxyJsCall); v8::Local t = Nan::New(New); s_ct.Reset(t); @@ -116,6 +117,11 @@ void uvAsyncCb_dynamicProxyJsCall(uv_async_t *handle) { NAN_METHOD(Java::New) { Nan::HandleScope scope; + if (!isDefaultLoopRunning) { + uv_async_init(uv_default_loop(), &uvAsync_dynamicProxyJsCall, uvAsyncCb_dynamicProxyJsCall); + isDefaultLoopRunning = true; + } + Java *self = new Java(); self->Wrap(info.This()); @@ -1241,7 +1247,9 @@ NAN_METHOD(Java::instanceOf) { } NAN_METHOD(Java::stop) { - uv_close((uv_handle_t *)&uvAsync_dynamicProxyJsCall, NULL); + if (isDefaultLoopRunning) { + uv_close((uv_handle_t *)&uvAsync_dynamicProxyJsCall, NULL); + } } template