async_hooks: don't reuse resource in HttpAgent · nodejs/node@6070e88 · GitHub
Skip to content

Commit 6070e88

Browse files
Flarnatargos
authored andcommitted
async_hooks: don't reuse resource in HttpAgent
As discussed in nodejs/diagnostics#248, #21313 and https://docs.google.com/document/d/1g8OrG5lMIUhRn1zbkutgY83MiTSMx-0NHDs8Bf-nXxM/preview reusing the resource object is a blocker for landing a resource based async hooks API and get rid of the promise destroy hook. This PR ensures that HttpAgent uses the a new resource object in case the socket handle gets reused. PR-URL: #27581 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent f872210 commit 6070e88

5 files changed

Lines changed: 141 additions & 7 deletions

File tree

lib/_http_agent.js

Lines changed: 11 additions & 3 deletions

src/async_wrap.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,26 @@ void AsyncWrap::PopAsyncIds(const FunctionCallbackInfo<Value>& args) {
410410

411411

412412
void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
413+
CHECK(args[0]->IsObject());
414+
413415
AsyncWrap* wrap;
414416
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
417+
418+
Local<Object> resource = args[0].As<Object>();
415419
double execution_async_id =
416-
args[0]->IsNumber() ? args[0].As<Number>()->Value() : kInvalidAsyncId;
417-
wrap->AsyncReset(execution_async_id);
420+
args[1]->IsNumber() ? args[1].As<Number>()->Value() : kInvalidAsyncId;
421+
wrap->AsyncReset(resource, execution_async_id);
418422
}
419423

424+
425+
void AsyncWrap::GetProviderType(const FunctionCallbackInfo<Value>& args) {
426+
AsyncWrap* wrap;
427+
args.GetReturnValue().Set(AsyncWrap::PROVIDER_NONE);
428+
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
429+
args.GetReturnValue().Set(wrap->provider_type());
430+
}
431+
432+
420433
void AsyncWrap::EmitDestroy() {
421434
AsyncWrap::EmitDestroy(env(), async_id_);
422435
// Ensure no double destroy is emitted via AsyncReset().
@@ -437,6 +450,7 @@ Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) {
437450
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap"));
438451
env->SetProtoMethod(tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
439452
env->SetProtoMethod(tmpl, "asyncReset", AsyncWrap::AsyncReset);
453+
env->SetProtoMethod(tmpl, "getProviderType", AsyncWrap::GetProviderType);
440454
env->set_async_wrap_ctor_template(tmpl);
441455
}
442456
return tmpl;

src/async_wrap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class AsyncWrap : public BaseObject {
133133
static void PushAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
134134
static void PopAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
135135
static void AsyncReset(const v8::FunctionCallbackInfo<v8::Value>& args);
136+
static void GetProviderType(const v8::FunctionCallbackInfo<v8::Value>& args);
136137
static void QueueDestroyAsyncId(
137138
const v8::FunctionCallbackInfo<v8::Value>& args);
138139

test/async-hooks/init-hooks.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class ActivityCollector {
158158
// events this makes sense for a few tests in which we enable some hooks
159159
// later
160160
if (this._allowNoInit) {
161-
const stub = { uid, type: 'Unknown', handleIsObject: true };
161+
const stub = { uid, type: 'Unknown', handleIsObject: true, handle: {} };
162162
this._activities.set(uid, stub);
163163
return stub;
164164
} else if (!common.isMainThread) {
@@ -184,7 +184,8 @@ class ActivityCollector {
184184
triggerAsyncId,
185185
// In some cases (e.g. Timeout) the handle is a function, thus the usual
186186
// `typeof handle === 'object' && handle !== null` check can't be used.
187-
handleIsObject: handle instanceof Object
187+
handleIsObject: handle instanceof Object,
188+
handle
188189
};
189190
this._stamp(activity, 'init');
190191
this._activities.set(uid, activity);
Lines changed: 110 additions & 0 deletions

0 commit comments

Comments
 (0)