Skip to content

Commit

Permalink
async_hooks: AsyncWrap knows if it has emitted an init before
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed Oct 5, 2018
1 parent 5335c53 commit 9da6d7a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
12 changes: 2 additions & 10 deletions lib/_http_agent.js
Expand Up @@ -25,12 +25,7 @@ const net = require('net');
const util = require('util');
const EventEmitter = require('events');
const debug = util.debuglog('http');
const {
destroyHooksExist,
emitDestroy,
symbols
} = require('internal/async_hooks');
const async_id_symbol = symbols.async_id_symbol;
const { async_id_symbol } = require('internal/async_hooks').symbols;

// New Agent code.

Expand Down Expand Up @@ -172,10 +167,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
var socket = this.freeSockets[name].shift();
// Guard against an uninitialized or user supplied Socket.
if (socket._handle && typeof socket._handle.asyncReset === 'function') {
if (destroyHooksExist() && socket._handle.getAsyncId()) {
emitDestroy(socket._handle.getAsyncId());
}
// Assign the handle a new asyncId and run any init() hooks.
// Assign the handle a new asyncId and run any destroy()/init() hooks.
socket._handle.asyncReset();
socket[async_id_symbol] = socket._handle.getAsyncId();
}
Expand Down
17 changes: 7 additions & 10 deletions src/async_wrap.cc
Expand Up @@ -413,10 +413,7 @@ void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
double execution_async_id =
args[0]->IsNumber() ? args[0].As<Number>()->Value() : -1;
// This assumes that when asyncReset is called from JS, it is always about
// reusing an existing AsyncWrap instance and never to initialize a freshly
// created AsyncWrap instance.
wrap->AsyncReset(execution_async_id, false, true);
wrap->AsyncReset(execution_async_id);
}


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

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

// Now we can assign a new async_id_ to this instance.
async_id_ =
execution_async_id == -1 ? env()->new_async_id() : execution_async_id;
trigger_async_id_ = env()->get_default_trigger_async_id();
Expand Down
4 changes: 1 addition & 3 deletions src/async_wrap.h
Expand Up @@ -147,9 +147,7 @@ class AsyncWrap : public BaseObject {

inline double get_trigger_async_id() const;

void AsyncReset(double execution_async_id = -1,
bool silent = false,
bool reused = false);
void AsyncReset(double execution_async_id = -1, bool silent = false);

// Only call these within a valid HandleScope.
v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,
Expand Down
2 changes: 1 addition & 1 deletion src/node_http_parser.cc
Expand Up @@ -479,7 +479,7 @@ class Parser : public AsyncWrap, public StreamListener {
// We must only call AsyncReset for the latter case, because AsyncReset has
// already been called via the constructor for the former case.
if (isReused) {
parser->AsyncReset(-1, false, true);
parser->AsyncReset();
}
parser->Init(type);
}
Expand Down

0 comments on commit 9da6d7a

Please sign in to comment.