Skip to content

Commit cd233e3

Browse files
addaleaxMylesBorins
authored andcommitted
src: make EndStartedProfilers an exit hook
Run `EndStartedProfilers` on Environment teardown. This is part of a series of changes to make embedding easier, by requiring fewer internal methods to build a fully functioning Node.js instance. PR-URL: #30229 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Shelley Vohr <[email protected]>
1 parent 8234d04 commit cd233e3

6 files changed

+31
-18
lines changed

src/inspector_profiler.cc

+21-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "diagnosticfilename-inl.h"
55
#include "memory_tracker-inl.h"
66
#include "node_file.h"
7+
#include "node_errors.h"
78
#include "node_internals.h"
89
#include "util-inl.h"
910
#include "v8-inspector.h"
@@ -13,6 +14,7 @@
1314
namespace node {
1415
namespace profiler {
1516

17+
using errors::TryCatchScope;
1618
using v8::Context;
1719
using v8::Function;
1820
using v8::FunctionCallbackInfo;
@@ -219,12 +221,21 @@ void V8CoverageConnection::WriteProfile(Local<String> message) {
219221
}
220222

221223
// append source-map cache information to coverage object:
222-
Local<Function> source_map_cache_getter = env_->source_map_cache_getter();
223224
Local<Value> source_map_cache_v;
224-
if (!source_map_cache_getter->Call(env()->context(),
225-
Undefined(isolate), 0, nullptr)
226-
.ToLocal(&source_map_cache_v)) {
227-
return;
225+
{
226+
TryCatchScope try_catch(env());
227+
{
228+
Isolate::AllowJavascriptExecutionScope allow_js_here(isolate);
229+
Local<Function> source_map_cache_getter = env_->source_map_cache_getter();
230+
if (!source_map_cache_getter->Call(
231+
context, Undefined(isolate), 0, nullptr)
232+
.ToLocal(&source_map_cache_v)) {
233+
return;
234+
}
235+
}
236+
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
237+
PrintCaughtException(isolate, context, try_catch);
238+
}
228239
}
229240
// Avoid writing to disk if no source-map data:
230241
if (!source_map_cache_v->IsUndefined()) {
@@ -351,7 +362,7 @@ void V8HeapProfilerConnection::End() {
351362

352363
// For now, we only support coverage profiling, but we may add more
353364
// in the future.
354-
void EndStartedProfilers(Environment* env) {
365+
static void EndStartedProfilers(Environment* env) {
355366
Debug(env, DebugCategory::INSPECTOR_PROFILER, "EndStartedProfilers\n");
356367
V8ProfilerConnection* connection = env->cpu_profiler_connection();
357368
if (connection != nullptr && !connection->ending()) {
@@ -390,6 +401,10 @@ std::string GetCwd(Environment* env) {
390401
}
391402

392403
void StartProfilers(Environment* env) {
404+
AtExit(env, [](void* env) {
405+
EndStartedProfilers(static_cast<Environment*>(env));
406+
}, env);
407+
393408
Isolate* isolate = env->isolate();
394409
Local<String> coverage_str = env->env_vars()->Get(
395410
isolate, FIXED_ONE_BYTE_STRING(isolate, "NODE_V8_COVERAGE"))

src/node.cc

-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ static const unsigned kMaxSignal = 32;
168168

169169
void WaitForInspectorDisconnect(Environment* env) {
170170
#if HAVE_INSPECTOR
171-
profiler::EndStartedProfilers(env);
172171

173172
if (env->inspector_agent()->IsActive()) {
174173
// Restore signal dispositions, the app is done and is no longer

src/node_errors.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,7 @@ void TriggerUncaughtException(Isolate* isolate,
993993

994994
// Now we are certain that the exception is fatal.
995995
ReportFatalException(env, error, message, EnhanceFatalException::kEnhance);
996-
#if HAVE_INSPECTOR
997-
profiler::EndStartedProfilers(env);
998-
#endif
996+
RunAtExit(env);
999997

1000998
// If the global uncaught exception handler sets process.exitCode,
1001999
// exit with that code. Otherwise, exit with 1.

src/node_internals.h

-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
318318
#if HAVE_INSPECTOR
319319
namespace profiler {
320320
void StartProfilers(Environment* env);
321-
void EndStartedProfilers(Environment* env);
322321
}
323322
#endif // HAVE_INSPECTOR
324323

src/node_process_methods.cc

+9-4
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,15 @@ static void Kill(const FunctionCallbackInfo<Value>& args) {
172172
if (!args[0]->Int32Value(context).To(&pid)) return;
173173
int sig;
174174
if (!args[1]->Int32Value(context).To(&sig)) return;
175-
// TODO(joyeecheung): white list the signals?
176175

177-
#if HAVE_INSPECTOR
178-
profiler::EndStartedProfilers(env);
179-
#endif
176+
uv_pid_t own_pid = uv_os_getpid();
177+
if (sig > 0 &&
178+
(pid == 0 || pid == -1 || pid == own_pid || pid == -own_pid) &&
179+
!HasSignalJSHandler(sig)) {
180+
// This is most likely going to terminate this process.
181+
// It's not an exact method but it might be close enough.
182+
RunAtExit(env);
183+
}
180184

181185
int err = uv_kill(pid, sig);
182186
args.GetReturnValue().Set(err);
@@ -428,6 +432,7 @@ static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
428432

429433
static void ReallyExit(const FunctionCallbackInfo<Value>& args) {
430434
Environment* env = Environment::GetCurrent(args);
435+
RunAtExit(env);
431436
WaitForInspectorDisconnect(env);
432437
int code = args[0]->Int32Value(env->context()).FromMaybe(0);
433438
env->Exit(code);

src/node_worker.cc

-3
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,6 @@ void Worker::Run() {
400400
if (exit_code_ == 0 && !stopped)
401401
exit_code_ = exit_code;
402402

403-
#if HAVE_INSPECTOR
404-
profiler::EndStartedProfilers(env_.get());
405-
#endif
406403
Debug(this, "Exiting thread for worker %llu with exit code %d",
407404
thread_id_, exit_code_);
408405
}

0 commit comments

Comments
 (0)