Skip to content

Commit 54227ee

Browse files
addaleaxtargos
authored andcommitted
src: add native debugging code to workers
Now that we have better native debugging utilities in core, let’s use them :) PR-URL: #21423 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Matheus Marchini <matheus@sthima.com>
1 parent 6f80e30 commit 54227ee

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/async_wrap.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
803803

804804
std::string AsyncWrap::diagnostic_name() const {
805805
return std::string(provider_names[provider_type()]) +
806-
" (" + std::to_string(static_cast<int64_t>(async_id_)) + ")";
806+
" (" + std::to_string(env()->thread_id()) + ":" +
807+
std::to_string(static_cast<int64_t>(async_id_)) + ")";
807808
}
808809

809810
} // namespace node

src/node_messaging.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "debug_utils.h"
12
#include "node_messaging.h"
23
#include "node_internals.h"
34
#include "node_buffer.h"
@@ -305,8 +306,10 @@ void MessagePortData::AddToIncomingQueue(Message&& message) {
305306
Mutex::ScopedLock lock(mutex_);
306307
incoming_messages_.emplace_back(std::move(message));
307308

308-
if (owner_ != nullptr)
309+
if (owner_ != nullptr) {
310+
Debug(owner_, "Adding message to incoming queue");
309311
owner_->TriggerAsync();
312+
}
310313
}
311314

312315
bool MessagePortData::IsSiblingClosed() const {
@@ -380,6 +383,8 @@ MessagePort::MessagePort(Environment* env,
380383
Local<Function> init = fn.As<Function>();
381384
USE(init->Call(context, wrap, 0, nullptr));
382385
}
386+
387+
Debug(this, "Created message port");
383388
}
384389

