Skip to content

Commit aa8dfe9

Browse files
committed
Add custom exit process handler for node 12 or superior.
1 parent 1a8cc89 commit aa8dfe9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

source/loaders/node_loader/source/node_loader_impl.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3562,6 +3562,26 @@ void *node_loader_impl_register(void *node_impl_ptr, void *env_ptr, void *functi
35623562
}
35633563
}
35643564

3565+
/* Set up the process exit handler */
3566+
#if NODE_MAJOR_VERSION >= 12 || (NODE_MAJOR_VERSION == 12 && NODE_MINOR_VERSION >= 13)
3567+
{
3568+
v8::Isolate *isolate = v8::Isolate::GetCurrent();
3569+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
3570+
node::Environment *nodeEnv = node::GetCurrentEnvironment(context);
3571+
auto handler = [&](node::Environment *nodeEnv, int exit_code) {
3572+
nodeEnv->set_can_call_into_js(false);
3573+
nodeEnv->stop_sub_worker_contexts();
3574+
v8::DisposePlatform();
3575+
uv_library_shutdown();
3576+
if (exit_code != 0)
3577+
{
3578+
exit(exit_code); // Not sure about this anyway
3579+
}
3580+
};
3581+
node::SetProcessExitHandler(nodeEnv, handler);
3582+
}
3583+
#endif
3584+
35653585
/* Run test function, this one can be called without thread safe mechanism */
35663586
/* because it is run already in the correct V8 thread */
35673587
#if (!defined(NDEBUG) || defined(DEBUG) || defined(_DEBUG) || defined(__DEBUG) || defined(__DEBUG__))

0 commit comments

Comments
 (0)