Skip to content

Commit 260ab8d

Browse files
hs0225daeyeon
authored andcommitted
fix: fix multiple Node::Start executions in a process
Signed-off-by: Hosung Kim hs852.kim@samsung.com
1 parent aa3d8dc commit 260ab8d

File tree

6 files changed

+43
-11
lines changed

6 files changed

+43
-11
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ class Runtime::Internal {
4848

4949
std::pair<bool, int> Init(int argc, char** argv) {
5050
if (state_ != State::kNotInitialized) {
51-
LWNODE_DEV_LOG("[Runtime::Init] already initialized");
51+
LWNODE_DEV_LOG("[Runtime::Internal::Init] already initialized");
5252
return std::make_pair(false, -1);
5353
}
5454

55-
LWNODE_DEV_LOG("[Runtime::Init]");
55+
LWNODE_DEV_LOG("[Runtime::Internal::Init]");
5656
state_ = State::kInitialized;
5757

5858
// Set sendMessageSync callback to isolate context embedder data.
@@ -72,12 +72,12 @@ class Runtime::Internal {
7272

7373
int Run() {
7474
if (state_ != State::kInitialized) {
75-
LWNODE_DEV_LOG("[Runtime::Run] not initialized");
75+
LWNODE_DEV_LOG("[Runtime::Internal::Run] not initialized");
7676
return -1;
7777
}
7878

7979
CHECK_NOT_NULL(instance_);
80-
LWNODE_DEV_LOG("[Runtime::Run]");
80+
LWNODE_DEV_LOG("[Runtime::Internal::Run]");
8181
state_ = State::kRunning;
8282

8383
int result = runner_.Run(*instance_);
@@ -89,26 +89,26 @@ class Runtime::Internal {
8989

9090
void Stop() {
9191
if (state_ != State::kRunning) {
92-
LWNODE_DEV_LOG("[Runtime::Stop] already stopped");
92+
LWNODE_DEV_LOG("[Runtime::Internal::Stop] already stopped");
9393
return;
9494
}
9595

9696
CHECK_NOT_NULL(instance_);
97-
LWNODE_DEV_LOG("[Runtime::Stop]");
97+
LWNODE_DEV_LOG("[Runtime::Internal::Stop]");
9898
state_ = State::kStopped;
9999

100100
runner_.Stop();
101101
}
102102

103103
void Free() {
104104
if (state_ != State::kStopped && state_ != State::kInitialized) {
105-
LWNODE_DEV_LOG("[Runtime::Free] not stopped");
105+
LWNODE_DEV_LOG("[Runtime::Internal::Free] not stopped");
106106
return;
107107
}
108108

109109
state_ = State::kReleased;
110110
if (instance_) {
111-
LWNODE_DEV_LOG("[Runtime::Free]");
111+
LWNODE_DEV_LOG("[Runtime::Internal::Free]");
112112
DisposeNode(instance_);
113113
}
114114

deps/node/src/node.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,10 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
831831
std::vector<std::string>* exec_argv,
832832
std::vector<std::string>* errors) {
833833
// Make sure InitializeNodeWithArgs() is called only once.
834-
CHECK(!init_called.exchange(true));
834+
// CHECK(!init_called.exchange(true)); @lwnode
835+
if (node_is_initialized) {
836+
return 0;
837+
}
835838

836839
// Initialize node_start_time to get relative uptime.
837840
per_process::node_start_time = uv_hrtime();

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 0
22-
#define LWNODE_VERSION_TAG "v1.0.0"
21+
#define LWNODE_VERSION_PATCH 1
22+
#define LWNODE_VERSION_TAG "v1.0.1"

src/api/engine.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Platform* Platform::GetInstance() {
4040
void Platform::Dispose() {
4141
LWNODE_CALL_TRACE_GC_START();
4242
// s_platform is freed in Escargot::PlatformBridge
43+
s_platform = nullptr;
4344
LWNODE_CALL_TRACE_GC_END();
4445
}
4546

test/embedding/embedtest.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,30 @@ TEST0(Embedtest, MessagePort2_Post_Many_JS_First) {
7373

7474
EXPECT_EQ(count1, 10);
7575
}
76+
77+
TEST0(Embedtest, Restart) {
78+
int count = 0;
79+
for (int i = 0; i < 3; i++) {
80+
auto runtime = std::make_shared<lwnode::Runtime>();
81+
82+
std::promise<void> promise;
83+
std::future<void> init_future = promise.get_future();
84+
const char* script = "test/embedding/test-21-runtime-hello.js";
85+
std::string path = (std::filesystem::current_path() / script).string();
86+
87+
char* args[] = {const_cast<char*>(""), const_cast<char*>(path.c_str())};
88+
89+
std::thread worker = std::thread(
90+
[&](std::promise<void>&& promise) mutable {
91+
std::cout << ++count << " Start " << std::endl;
92+
runtime->Start(COUNT_OF(args), args, std::move(promise));
93+
std::cout << count << " /Start " << std::endl;
94+
},
95+
std::move(promise));
96+
97+
init_future.wait();
98+
worker.join();
99+
std::this_thread::sleep_for(std::chrono::seconds(1));
100+
}
101+
EXPECT_EQ(count, 3);
102+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('hello world!');

0 commit comments

Comments
 (0)