Skip to content

Commit e204dba

Browse files
addaleaxtargos
authored andcommitted
src: pass resource object along with InternalMakeCallback
This was an oversight in 9fdb6e6. Fixing this is necessary to make `executionAsyncResource()` work as expected. Refs: #30959 Fixes: #32060 PR-URL: #32063 Reviewed-By: Vladimir de Turckheim <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ffefb05 commit e204dba

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/api/callback.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ void InternalCallbackScope::Close() {
139139
}
140140

141141
MaybeLocal<Value> InternalMakeCallback(Environment* env,
142+
Local<Object> resource,
142143
Local<Object> recv,
143144
const Local<Function> callback,
144145
int argc,
@@ -150,7 +151,7 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
150151
CHECK(!argv[i].IsEmpty());
151152
#endif
152153

153-
InternalCallbackScope scope(env, recv, asyncContext);
154+
InternalCallbackScope scope(env, resource, asyncContext);
154155
if (scope.Failed()) {
155156
return MaybeLocal<Value>();
156157
}
@@ -224,7 +225,7 @@ MaybeLocal<Value> MakeCallback(Isolate* isolate,
224225
CHECK_NOT_NULL(env);
225226
Context::Scope context_scope(env->context());
226227
MaybeLocal<Value> ret =
227-
InternalMakeCallback(env, recv, callback, argc, argv, asyncContext);
228+
InternalMakeCallback(env, recv, recv, callback, argc, argv, asyncContext);
228229
if (ret.IsEmpty() && env->async_callback_scope_depth() == 0) {
229230
// This is only for legacy compatibility and we may want to look into
230231
// removing/adjusting it.

src/async_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ MaybeLocal<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
749749
ProviderType provider = provider_type();
750750
async_context context { get_async_id(), get_trigger_async_id() };
751751
MaybeLocal<Value> ret = InternalMakeCallback(
752-
env(), object(), cb, argc, argv, context);
752+
env(), GetResource(), object(), cb, argc, argv, context);
753753

754754
// This is a static call with cached values because the `this` object may
755755
// no longer be alive at this point.

src/node_internals.h

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ static v8::MaybeLocal<v8::Object> New(Environment* env,
201201

202202
v8::MaybeLocal<v8::Value> InternalMakeCallback(
203203
Environment* env,
204+
v8::Local<v8::Object> resource,
204205
v8::Local<v8::Object> recv,
205206
const v8::Local<v8::Function> callback,
206207
int argc,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const {
5+
executionAsyncResource,
6+
executionAsyncId,
7+
createHook,
8+
} = require('async_hooks');
9+
const http = require('http');
10+
11+
const hooked = {};
12+
createHook({
13+
init(asyncId, type, triggerAsyncId, resource) {
14+
hooked[asyncId] = resource;
15+
}
16+
}).enable();
17+
18+
const server = http.createServer((req, res) => {
19+
res.write('hello');
20+
setTimeout(() => {
21+
res.end(' world!');
22+
}, 1000);
23+
});
24+
25+
server.listen(0, () => {
26+
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
27+
http.get({ port: server.address().port }, (res) => {
28+
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
29+
res.on('data', () => {
30+
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
31+
});
32+
res.on('end', () => {
33+
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
34+
server.close();
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)