385390
void MessagePort::AddToIncomingQueue(Message&& message) {
@@ -396,6 +401,8 @@ void MessagePort::TriggerAsync() {
396401
}
397402

398403
void MessagePort::Close(v8::Local<v8::Value> close_callback) {
404+
Debug(this, "Closing message port, data set = %d", static_cast<int>(!!data_));
405+
399406
if (data_) {
400407
// Wrap this call with accessing the mutex, so that TriggerAsync()
401408
// can check IsHandleClosing() without race conditions.
@@ -447,6 +454,7 @@ MessagePort* MessagePort::New(
447454
}
448455

449456
void MessagePort::OnMessage() {
457+
Debug(this, "Running MessagePort::OnMessage()");
450458
HandleScope handle_scope(env()->isolate());
451459
Local<Context> context = object(env()->isolate())->CreationContext();
452460

@@ -461,11 +469,15 @@ void MessagePort::OnMessage() {
461469
Mutex::ScopedLock lock(data_->mutex_);
462470

463471
if (stop_event_loop_) {
472+
Debug(this, "MessagePort stops loop as requested");
464473
CHECK(!data_->receiving_messages_);
465474
uv_stop(env()->event_loop());
466475
break;
467476
}
468477

478+
Debug(this, "MessagePort has message, receiving = %d",
479+
static_cast<int>(data_->receiving_messages_));
480+
469481
if (!data_->receiving_messages_)
470482
break;
471483
if (data_->incoming_messages_.empty())
@@ -475,6 +487,7 @@ void MessagePort::OnMessage() {
475487
}
476488

477489
if (!env()->can_call_into_js()) {
490+
Debug(this, "MessagePort drains queue because !can_call_into_js()");
478491
// In this case there is nothing to do but to drain the current queue.
479492
continue;
480493
}
@@ -508,6 +521,7 @@ bool MessagePort::IsSiblingClosed() const {
508521
}
509522

510523
void MessagePort::OnClose() {
524+
Debug(this, "MessagePort::OnClose()");
511525
if (data_) {
512526
data_->owner_ = nullptr;
513527
data_->Disentangle();
@@ -557,13 +571,15 @@ void MessagePort::PostMessage(const FunctionCallbackInfo<Value>& args) {
557571

558572
void MessagePort::Start() {
559573
Mutex::ScopedLock lock(data_->mutex_);
574+
Debug(this, "Start receiving messages");
560575
data_->receiving_messages_ = true;
561576
if (!data_->incoming_messages_.empty())
562577
TriggerAsync();
563578
}
564579

565580
void MessagePort::Stop() {
566581
Mutex::ScopedLock lock(data_->mutex_);
582+
Debug(this, "Stop receiving messages");
567583
data_->receiving_messages_ = false;
568584
}
569585

@@ -572,6 +588,7 @@ void MessagePort::StopEventLoop() {
572588
data_->receiving_messages_ = false;
573589
stop_event_loop_ = true;
574590

591+
Debug(this, "Received StopEventLoop request");
575592
TriggerAsync();
576593
}
577594

src/node_worker.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Worker::Worker(Environment* env, Local<Object> wrap)
4444
Mutex::ScopedLock next_thread_id_lock(next_thread_id_mutex);
4545
thread_id_ = next_thread_id++;
4646
}
47+
48+
Debug(this, "Creating worker with id %llu", thread_id_);
4749
wrap->Set(env->context(),
4850
env->thread_id_string(),
4951
Number::New(env->isolate(),
@@ -107,6 +109,8 @@ Worker::Worker(Environment* env, Local<Object> wrap)
107109

108110
// The new isolate won't be bothered on this thread again.
109111
isolate_->DiscardThreadSpecificMetadata();
112+
113+
Debug(this, "Set up worker with id %llu", thread_id_);
110114
}
111115

112116
bool Worker::is_stopped() const {
@@ -123,6 +127,7 @@ void Worker::Run() {
123127
MultiIsolatePlatform* platform = isolate_data_->platform();
124128
CHECK_NE(platform, nullptr);
125129

130+
Debug(this, "Starting worker with id %llu", thread_id_);
126131
{
127132
Locker locker(isolate_);
128133
Isolate::Scope isolate_scope(isolate_);
@@ -143,6 +148,8 @@ void Worker::Run() {
143148
// within it.
144149
if (child_port_ != nullptr)
145150
env_->set_message_port(child_port_->object(isolate_));
151+
152+
Debug(this, "Created message port for worker %llu", thread_id_);
146153
}
147154

148155
if (!is_stopped()) {
@@ -152,6 +159,8 @@ void Worker::Run() {
152159
// This loads the Node bootstrapping code.
153160
LoadEnvironment(env_.get());
154161
env_->async_hooks()->pop_async_id(1);
162+
163+
Debug(this, "Loaded environment for worker %llu", thread_id_);
155164
}
156165

157166
{
@@ -189,6 +198,9 @@ void Worker::Run() {
189198
Mutex::ScopedLock lock(mutex_);
190199
if (exit_code_ == 0 && !stopped)
191200
exit_code_ = exit_code;
201+
202+
Debug(this, "Exiting thread for worker %llu with exit code %d",
203+
thread_id_, exit_code_);
192204
}
193205

194206
env_->set_can_call_into_js(false);
@@ -237,12 +249,15 @@ void Worker::Run() {
237249
scheduled_on_thread_stopped_ = true;
238250
uv_async_send(thread_exit_async_.get());
239251
}
252+
253+
Debug(this, "Worker %llu thread stops", thread_id_);
240254
}
241255

242256
void Worker::DisposeIsolate() {
243257
if (isolate_ == nullptr)
244258
return;
245259

260+
Debug(this, "Worker %llu dispose isolate", thread_id_);
246261
CHECK(isolate_data_);
247262
MultiIsolatePlatform* platform = isolate_data_->platform();
248263
platform->CancelPendingDelayedTasks(isolate_);
@@ -275,6 +290,8 @@ void Worker::OnThreadStopped() {
275290
Mutex::ScopedLock lock(mutex_);
276291
scheduled_on_thread_stopped_ = false;
277292

293+
Debug(this, "Worker %llu thread stopped", thread_id_);
294+
278295
{
279296
Mutex::ScopedLock stopped_lock(stopped_mutex_);
280297
CHECK(stopped_);
@@ -318,6 +335,8 @@ Worker::~Worker() {
318335
// This has most likely already happened within the worker thread -- this
319336
// is just in case Worker creation failed early.
320337
DisposeIsolate();
338+
339+
Debug(this, "Worker %llu destroyed", thread_id_);
321340
}
322341

323342
void Worker::New(const FunctionCallbackInfo<Value>& args) {
@@ -371,6 +390,9 @@ void Worker::Unref(const FunctionCallbackInfo<Value>& args) {
371390
void Worker::Exit(int code) {
372391
Mutex::ScopedLock lock(mutex_);
373392
Mutex::ScopedLock stopped_lock(stopped_mutex_);
393+
394+
Debug(this, "Worker %llu called Exit(%d)", thread_id_, code);
395+
374396
if (!stopped_) {
375397
CHECK_NE(env_, nullptr);
376398
stopped_ = true;

0 commit comments

Comments
 (0)