Skip to content

Commit

Permalink
src: allow optional Isolate termination in node::Stop()
Browse files Browse the repository at this point in the history
PR-URL: #46583
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
codebytere authored Feb 17, 2023
1 parent 0084fc7 commit c566a04
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -911,10 +911,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());
Expand Down
2 changes: 1 addition & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ class Environment : public MemoryRetainer {
void RegisterHandleCleanups();
void CleanupHandles();
void Exit(ExitCode 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,
Expand Down
6 changes: 5 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 flags) {
env->ExitEnv(flags);
return 0;
}

Expand Down
10 changes: 10 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ enum Flags : uint32_t {
} // namespace ProcessInitializationFlags
namespace ProcessFlags = ProcessInitializationFlags; // Legacy alias.

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();
Expand Down Expand Up @@ -309,6 +318,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);

// 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
Expand Down

0 comments on commit c566a04

Please sign in to comment.