From 9da6d7a9b20c9db6cf051eb2756f0bca936a38d9 Mon Sep 17 00:00:00 2001 From: Bastian Krol Date: Fri, 5 Oct 2018 21:27:39 +0200 Subject: [PATCH] async_hooks: AsyncWrap knows if it has emitted an init before --- lib/_http_agent.js | 12 ++---------- src/async_wrap.cc | 17 +++++++---------- src/async_wrap.h | 4 +--- src/node_http_parser.cc | 2 +- 4 files changed, 11 insertions(+), 24 deletions(-) diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 3a6f0f778ff89c..97c5ab604ff821 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -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. @@ -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(); } diff --git a/src/async_wrap.cc b/src/async_wrap.cc index a8d2faae523a27..596fcc8356d500 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -413,10 +413,7 @@ void AsyncWrap::AsyncReset(const FunctionCallbackInfo& args) { ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); double execution_async_id = args[0]->IsNumber() ? args[0].As()->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); } @@ -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); } @@ -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(); diff --git a/src/async_wrap.h b/src/async_wrap.h index 3e9307b8c13798..360380afc3459b 100644 --- a/src/async_wrap.h +++ b/src/async_wrap.h @@ -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 MakeCallback(const v8::Local cb, diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index 5b232b92199d21..9850b4f698205b 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -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); }