From 72f00941f638e47c1359afcc1599d575acaf65c8 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 9 Feb 2023 09:56:20 +0100 Subject: [PATCH] src: allow optional Isolate termination in node::Stop() --- src/env.cc | 5 +++-- src/env.h | 2 +- src/node.cc | 6 +++++- src/node.h | 8 ++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/env.cc b/src/env.cc index 692a344703a196..65d0bed9adcb3d 100644 --- a/src/env.cc +++ b/src/env.cc @@ -911,10 +911,11 @@ void Environment::InitializeLibuv() { StartProfilerIdleNotifier(); } -void Environment::ExitEnv() { +void Environment::ExitEnv(StopFlags flags) { // Should not access non-thread-safe methods here. set_stopping(true); - isolate_->TerminateExecution(); + if ((flags & StopFlags::kDoNotTerminateIsolate) == 0) + isolate_->TerminateExecution(); SetImmediateThreadsafe([](Environment* env) { env->set_can_call_into_js(false); uv_stop(env->event_loop()); diff --git a/src/env.h b/src/env.h index c2eb764e740400..a383523e7479c5 100644 --- a/src/env.h +++ b/src/env.h @@ -636,7 +636,7 @@ class Environment : public MemoryRetainer { void RegisterHandleCleanups(); void CleanupHandles(); void Exit(ExitCode code); - void ExitEnv(); + void ExitEnv(StopFlags flags); // Register clean-up cb to be called on environment destruction. inline void RegisterHandleCleanup(uv_handle_t* handle, diff --git a/src/node.cc b/src/node.cc index f92be4b089db87..152b5fd1dfd5c9 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1254,7 +1254,11 @@ int Start(int argc, char** argv) { } int Stop(Environment* env) { - env->ExitEnv(); + return Stop(env, StopFlags::kNoFlags); +} + +int Stop(Environment* env, StopFlags flags) { + env->ExitEnv(flags); return 0; } diff --git a/src/node.h b/src/node.h index 48477e970829c3..43d418c567168b 100644 --- a/src/node.h +++ b/src/node.h @@ -306,9 +306,17 @@ class NODE_EXTERN InitializationResult { // better suited for a public embedder API. NODE_EXTERN int Start(int argc, char* argv[]); +enum StopFlags { + kNoFlags = 0, + // Do not explicitly terminate the Isolate + // when exiting the Environment. + kDoNotTerminateIsolate = 1 << 0, +}; + // Tear down Node.js while it is running (there are active handles // in the loop and / or actively executing JavaScript code). NODE_EXTERN int Stop(Environment* env); +NODE_EXTERN int Stop(Environment* env, StopFlags flags); // Set up per-process state needed to run Node.js. This will consume arguments // from argv, fill exec_argv, and possibly add errors resulting from parsing