diff --git a/src/env.cc b/src/env.cc index 837a879864c46d..045f6242e9e49e 100644 --- a/src/env.cc +++ b/src/env.cc @@ -902,10 +902,11 @@ void Environment::InitializeLibuv() { StartProfilerIdleNotifier(); } -void Environment::ExitEnv() { +void Environment::ExitEnv(StopFlags::Flags 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 673581fd0e2ef9..82f2dd1689114e 100644 --- a/src/env.h +++ b/src/env.h @@ -628,7 +628,7 @@ class Environment : public MemoryRetainer { void RegisterHandleCleanups(); void CleanupHandles(); void Exit(int code); - void ExitEnv(); + void ExitEnv(StopFlags::Flags 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 7f84c6be74377d..e7724ba9c4155e 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1231,7 +1231,11 @@ int Start(int argc, char** argv) { } int Stop(Environment* env) { - env->ExitEnv(); + return Stop(env, StopFlags::kNoFlags); +} + +int Stop(Environment* env, StopFlags::Flags flags) { + env->ExitEnv(flags); return 0; } diff --git a/src/node.h b/src/node.h index 812c4943cdc7d7..02ce506e427251 100644 --- a/src/node.h +++ b/src/node.h @@ -274,6 +274,15 @@ enum Flags : uint64_t { // TODO(addaleax): Make this the canonical name, as it is more descriptive. namespace ProcessInitializationFlags = ProcessFlags; +namespace StopFlags { +enum Flags : uint32_t { + kNoFlags = 0, + // Do not explicitly terminate the Isolate + // when exiting the Environment. + kDoNotTerminateIsolate = 1 << 0, +}; +} // namespace StopFlags + class NODE_EXTERN InitializationResult { public: virtual ~InitializationResult(); @@ -310,6 +319,7 @@ NODE_EXTERN int Start(int argc, char* argv[]); // 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 flags); // This runs a subset of the initialization performed by // InitializeOncePerProcess(), which supersedes this function.