GeezerVison is showing github.com — inspector: enable Inspector JS API in workers · nodejs/node@1c3a2eb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/inspector.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,7 @@ const util = require('util'); | 14 | 14 | const { Connection, open, url } = process.binding('inspector'); | 15 | 15 | const { originalConsole } = require('internal/process/per_thread'); | 16 | 16 |
| 17 | | -if (!Connection || !require('internal/worker').isMainThread) | | 17 | +if (!Connection) | 18 | 18 | throw new ERR_INSPECTOR_NOT_AVAILABLE(); | 19 | 19 |
| 20 | 20 | const connectionSymbol = Symbol('connectionProperty'); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/inspector.js +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -// TODO(addaleax): Figure out how to integrate the inspector with workers. | 4 | | -const hasInspector = process.config.variables.v8_enable_inspector === 1 && | 5 | | - require('internal/worker').isMainThread; | | 3 | +const hasInspector = process.config.variables.v8_enable_inspector === 1; | 6 | 4 | const inspector = hasInspector ? require('inspector') : undefined; | 7 | 5 |
| 8 | 6 | let session; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/worker.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,7 @@ const { MessagePort, MessageChannel } = internalBinding('messaging'); | 17 | 17 | const { handle_onclose } = internalBinding('symbols'); | 18 | 18 | const { clearAsyncIdStack } = require('internal/async_hooks'); | 19 | 19 | const { serializeError, deserializeError } = require('internal/error-serdes'); | | 20 | +const { pathToFileURL } = require('url'); | 20 | 21 |
| 21 | 22 | util.inherits(MessagePort, EventEmitter); | 22 | 23 |
| @@ -257,8 +258,9 @@ class Worker extends EventEmitter { | 257 | 258 | } | 258 | 259 | } | 259 | 260 |
| | 261 | + const url = options.eval ? null : pathToFileURL(filename); | 260 | 262 | // Set up the C++ handle for the worker, as well as some internal wiring. | 261 | | - this[kHandle] = new WorkerImpl(); | | 263 | + this[kHandle] = new WorkerImpl(url); | 262 | 264 | this[kHandle].onexit = (code) => this[kOnExit](code); | 263 | 265 | this[kPort] = this[kHandle].messagePort; | 264 | 266 | this[kPort].on('message', (data) => this[kOnMessage](data)); | |
Collapse file Copy file name to clipboardExpand all lines: src/inspector/main_thread_interface.cc -7Lines changed: 0 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -354,13 +354,6 @@ void MainThreadHandle::Reset() { | 354 | 354 | main_thread_ = nullptr; | 355 | 355 | } | 356 | 356 |
| 357 | | -Agent* MainThreadHandle::GetInspectorAgent() { | 358 | | - Mutex::ScopedLock scoped_lock(block_lock_); | 359 | | - if (main_thread_ == nullptr) | 360 | | - return nullptr; | 361 | | - return main_thread_->inspector_agent(); | 362 | | -} | 363 | | - | 364 | 357 | std::unique_ptr<InspectorSessionDelegate> | 365 | 358 | MainThreadHandle::MakeDelegateThreadSafe( | 366 | 359 | std::unique_ptr<InspectorSessionDelegate> delegate) { | |
Collapse file Copy file name to clipboardExpand all lines: src/inspector/main_thread_interface.h -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,7 +54,6 @@ class MainThreadHandle : public std::enable_shared_from_this<MainThreadHandle> { | 54 | 54 | return ++next_object_id_; | 55 | 55 | } | 56 | 56 | bool Post(std::unique_ptr<Request> request); | 57 | | - Agent* GetInspectorAgent(); | 58 | 57 | std::unique_ptr<InspectorSessionDelegate> MakeDelegateThreadSafe( | 59 | 58 | std::unique_ptr<InspectorSessionDelegate> delegate); | 60 | 59 | bool Expired(); | |
Collapse file Copy file name to clipboardExpand all lines: src/inspector/tracing_agent.cc +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,11 +74,14 @@ DispatchResponse TracingAgent::start( | 74 | 74 | if (categories_set.empty()) | 75 | 75 | return DispatchResponse::Error("At least one category should be enabled"); | 76 | 76 |
| 77 | | - trace_writer_ = env_->tracing_agent_writer()->agent()->AddClient( | 78 | | - categories_set, | 79 | | - std::unique_ptr<InspectorTraceWriter>( | 80 | | - new InspectorTraceWriter(frontend_.get())), | 81 | | - tracing::Agent::kIgnoreDefaultCategories); | | 77 | + auto* writer = env_->tracing_agent_writer(); | | 78 | + if (writer != nullptr) { | | 79 | + trace_writer_ = env_->tracing_agent_writer()->agent()->AddClient( | | 80 | + categories_set, | | 81 | + std::unique_ptr<InspectorTraceWriter>( | | 82 | + new InspectorTraceWriter(frontend_.get())), | | 83 | + tracing::Agent::kIgnoreDefaultCategories); | | 84 | + } | 82 | 85 | return DispatchResponse::OK(); | 83 | 86 | } | 84 | 87 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/inspector_agent.cc +25-5Lines changed: 25 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -189,6 +189,12 @@ static int StartDebugSignalHandler() { | 189 | 189 |
| 190 | 190 | const int CONTEXT_GROUP_ID = 1; | 191 | 191 |
| | 192 | +std::string GetWorkerLabel(node::Environment* env) { | | 193 | + std::ostringstream result; | | 194 | + result << "Worker[" << env->thread_id() << "]"; | | 195 | + return result.str(); | | 196 | +} | | 197 | + | 192 | 198 | class ChannelImpl final : public v8_inspector::V8Inspector::Channel, | 193 | 199 | public protocol::FrontendChannel { | 194 | 200 | public: | @@ -373,10 +379,13 @@ void NotifyClusterWorkersDebugEnabled(Environment* env) { | 373 | 379 |
| 374 | 380 | class NodeInspectorClient : public V8InspectorClient { | 375 | 381 | public: | 376 | | - explicit NodeInspectorClient(node::Environment* env) : env_(env) { | | 382 | + explicit NodeInspectorClient(node::Environment* env, bool is_main) | | 383 | + : env_(env), is_main_(is_main) { | 377 | 384 | client_ = V8Inspector::create(env->isolate(), this); | 378 | 385 | // TODO(bnoordhuis) Make name configurable from src/node.cc. | 379 | | - ContextInfo info(GetHumanReadableProcessName()); | | 386 | + std::string name = | | 387 | + is_main_ ? GetHumanReadableProcessName() : GetWorkerLabel(env); | | 388 | + ContextInfo info(name); | 380 | 389 | info.is_default = true; | 381 | 390 | contextCreated(env->context(), info); | 382 | 391 | } | @@ -593,6 +602,7 @@ class NodeInspectorClient : public V8InspectorClient { | 593 | 602 | } | 594 | 603 |
| 595 | 604 | node::Environment* env_; | | 605 | + bool is_main_; | 596 | 606 | bool running_nested_loop_ = false; | 597 | 607 | std::unique_ptr<V8Inspector> client_; | 598 | 608 | std::unordered_map<int, std::unique_ptr<ChannelImpl>> channels_; | @@ -610,13 +620,23 @@ Agent::Agent(Environment* env) | 610 | 620 | : parent_env_(env), | 611 | 621 | debug_options_(env->options()->debug_options) {} | 612 | 622 |
| 613 | | -Agent::~Agent() = default; | | 623 | +Agent::~Agent() { | | 624 | + if (start_io_thread_async.data == this) { | | 625 | + start_io_thread_async.data = nullptr; | | 626 | + // This is global, will never get freed | | 627 | + uv_close(reinterpret_cast<uv_handle_t*>(&start_io_thread_async), nullptr); | | 628 | + } | | 629 | +} | 614 | 630 |
| 615 | 631 | bool Agent::Start(const std::string& path, | 616 | | - std::shared_ptr<DebugOptions> options) { | | 632 | + std::shared_ptr<DebugOptions> options, | | 633 | + bool is_main) { | | 634 | + if (options == nullptr) { | | 635 | + options = std::make_shared<DebugOptions>(); | | 636 | + } | 617 | 637 | path_ = path; | 618 | 638 | debug_options_ = options; | 619 | | - client_ = std::make_shared<NodeInspectorClient>(parent_env_); | | 639 | + client_ = std::make_shared<NodeInspectorClient>(parent_env_, is_main); | 620 | 640 | if (parent_env_->is_main_thread()) { | 621 | 641 | CHECK_EQ(0, uv_async_init(parent_env_->event_loop(), | 622 | 642 | &start_io_thread_async, | |
Collapse file Copy file name to clipboardExpand all lines: src/inspector_agent.h +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,7 +47,9 @@ class Agent { | 47 | 47 | ~Agent(); | 48 | 48 |
| 49 | 49 | // Create client_, may create io_ if option enabled | 50 | | - bool Start(const std::string& path, std::shared_ptr<DebugOptions> options); | | 50 | + bool Start(const std::string& path, | | 51 | + std::shared_ptr<DebugOptions> options, | | 52 | + bool is_worker); | 51 | 53 | // Stop and destroy io_ | 52 | 54 | void Stop(); | 53 | 55 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -327,7 +327,7 @@ static struct { | 327 | 327 | // right away on the websocket port and fails to bind/etc, this will return | 328 | 328 | // false. | 329 | 329 | return env->inspector_agent()->Start( | 330 | | - script_path == nullptr ? "" : script_path, options); | | 330 | + script_path == nullptr ? "" : script_path, options, true); | 331 | 331 | } | 332 | 332 |
| 333 | 333 | bool InspectorStarted(Environment* env) { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_worker.cc +29-3Lines changed: 29 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,10 +35,26 @@ namespace { | 35 | 35 | uint64_t next_thread_id = 1; | 36 | 36 | Mutex next_thread_id_mutex; | 37 | 37 |
| | 38 | +#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR | | 39 | +void StartWorkerInspector(Environment* child, const std::string& url) { | | 40 | + child->inspector_agent()->Start(url, nullptr, false); | | 41 | +} | | 42 | + | | 43 | +void WaitForWorkerInspectorToStop(Environment* child) { | | 44 | + child->inspector_agent()->WaitForDisconnect(); | | 45 | + child->inspector_agent()->Stop(); | | 46 | +} | | 47 | + | | 48 | +#else | | 49 | +// No-ops | | 50 | +void StartWorkerInspector(Environment* child, const std::string& url) {} | | 51 | +void WaitForWorkerInspectorToStop(Environment* child) {} | | 52 | +#endif | | 53 | + | 38 | 54 | } // anonymous namespace | 39 | 55 |
| 40 | | -Worker::Worker(Environment* env, Local<Object> wrap) | 41 | | - : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER) { | | 56 | +Worker::Worker(Environment* env, Local<Object> wrap, const std::string& url) | | 57 | + : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), url_(url) { | 42 | 58 | // Generate a new thread id. | 43 | 59 | { | 44 | 60 | Mutex::ScopedLock next_thread_id_lock(next_thread_id_mutex); | @@ -155,6 +171,7 @@ void Worker::Run() { | 155 | 171 | env_->async_hooks()->pop_async_id(1); | 156 | 172 |
| 157 | 173 | Debug(this, "Loaded environment for worker %llu", thread_id_); | | 174 | + StartWorkerInspector(env_.get(), url_); | 158 | 175 | } | 159 | 176 |
| 160 | 177 | { | @@ -215,6 +232,7 @@ void Worker::Run() { | 215 | 232 | env_->stop_sub_worker_contexts(); | 216 | 233 | env_->RunCleanup(); | 217 | 234 | RunAtExit(env_.get()); | | 235 | + WaitForWorkerInspectorToStop(env_.get()); | 218 | 236 |
| 219 | 237 | { | 220 | 238 | Mutex::ScopedLock stopped_lock(stopped_mutex_); | @@ -350,7 +368,15 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | 350 | 368 | return; | 351 | 369 | } | 352 | 370 |
| 353 | | - new Worker(env, args.This()); | | 371 | + std::string url; | | 372 | + // Argument might be a string or URL | | 373 | + if (args.Length() == 1 && !args[0]->IsNullOrUndefined()) { | | 374 | + Utf8Value value( | | 375 | + args.GetIsolate(), | | 376 | + args[0]->ToString(env->context()).FromMaybe(v8::Local<v8::String>())); | | 377 | + url.append(value.out(), value.length()); | | 378 | + } | | 379 | + new Worker(env, args.This(), url); | 354 | 380 | } | 355 | 381 |
| 356 | 382 | void Worker::StartThread(const FunctionCallbackInfo<Value>& args) { | |
|