| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4647e6b commit ef5dabd
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,6 +45,7 @@ void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str); | |||
| 45 | 45 | V(DIAGNOSTICS) \ | |
| 46 | 46 | V(HUGEPAGES) \ | |
| 47 | 47 | V(INSPECTOR_SERVER) \ | |
| 48 | + V(INSPECTOR_CLIENT) \ | ||
| 48 | 49 | V(INSPECTOR_PROFILER) \ | |
| 49 | 50 | V(CODE_CACHE) \ | |
| 50 | 51 | V(NGTCP2_DEBUG) \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -664,6 +664,10 @@ inline bool Environment::should_create_inspector() const { | |||
| 664 | 664 | !options_->test_runner && !options_->watch_mode; | |
| 665 | 665 | } | |
| 666 | 666 | ||
| 667 | + inline bool Environment::should_wait_for_inspector_frontend() const { | ||
| 668 | + return (flags_ & EnvironmentFlags::kNoWaitForInspectorFrontend) == 0; | ||
| 669 | + } | ||
| 670 | + | ||
| 667 | 671 | inline bool Environment::tracks_unmanaged_fds() const { | |
| 668 | 672 | return flags_ & EnvironmentFlags::kTrackUnmanagedFds; | |
| 669 | 673 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -628,6 +628,7 @@ class Environment : public MemoryRetainer { | |||
| 628 | 628 | // the ownership if transferred into the Environment. | |
| 629 | 629 | void InitializeInspector( | |
| 630 | 630 | std::unique_ptr<inspector::ParentInspectorHandle> parent_handle); | |
| 631 | + void WaitForInspectorFrontendByOptions(); | ||
| 631 | 632 | #endif | |
| 632 | 633 | ||
| 633 | 634 | inline size_t async_callback_scope_depth() const; | |
@@ -798,6 +799,7 @@ class Environment : public MemoryRetainer { | |||
| 798 | 799 | inline bool no_native_addons() const; | |
| 799 | 800 | inline bool should_not_register_esm_loader() const; | |
| 800 | 801 | inline bool should_create_inspector() const; | |
| 802 | + inline bool should_wait_for_inspector_frontend() const; | ||
| 801 | 803 | inline bool owns_process_state() const; | |
| 802 | 804 | inline bool owns_inspector() const; | |
| 803 | 805 | inline bool tracks_unmanaged_fds() const; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -487,6 +487,8 @@ class NodeInspectorClient : public V8InspectorClient { | |||
| 487 | 487 | } | |
| 488 | 488 | ||
| 489 | 489 | if (interface_) { | |
| 490 | + per_process::Debug(DebugCategory::INSPECTOR_CLIENT, | ||
| 491 | + "Stopping waiting for frontend events\n"); | ||
| 490 | 492 | interface_->StopWaitingForFrontendEvent(); | |
| 491 | 493 | } | |
| 492 | 494 | } | |
@@ -668,11 +670,16 @@ class NodeInspectorClient : public V8InspectorClient { | |||
| 668 | 670 | ||
| 669 | 671 | running_nested_loop_ = true; | |
| 670 | 672 | ||
| 673 | + per_process::Debug(DebugCategory::INSPECTOR_CLIENT, | ||
| 674 | + "Entering nested loop\n"); | ||
| 675 | + | ||
| 671 | 676 | while (shouldRunMessageLoop()) { | |
| 672 | 677 | if (interface_) interface_->WaitForFrontendEvent(); | |
| 673 | 678 | env_->RunAndClearInterrupts(); | |
| 674 | 679 | } | |
| 675 | 680 | running_nested_loop_ = false; | |
| 681 | + | ||
| 682 | + per_process::Debug(DebugCategory::INSPECTOR_CLIENT, "Exited nested loop\n"); | ||
| 676 | 683 | } | |
| 677 | 684 | ||
| 678 | 685 | double currentTimeMS() override { | |
@@ -759,27 +766,11 @@ bool Agent::Start(const std::string& path, | |||
| 759 | 766 | } | |
| 760 | 767 | }, parent_env_); | |
| 761 | 768 | ||
| 762 | - bool wait_for_connect = options.wait_for_connect(); | ||
| 763 | - bool should_break_first_line = options.should_break_first_line(); | ||
| 764 | - if (parent_handle_) { | ||
| 765 | - should_break_first_line = parent_handle_->WaitForConnect(); | ||
| 766 | - parent_handle_->WorkerStarted(client_->getThreadHandle(), | ||
| 767 | - should_break_first_line); | ||
| 768 | - } else if (!options.inspector_enabled || !options.allow_attaching_debugger || | ||
| 769 | - !StartIoThread()) { | ||
| 769 | + if (!parent_handle_ && | ||
| 770 | + (!options.inspector_enabled || !options.allow_attaching_debugger || | ||
| 771 | + !StartIoThread())) { | ||
| 770 | 772 | return false; | |
| 771 | 773 | } | |
| 772 | - | ||
| 773 | - if (wait_for_connect || should_break_first_line) { | ||
| 774 | - // Patch the debug options to implement waitForDebuggerOnStart for | ||
| 775 | - // the NodeWorker.enable method. | ||
| 776 | - if (should_break_first_line) { | ||
| 777 | - CHECK(!parent_env_->has_serialized_options()); | ||
| 778 | - debug_options_.EnableBreakFirstLine(); | ||
| 779 | - parent_env_->options()->get_debug_options()->EnableBreakFirstLine(); | ||
| 780 | - } | ||
| 781 | - client_->waitForFrontend(); | ||
| 782 | - } | ||
| 783 | 774 | return true; | |
| 784 | 775 | } | |
| 785 | 776 | ||
@@ -1038,6 +1029,33 @@ void Agent::WaitForConnect() { | |||
| 1038 | 1029 | client_->waitForFrontend(); | |
| 1039 | 1030 | } | |
| 1040 | 1031 | ||
| 1032 | + bool Agent::WaitForConnectByOptions() { | ||
| 1033 | + if (client_ == nullptr) { | ||
| 1034 | + return false; | ||
| 1035 | + } | ||
| 1036 | + | ||
| 1037 | + bool wait_for_connect = debug_options_.wait_for_connect(); | ||
| 1038 | + bool should_break_first_line = debug_options_.should_break_first_line(); | ||
| 1039 | + if (parent_handle_) { | ||
| 1040 | + should_break_first_line = parent_handle_->WaitForConnect(); | ||
| 1041 | + parent_handle_->WorkerStarted(client_->getThreadHandle(), | ||
| 1042 | + should_break_first_line); | ||
| 1043 | + } | ||
| 1044 | + | ||
| 1045 | + if (wait_for_connect || should_break_first_line) { | ||
| 1046 | + // Patch the debug options to implement waitForDebuggerOnStart for | ||
| 1047 | + // the NodeWorker.enable method. | ||
| 1048 | + if (should_break_first_line) { | ||
| 1049 | + CHECK(!parent_env_->has_serialized_options()); | ||
| 1050 | + debug_options_.EnableBreakFirstLine(); | ||
| 1051 | + parent_env_->options()->get_debug_options()->EnableBreakFirstLine(); | ||
| 1052 | + } | ||
| 1053 | + client_->waitForFrontend(); | ||
| 1054 | + return true; | ||
| 1055 | + } | ||
| 1056 | + return false; | ||
| 1057 | + } | ||
| 1058 | + | ||
| 1041 | 1059 | void Agent::StopIfWaitingForConnect() { | |
| 1042 | 1060 | if (client_ == nullptr) { | |
| 1043 | 1061 | return; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,6 +61,7 @@ class Agent { | |||
| 61 | 61 | ||
| 62 | 62 | // Blocks till frontend connects and sends "runIfWaitingForDebugger" | |
| 63 | 63 | void WaitForConnect(); | |
| 64 | + bool WaitForConnectByOptions(); | ||
| 64 | 65 | void StopIfWaitingForConnect(); | |
| 65 | 66 | ||
| 66 | 67 | // Blocks till all the sessions with "WaitForDisconnectOnShutdown" disconnect | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -204,7 +204,17 @@ void Environment::InitializeInspector( | |||
| 204 | 204 | return; | |
| 205 | 205 | } | |
| 206 | 206 | ||
| 207 | + if (should_wait_for_inspector_frontend()) { | ||
| 208 | + WaitForInspectorFrontendByOptions(); | ||
| 209 | + } | ||
| 210 | + | ||
| 207 | 211 | profiler::StartProfilers(this); | |
| 212 | + } | ||
| 213 | + | ||
| 214 | + void Environment::WaitForInspectorFrontendByOptions() { | ||
| 215 | + if (!inspector_agent_->WaitForConnectByOptions()) { | ||
| 216 | + return; | ||
| 217 | + } | ||
| 208 | 218 | ||
| 209 | 219 | if (inspector_agent_->options().break_node_first_line) { | |
| 210 | 220 | inspector_agent_->PauseOnNextJavascriptStatement("Break at bootstrap"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -661,7 +661,11 @@ enum Flags : uint64_t { | |||
| 661 | 661 | // Controls where or not the InspectorAgent for this Environment should | |
| 662 | 662 | // call StartDebugSignalHandler. This control is needed by embedders who may | |
| 663 | 663 | // not want to allow other processes to start the V8 inspector. | |
| 664 | - kNoStartDebugSignalHandler = 1 << 10 | ||
| 664 | + kNoStartDebugSignalHandler = 1 << 10, | ||
| 665 | + // Controls whether the InspectorAgent created for this Environment waits for | ||
| 666 | + // Inspector frontend events during the Environment creation. It's used to | ||
| 667 | + // call node::Stop(env) on a Worker thread that is waiting for the events. | ||
| 668 | + kNoWaitForInspectorFrontend = 1 << 11 | ||
| 665 | 669 | }; | |
| 666 | 670 | } // namespace EnvironmentFlags | |
| 667 | 671 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -359,6 +359,9 @@ void Worker::Run() { | |||
| 359 | 359 | CHECK(!context.IsEmpty()); | |
| 360 | 360 | Context::Scope context_scope(context); | |
| 361 | 361 | { | |
| 362 | + #if HAVE_INSPECTOR | ||
| 363 | + environment_flags_ |= EnvironmentFlags::kNoWaitForInspectorFrontend; | ||
| 364 | + #endif | ||
| 362 | 365 | env_.reset(CreateEnvironment( | |
| 363 | 366 | data.isolate_data_.get(), | |
| 364 | 367 | context, | |
@@ -380,6 +383,10 @@ void Worker::Run() { | |||
| 380 | 383 | this->env_ = env_.get(); | |
| 381 | 384 | } | |
| 382 | 385 | Debug(this, "Created Environment for worker with id %llu", thread_id_.id); | |
| 386 | + | ||
| 387 | + #if HAVE_INSPECTOR | ||
| 388 | + this->env_->WaitForInspectorFrontendByOptions(); | ||
| 389 | + #endif | ||
| 383 | 390 | if (is_stopped()) return; | |
| 384 | 391 | { | |
| 385 | 392 | if (!CreateEnvMessagePort(env_.get())) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,47 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + common.skipIfInspectorDisabled(); | ||
| 5 | + | ||
| 6 | + const { workerData, Worker } = require('node:worker_threads'); | ||
| 7 | + if (!workerData) { | ||
| 8 | + common.skipIfWorker(); | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + const assert = require('node:assert'); | ||
| 12 | + | ||
| 13 | + let TIMEOUT = common.platformTimeout(5000); | ||
| 14 | + if (common.isWindows) { | ||
| 15 | + // Refs: https://github.com/nodejs/build/issues/3014 | ||
| 16 | + TIMEOUT = common.platformTimeout(15000); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + // Refs: https://github.com/nodejs/node/issues/53648 | ||
| 20 | + | ||
| 21 | + (async () => { | ||
| 22 | + if (!workerData) { | ||
| 23 | + // worker.terminate() should terminate the worker created with execArgv: | ||
| 24 | + // ["--inspect-brk"]. | ||
| 25 | + { | ||
| 26 | + const worker = new Worker(__filename, { | ||
| 27 | + execArgv: ['--inspect-brk=0'], | ||
| 28 | + workerData: {}, | ||
| 29 | + }); | ||
| 30 | + await new Promise((r) => setTimeout(r, TIMEOUT)); | ||
| 31 | + worker.on('exit', common.mustCall()); | ||
| 32 | + await worker.terminate(); | ||
| 33 | + } | ||
| 34 | + // process.exit() should kill the process. | ||
| 35 | + { | ||
| 36 | + new Worker(__filename, { | ||
| 37 | + execArgv: ['--inspect-brk=0'], | ||
| 38 | + workerData: {}, | ||
| 39 | + }); | ||
| 40 | + await new Promise((r) => setTimeout(r, TIMEOUT)); | ||
| 41 | + process.on('exit', (status) => assert.strictEqual(status, 0)); | ||
| 42 | + setImmediate(() => process.exit()); | ||
| 43 | + } | ||
| 44 | + } else { | ||
| 45 | + console.log('Worker running!'); | ||
| 46 | + } | ||
| 47 | + })().then(common.mustCall()); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments