Skip to content

Commit f972bd8

Browse files
Eugene Ostroukhovaddaleax
Eugene Ostroukhov
authored andcommitted
inspector: libuv notification on incoming message
Currently Inspector posts a V8 "task" when a message is incoming. To make sure messages are processed even when no JS is executed (e.g. while waiting for I/O or timer), inspector will now post a libuv request. Fixes: #11589 PR-URL: #11617 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 98d3328 commit f972bd8

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

src/inspector_agent.cc

+13-4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class AgentImpl {
159159

160160
static void ThreadCbIO(void* agent);
161161
static void WriteCbIO(uv_async_t* async);
162+
static void MainThreadAsyncCb(uv_async_t* req);
162163

163164
void InstallInspectorOnProcess();
164165

@@ -190,6 +191,7 @@ class AgentImpl {
190191
node::Environment* parent_env_;
191192

192193
uv_async_t io_thread_req_;
194+
uv_async_t main_thread_req_;
193195
V8NodeInspector* inspector_;
194196
v8::Platform* platform_;
195197
MessageQueue<InspectorAction> incoming_message_queue_;
@@ -334,6 +336,9 @@ AgentImpl::AgentImpl(Environment* env) : delegate_(nullptr),
334336
dispatching_messages_(false),
335337
session_id_(0),
336338
server_(nullptr) {
339+
CHECK_EQ(0, uv_async_init(env->event_loop(), &main_thread_req_,
340+
AgentImpl::MainThreadAsyncCb));
341+
uv_unref(reinterpret_cast<uv_handle_t*>(&main_thread_req_));
337342
CHECK_EQ(0, uv_sem_init(&start_sem_, 0));
338343
memset(&io_thread_req_, 0, sizeof(io_thread_req_));
339344
}
@@ -416,10 +421,7 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
416421

417422
InstallInspectorOnProcess();
418423

419-
int err = uv_loop_init(&child_loop_);
420-
CHECK_EQ(err, 0);
421-
422-
err = uv_thread_create(&thread_, AgentImpl::ThreadCbIO, this);
424+
int err = uv_thread_create(&thread_, AgentImpl::ThreadCbIO, this);
423425
CHECK_EQ(err, 0);
424426
uv_sem_wait(&start_sem_);
425427

@@ -606,6 +608,7 @@ void AgentImpl::PostIncomingMessage(InspectorAction action, int session_id,
606608
platform_->CallOnForegroundThread(isolate,
607609
new DispatchOnInspectorBackendTask(this));
608610
isolate->RequestInterrupt(InterruptCallback, this);
611+
CHECK_EQ(0, uv_async_send(&main_thread_req_));
609612
}
610613
NotifyMessageReceived();
611614
}
@@ -662,6 +665,12 @@ void AgentImpl::DispatchMessages() {
662665
dispatching_messages_ = false;
663666
}
664667

668+
// static
669+
void AgentImpl::MainThreadAsyncCb(uv_async_t* req) {
670+
AgentImpl* agent = node::ContainerOf(&AgentImpl::main_thread_req_, req);
671+
agent->DispatchMessages();
672+
}
673+
665674
void AgentImpl::Write(TransportAction action, int session_id,
666675
const StringView& inspector_message) {
667676
AppendMessage(&outgoing_message_queue_, action, session_id,

test/inspector/inspector-helper.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,24 @@ Harness.prototype.expectShutDown = function(errorCode) {
427427
});
428428
};
429429

430-
exports.startNodeForInspectorTest = function(callback) {
431-
const child = spawn(process.execPath,
432-
[ '--inspect-brk', mainScript ]);
430+
Harness.prototype.kill = function() {
431+
return this.enqueue_((callback) => {
432+
this.process_.kill();
433+
callback();
434+
});
435+
};
436+
437+
exports.startNodeForInspectorTest = function(callback,
438+
inspectorFlag = '--inspect-brk',
439+
opt_script_contents) {
440+
const args = [inspectorFlag];
441+
if (opt_script_contents) {
442+
args.push('-e', opt_script_contents);
443+
} else {
444+
args.push(mainScript);
445+
}
446+
447+
const child = spawn(process.execPath, args);
433448

434449
const timeoutId = timeout('Child process did not start properly', 4);
435450

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
require('../common');
3+
const helper = require('./inspector-helper.js');
4+
5+
function shouldShutDown(session) {
6+
session
7+
.sendInspectorCommands([
8+
{ 'method': 'Debugger.enable' },
9+
{ 'method': 'Debugger.pause' },
10+
])
11+
.disconnect(true);
12+
}
13+
14+
function runTests(harness) {
15+
// 1 second wait to make sure the inferior began running the script
16+
setTimeout(() => harness.runFrontendSession([shouldShutDown]).kill(), 1000);
17+
}
18+
19+
const script = 'setInterval(() => {debugger;}, 60000);';
20+
helper.startNodeForInspectorTest(runTests, '--inspect', script);

0 commit comments

Comments
 (0)