Skip to content

Commit a3abc42

Browse files
authored
async_hooks,inspector: implement inspector api without async_wrap
Implementing the inspector session object as an async resource causes unwanted context change when a breakpoint callback function is being called. Modelling the inspector api without the AsyncWrap base class ensures that the callback has access to the AsyncLocalStorage instance that is active in the affected user function. See `test-inspector-async-context-brk.js` for an illustration of the use case. PR-URL: #51501 Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent 26f63be commit a3abc42

File tree

6 files changed

+64
-28
lines changed

6 files changed

+64
-28
lines changed

‎src/async_wrap.h‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,9 @@ namespace node{
102102
#defineNODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)
103103
#endif// HAVE_OPENSSL
104104

105-
#if HAVE_INSPECTOR
106-
#defineNODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V) \
107-
V(INSPECTORJSBINDING)
108-
#else
109-
#defineNODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V)
110-
#endif// HAVE_INSPECTOR
111-
112-
#defineNODE_ASYNC_PROVIDER_TYPES(V) \
113-
NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
114-
NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V) \
115-
NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V)
105+
#defineNODE_ASYNC_PROVIDER_TYPES(V) \
106+
NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
107+
NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)
116108

117109
classEnvironment;
118110
classDestroyParam;

‎src/inspector_js_api.cc‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include"async_wrap-inl.h"
21
#include"base_object-inl.h"
32
#include"inspector_agent.h"
43
#include"inspector_io.h"
@@ -61,7 +60,7 @@ struct MainThreadConnection{
6160
};
6261

6362
template <typename ConnectionType>
64-
classJSBindingsConnection : publicAsyncWrap{
63+
classJSBindingsConnection : publicBaseObject{
6564
public:
6665
classJSBindingsSessionDelegate : publicInspectorSessionDelegate{
6766
public:
@@ -91,15 +90,16 @@ class JSBindingsConnection : public AsyncWrap{
9190
JSBindingsConnection(Environment* env,
9291
Local<Object> wrap,
9392
Local<Function> callback)
94-
: AsyncWrap(env, wrap, PROVIDER_INSPECTORJSBINDING),
95-
callback_(env->isolate(), callback){
93+
: BaseObject(env, wrap), callback_(env->isolate(), callback){
9694
Agent* inspector = env->inspector_agent();
9795
session_ = ConnectionType::Connect(
9896
inspector, std::make_unique<JSBindingsSessionDelegate>(env, this));
9997
}
10098

10199
voidOnMessage(Local<Value> value){
102-
MakeCallback(callback_.Get(env()->isolate()), 1, &value);
100+
auto result = callback_.Get(env()->isolate())
101+
->Call(env()->context(), object(), 1, &value);
102+
(void)result;
103103
}
104104

105105
staticvoidBind(Environment* env, Local<Object> target){
@@ -108,7 +108,6 @@ class JSBindingsConnection : public AsyncWrap{
108108
NewFunctionTemplate(isolate, JSBindingsConnection::New);
109109
tmpl->InstanceTemplate()->SetInternalFieldCount(
110110
JSBindingsConnection::kInternalFieldCount);
111-
tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
112111
SetProtoMethod(isolate, tmpl, "dispatch", JSBindingsConnection::Dispatch);
113112
SetProtoMethod(
114113
isolate, tmpl, "disconnect", JSBindingsConnection::Disconnect);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
const{ AsyncLocalStorage }=require('async_hooks');
4+
constals=newAsyncLocalStorage();
5+
6+
functiongetStore(){
7+
returnals.getStore();
8+
}
9+
10+
common.skipIfInspectorDisabled();
11+
12+
constassert=require('assert');
13+
const{ Session }=require('inspector');
14+
constpath=require('path');
15+
const{ pathToFileURL }=require('url');
16+
17+
letvalueInFunction=0;
18+
letvalueInBreakpoint=0;
19+
20+
functiondebugged(){
21+
valueInFunction=getStore();
22+
return42;
23+
}
24+
25+
asyncfunctiontest(){
26+
constsession=newSession();
27+
28+
session.connect();
29+
session.post('Debugger.enable');
30+
31+
session.on('Debugger.paused',()=>{
32+
valueInBreakpoint=getStore();
33+
});
34+
35+
awaitnewPromise((resolve,reject)=>{
36+
session.post('Debugger.setBreakpointByUrl',{
37+
'lineNumber': 22,
38+
'url': pathToFileURL(path.resolve(__dirname,__filename)).toString(),
39+
'columnNumber': 0,
40+
'condition': ''
41+
},(error,result)=>{
42+
returnerror ? reject(error) : resolve(result);
43+
});
44+
});
45+
46+
als.run(1,debugged);
47+
assert.strictEqual(valueInFunction,valueInBreakpoint);
48+
assert.strictEqual(valueInFunction,1);
49+
50+
session.disconnect();
51+
}
52+
53+
constinterval=setInterval(()=>{},1000);
54+
test().then(common.mustCall(()=>{
55+
clearInterval(interval);
56+
}));

‎test/parallel/test-inspector-multisession-js.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --expose-internals
21
'use strict';
32
constcommon=require('../common');
43

‎test/sequential/test-async-wrap-getasyncid.js‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ const{getSystemErrorName } = require('util');
4747
deleteproviders.WORKER;
4848
// TODO(danbev): Test for these
4949
deleteproviders.JSUDPWRAP;
50-
if(!common.isMainThread)
51-
deleteproviders.INSPECTORJSBINDING;
5250
deleteproviders.KEYPAIRGENREQUEST;
5351
deleteproviders.KEYGENREQUEST;
5452
deleteproviders.KEYEXPORTREQUEST;
@@ -316,13 +314,6 @@ if (common.hasCrypto){// eslint-disable-line node-core/crypto-check
316314
testInitialized(req,'SendWrap');
317315
}
318316

319-
if(process.features.inspector&&common.isMainThread){
320-
constbinding=internalBinding('inspector');
321-
consthandle=newbinding.Connection(()=>{});
322-
testInitialized(handle,'Connection');
323-
handle.disconnect();
324-
}
325-
326317
// PROVIDER_HEAPDUMP
327318
{
328319
v8.getHeapSnapshot().destroy();

‎typings/internalBinding/async_wrap.d.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ declare namespace InternalAsyncWrapBinding{
6868
SIGNREQUEST: 54;
6969
TLSWRAP: 55;
7070
VERIFYREQUEST: 56;
71-
INSPECTORJSBINDING: 57;
7271
}
7372
}
7473

0 commit comments

Comments
(0)