Skip to content

Commit 9da6d7a

Browse files
committed
async_hooks: AsyncWrap knows if it has emitted an init before
1 parent 5335c53 commit 9da6d7a

File tree

4 files changed

+11
-24
lines changed

4 files changed

+11
-24
lines changed

lib/_http_agent.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ const net = require('net');
2525
const util = require('util');
2626
const EventEmitter = require('events');
2727
const debug = util.debuglog('http');
28-
const {
29-
destroyHooksExist,
30-
emitDestroy,
31-
symbols
32-
} = require('internal/async_hooks');
33-
const async_id_symbol = symbols.async_id_symbol;
28+
const { async_id_symbol } = require('internal/async_hooks').symbols;
3429

3530
// New Agent code.
3631

@@ -172,10 +167,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
172167
var socket = this.freeSockets[name].shift();
173168
// Guard against an uninitialized or user supplied Socket.
174169
if (socket._handle && typeof socket._handle.asyncReset === 'function') {
175-
if (destroyHooksExist() && socket._handle.getAsyncId()) {
176-
emitDestroy(socket._handle.getAsyncId());
177-
}
178-
// Assign the handle a new asyncId and run any init() hooks.
170+
// Assign the handle a new asyncId and run any destroy()/init() hooks.
179171
socket._handle.asyncReset();
180172
socket[async_id_symbol] = socket._handle.getAsyncId();
181173
}

src/async_wrap.cc

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,7 @@ void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
413413
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
414414
double execution_async_id =
415415
args[0]->IsNumber() ? args[0].As<Number>()->Value() : -1;
416-
// This assumes that when asyncReset is called from JS, it is always about
417-
// reusing an existing AsyncWrap instance and never to initialize a freshly
418-
// created AsyncWrap instance.
419-
wrap->AsyncReset(execution_async_id, false, true);
416+
wrap->AsyncReset(execution_async_id);
420417
}
421418

422419

@@ -566,6 +563,7 @@ AsyncWrap::AsyncWrap(Environment* env,
566563
CHECK_NE(provider, PROVIDER_NONE);
567564
CHECK_GE(object->InternalFieldCount(), 1);
568565

566+
async_id_ = -1;
569567
// Use AsyncReset() call to execute the init() callbacks.
570568
AsyncReset(execution_async_id, silent);
571569
}
@@ -608,16 +606,15 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
608606
// Generalized call for both the constructor and for handles that are pooled
609607
// and reused over their lifetime. This way a new uid can be assigned when
610608
// the resource is pulled out of the pool and put back into use.
611-
void AsyncWrap::AsyncReset(double execution_async_id,
612-
bool silent,
613-
bool reused) {
614-
if (reused) {
615-
// If this instance was in use before, we have already emitted an init with
609+
void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
610+
if (async_id_ != -1) {
611+
// This instance was in use before, we have already emitted an init with
616612
// its previous async_id and need to emit a matching destroy for that
617613
// before generating a new async_id.
618-
EmitDestroy(env(), get_async_id());
614+
EmitDestroy(env(), async_id_);
619615
}
620616

617+
// Now we can assign a new async_id_ to this instance.
621618
async_id_ =
622619
execution_async_id == -1 ? env()->new_async_id() : execution_async_id;
623620
trigger_async_id_ = env()->get_default_trigger_async_id();

src/async_wrap.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ class AsyncWrap : public BaseObject {
147147

148148
inline double get_trigger_async_id() const;
149149

150-
void AsyncReset(double execution_async_id = -1,
151-
bool silent = false,
152-
bool reused = false);
150+
void AsyncReset(double execution_async_id = -1, bool silent = false);
153151

154152
// Only call these within a valid HandleScope.
155153
v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,

src/node_http_parser.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ class Parser : public AsyncWrap, public StreamListener {
479479
// We must only call AsyncReset for the latter case, because AsyncReset has
480480
// already been called via the constructor for the former case.
481481
if (isReused) {
482-
parser->AsyncReset(-1, false, true);
482+
parser->AsyncReset();
483483
}
484484
parser->Init(type);
485485
}

0 commit comments

Comments
 (0)