From 7a8f59f1d6860d1abda45d7f2aaad0f188f71b83 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 29 Mar 2020 15:07:00 +0200 Subject: [PATCH] embedding: make Stop() stop Workers This makes sense given that terminating execution of the parent thread this way likely also is supposed to stop all running Worker threads spawned by it. Backport-PR-URL: https://github.com/nodejs/node/pull/35241 PR-URL: https://github.com/nodejs/node/pull/32531 Reviewed-By: Colin Ihrig Reviewed-By: Gireesh Punathil Reviewed-By: James M Snell --- src/api/environment.cc | 3 +-- src/env.cc | 3 ++- src/env.h | 2 +- src/node.cc | 2 +- src/node.h | 7 ++++--- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index cf10aa398f0744..e47c5d42292615 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -714,8 +714,7 @@ ThreadId AllocateEnvironmentThreadId() { } void DefaultProcessExitHandler(Environment* env, int exit_code) { - env->set_can_call_into_js(false); - env->stop_sub_worker_contexts(); + Stop(env); DisposePlatform(); exit(exit_code); } diff --git a/src/env.cc b/src/env.cc index b2e4721b96db1b..5d29deedd70ba8 100644 --- a/src/env.cc +++ b/src/env.cc @@ -523,9 +523,10 @@ void Environment::InitializeLibuv(bool start_profiler_idle_notifier) { } } -void Environment::ExitEnv() { +void Environment::Stop() { set_can_call_into_js(false); set_stopping(true); + stop_sub_worker_contexts(); isolate_->TerminateExecution(); SetImmediateThreadsafe([](Environment* env) { uv_stop(env->event_loop()); }); } diff --git a/src/env.h b/src/env.h index 13c562133ce44a..e9e760bbb8b3b0 100644 --- a/src/env.h +++ b/src/env.h @@ -913,7 +913,7 @@ class Environment : public MemoryRetainer { void RegisterHandleCleanups(); void CleanupHandles(); void Exit(int code); - void ExitEnv(); + void Stop(); // 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 46e8f74cc286f7..00ae36cc0fe669 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1035,7 +1035,7 @@ int Start(int argc, char** argv) { } int Stop(Environment* env) { - env->ExitEnv(); + env->Stop(); return 0; } diff --git a/src/node.h b/src/node.h index 74fe59097ff673..eea39ed44eda0c 100644 --- a/src/node.h +++ b/src/node.h @@ -218,7 +218,8 @@ class Environment; 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). +// in the loop and / or actively executing JavaScript code). This also stops +// all Workers that may have been started earlier. NODE_EXTERN int Stop(Environment* env); // TODO(addaleax): Officially deprecate this and replace it with something @@ -457,8 +458,8 @@ NODE_EXTERN void FreeEnvironment(Environment* env); // It receives the Environment* instance and the exit code as arguments. // This could e.g. call Stop(env); in order to terminate execution and stop // the event loop. -// The default handler disposes of the global V8 platform instance, if one is -// being used, and calls exit(). +// The default handler calls Stop(), disposes of the global V8 platform +// instance, if one is being used, and calls exit(). NODE_EXTERN void SetProcessExitHandler( Environment* env, std::function&& handler);