-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Historically Node process sends Runtime.executionContextDestroyed with main context as argument when it is finished. This approach has some disadvantages. V8 prevents running some protocol command on destroyed contexts, e.g. Runtime.evaluate will return an error or Debugger.enable won't return a list of scripts. Both command might be useful for different tools, e.g. tool runs Profiler.startPreciseCoverage and at the end of node process it would like to get list of all scripts to match data to source code. Or some tooling frontend would like to provide capabilities to run commands in console when node process is finished to allow user to inspect state of the program at exit. This PR adds new domain: NodeRuntime. After NodeRuntime.notifyWhenWaitingForDisconnect is enabled by at least one client, node will send NodeRuntime.waitingForDebuggerToDisconnect event instead of Runtime.executionContextDestroyed. Based on this signal any protocol client can capture all required information and then disconnect its session. PR-URL: #27600 Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
- Loading branch information
Showing
7 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "runtime_agent.h" | ||
|
||
#include "env-inl.h" | ||
#include "inspector_agent.h" | ||
|
||
namespace node { | ||
namespace inspector { | ||
namespace protocol { | ||
|
||
RuntimeAgent::RuntimeAgent(Environment* env) | ||
: notify_when_waiting_for_disconnect_(false), env_(env) {} | ||
|
||
void RuntimeAgent::Wire(UberDispatcher* dispatcher) { | ||
frontend_ = std::make_unique<NodeRuntime::Frontend>(dispatcher->channel()); | ||
NodeRuntime::Dispatcher::wire(dispatcher, this); | ||
} | ||
|
||
DispatchResponse RuntimeAgent::notifyWhenWaitingForDisconnect(bool enabled) { | ||
if (!env_->owns_process_state()) { | ||
return DispatchResponse::Error( | ||
"NodeRuntime domain can only be used through main thread sessions"); | ||
} | ||
notify_when_waiting_for_disconnect_ = enabled; | ||
return DispatchResponse::OK(); | ||
} | ||
|
||
bool RuntimeAgent::notifyWaitingForDisconnect() { | ||
if (notify_when_waiting_for_disconnect_) { | ||
frontend_->waitingForDisconnect(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} // namespace protocol | ||
} // namespace inspector | ||
} // namespace node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef SRC_INSPECTOR_RUNTIME_AGENT_H_ | ||
#define SRC_INSPECTOR_RUNTIME_AGENT_H_ | ||
|
||
#include "node/inspector/protocol/NodeRuntime.h" | ||
#include "v8.h" | ||
|
||
namespace node { | ||
class Environment; | ||
|
||
namespace inspector { | ||
namespace protocol { | ||
|
||
class RuntimeAgent : public NodeRuntime::Backend { | ||
public: | ||
explicit RuntimeAgent(Environment* env); | ||
|
||
void Wire(UberDispatcher* dispatcher); | ||
|
||
DispatchResponse notifyWhenWaitingForDisconnect(bool enabled) override; | ||
|
||
bool notifyWaitingForDisconnect(); | ||
|
||
private: | ||
std::shared_ptr<NodeRuntime::Frontend> frontend_; | ||
bool notify_when_waiting_for_disconnect_; | ||
Environment* env_; | ||
}; | ||
} // namespace protocol | ||
} // namespace inspector | ||
} // namespace node | ||
|
||
#endif // SRC_INSPECTOR_RUNTIME_AGENT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
common.skipIfInspectorDisabled(); | ||
|
||
const assert = require('assert'); | ||
const { NodeInstance } = require('../common/inspector-helper.js'); | ||
|
||
function mainContextDestroyed(notification) { | ||
return notification.method === 'Runtime.executionContextDestroyed' && | ||
notification.params.executionContextId === 1; | ||
} | ||
|
||
async function runTest() { | ||
const child = new NodeInstance(['--inspect-brk=0', '-e', 'process.exit(55)']); | ||
const session = await child.connectInspectorSession(); | ||
const oldStyleSession = await child.connectInspectorSession(); | ||
await oldStyleSession.send([ | ||
{ method: 'Runtime.enable' }]); | ||
await session.send([ | ||
{ method: 'Runtime.enable' }, | ||
{ method: 'NodeRuntime.notifyWhenWaitingForDisconnect', | ||
params: { enabled: true } }, | ||
{ method: 'Runtime.runIfWaitingForDebugger' }]); | ||
await session.waitForNotification((notification) => { | ||
return notification.method === 'NodeRuntime.waitingForDisconnect'; | ||
}); | ||
const receivedExecutionContextDestroyed = | ||
session.unprocessedNotifications().some(mainContextDestroyed); | ||
if (receivedExecutionContextDestroyed) { | ||
assert.fail('When NodeRuntime enabled, ' + | ||
'Runtime.executionContextDestroyed should not be sent'); | ||
} | ||
const { result: { value } } = await session.send({ | ||
method: 'Runtime.evaluate', params: { expression: '42' } | ||
}); | ||
assert.strictEqual(value, 42); | ||
await session.disconnect(); | ||
await oldStyleSession.waitForNotification(mainContextDestroyed); | ||
await oldStyleSession.disconnect(); | ||
assert.strictEqual((await child.expectShutdown()).exitCode, 55); | ||
} | ||
|
||
runTest(); |