Skip to content

Commit e0fef75

Browse files
inspector: allow JS when delivering to JS sessions
An in-process inspector.Session delivers protocol messages by calling into JavaScript, and V8 produces some of them while it asserts that JavaScript is not executed: Debugger.paused is broadcast from the Isolate::RequestInterrupt handler that Debugger.pause schedules, before V8 opens its own AllowJavascriptExecutionScope for the pause message loop. So the process aborts with "Invoke in DisallowJavascriptExecutionScope" as soon as a debugger client pauses it while JavaScript is running. Open an AllowJavascriptExecutionScope on the synchronous delivery path, the same opt-out d8's own inspector channel uses for every protocol message it delivers, see InspectorFrontend::Send() in deps/v8/src/d8/d8.cc. The messages produced where calling into JavaScript is really unsafe, such as GC callbacks, take the queueing branch instead and are delivered from a point where it is allowed again. Signed-off-by: Christian Aurich <christian.aurichzm@gmail.com>
1 parent b113d09 commit e0fef75

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎src/inspector_js_api.cc‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ class JSBindingsConnection : public BaseObject {
132132
Local<Value> argument;
133133
if (!ToV8Value(env()->context(), message, isolate).ToLocal(&argument))
134134
return;
135+
// An in-process inspector channel delivers protocol messages by calling
136+
// into JavaScript. Debugger.paused can be emitted from a V8
137+
// RequestInterrupt handler, before V8 opens its own
138+
// AllowJavascriptExecutionScope for the pause message loop. d8's inspector
139+
// channel makes the same opt-out in InspectorFrontend::Send(). Messages
140+
// emitted where calling into JavaScript is unsafe, such as GC callbacks,
141+
// take the queueing path above instead.
142+
Isolate::AllowJavascriptExecutionScope allow_js(isolate);
135143
OnMessage(argument);
136144
}
137145

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
common.skipIfInspectorDisabled();
5+
6+
const assert = require('assert');
7+
const { NodeInstance } = require('../common/inspector-helper.js');
8+
9+
// An in-process session observes Debugger.paused while the process is paused,
10+
// so its callback runs from the Isolate::RequestInterrupt handler that
11+
// Debugger.pause schedules, where V8 asserts that JS is not executed.
12+
const script = `
13+
const { Session } = require('inspector');
14+
const session = new Session();
15+
let paused = false;
16+
session.on('Debugger.paused', () => {
17+
paused = true;
18+
});
19+
session.connect();
20+
session.post('Debugger.enable');
21+
console.log('Ready');
22+
// Spin so that the pause is requested while JS is on the stack, which is what
23+
// makes V8 break from the interrupt handler instead of on the next call.
24+
const deadline = Date.now() + ${common.platformTimeout(10000)};
25+
while (!paused && Date.now() < deadline);
26+
console.log(paused ? 'Notified' : 'Not notified');
27+
`;
28+
29+
async function runTest() {
30+
const child = new NodeInstance(undefined, script);
31+
const session = await child.connectInspectorSession();
32+
await session.send({ method: 'NodeRuntime.enable' });
33+
await session.waitForNotification('NodeRuntime.waitingForDebugger');
34+
await session.send([
35+
{ 'method': 'Runtime.enable' },
36+
{ 'method': 'Debugger.enable' },
37+
]);
38+
await session.send({ method: 'Runtime.runIfWaitingForDebugger' });
39+
await session.send({ method: 'NodeRuntime.disable' });
40+
await session.waitForNotification('Debugger.paused', 'Break on start');
41+
await session.send({ 'method': 'Debugger.resume' });
42+
await session.waitForConsoleOutput('log', ['Ready']);
43+
44+
await session.send({ 'method': 'Debugger.pause' });
45+
await session.waitForNotification('Debugger.paused', 'Paused');
46+
await session.send({ 'method': 'Debugger.resume' });
47+
await session.waitForConsoleOutput('log', ['Notified']);
48+
49+
await session.waitForDisconnect();
50+
assert.strictEqual((await child.expectShutdown()).exitCode, 0);
51+
}
52+
53+
runTest().then(common.mustCall());

0 commit comments

Comments
 (0)