From 15e160e6264c0b737cc1deee3652de9b29d71d1f Mon Sep 17 00:00:00 2001 From: Eugene Ostroukhov Date: Wed, 3 May 2017 11:46:16 -0700 Subject: [PATCH] inspector: report when main context is destroyed PR-URL: https://github.com/nodejs/node/pull/12814 Reimplements: https://github.com/nodejs/node/pull/7756 Fixes: https://github.com/nodejs/node/issues/7742 Reviewed-By: Ben Noordhuis Reviewed-By: Timothy Gu Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Aleksey Kozyatinskiy --- src/inspector_agent.cc | 19 ++++++++++++++----- test/inspector/test-inspector.js | 8 ++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index 3a8364e4fa5..f573afc70ab 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -271,11 +271,6 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient { terminated_(false), running_nested_loop_(false) { inspector_ = V8Inspector::create(env->isolate(), this); - const uint8_t CONTEXT_NAME[] = "Node.js Main Context"; - StringView context_name(CONTEXT_NAME, sizeof(CONTEXT_NAME) - 1); - v8_inspector::V8ContextInfo info(env->context(), CONTEXT_GROUP_ID, - context_name); - inspector_->contextCreated(info); } void runMessageLoopOnPause(int context_group_id) override { @@ -296,6 +291,17 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient { return uv_hrtime() * 1.0 / NANOS_PER_MSEC; } + void contextCreated(Local context, const std::string& name) { + std::unique_ptr name_buffer = Utf8ToStringView(name); + v8_inspector::V8ContextInfo info(context, CONTEXT_GROUP_ID, + name_buffer->string()); + inspector_->contextCreated(info); + } + + void contextDestroyed(Local context) { + inspector_->contextDestroyed(context); + } + void quitMessageLoopOnPause() override { terminated_ = true; } @@ -379,6 +385,7 @@ bool Agent::Start(v8::Platform* platform, const char* path, inspector_ = std::unique_ptr( new NodeInspectorClient(parent_env_, platform)); + inspector_->contextCreated(parent_env_->context(), "Node.js Main Context"); platform_ = platform; if (options.inspector_enabled()) { return StartIoThread(); @@ -451,6 +458,8 @@ bool Agent::IsStarted() { } void Agent::WaitForDisconnect() { + CHECK_NE(inspector_, nullptr); + inspector_->contextDestroyed(parent_env_->context()); if (io_ != nullptr) { io_->WaitForDisconnect(); } diff --git a/test/inspector/test-inspector.js b/test/inspector/test-inspector.js index 865d9a00156..9d6bb563e11 100644 --- a/test/inspector/test-inspector.js +++ b/test/inspector/test-inspector.js @@ -87,6 +87,13 @@ function setupExpectValue(value) { }; } +function setupExpectContextDestroyed(id) { + return function(message) { + if ('Runtime.executionContextDestroyed' === message['method']) + return message['params']['executionContextId'] === id; + }; +} + function testBreakpointOnStart(session) { console.log('[test]', 'Verifying debugger stops on start (--inspect-brk option)'); @@ -203,6 +210,7 @@ function testI18NCharacters(session) { function testWaitsForFrontendDisconnect(session, harness) { console.log('[test]', 'Verify node waits for the frontend to disconnect'); session.sendInspectorCommands({ 'method': 'Debugger.resume'}) + .expectMessages(setupExpectContextDestroyed(1)) .expectStderrOutput('Waiting for the debugger to disconnect...') .disconnect(true); }