From 3d9d1ade2a361f408b116c5bafb2fcd560310f9c Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> Date: Sun, 5 May 2019 21:29:32 +0200 Subject: async_hooks: don't reuse resource in HttpAgent As discussed in https://github.com/nodejs/diagnostics/issues/248, https://github.com/nodejs/node/pull/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: https://github.com/nodejs/node/pull/27581 Reviewed-By: Matteo Collina Reviewed-By: Rich Trott --- lib/_http_agent.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'lib/_http_agent.js') diff --git a/lib/_http_agent.js b/lib/_http_agent.js index eb98f2b0bd..12f8529c38 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -40,6 +40,13 @@ const { async_id_symbol } = require('internal/async_hooks').symbols; // ClientRequest.onSocket(). The Agent is now *strictly* // concerned with managing a connection pool. +class ReusedHandle { + constructor(type, handle) { + this.type = type; + this.handle = handle; + } +} + function Agent(options) { if (!(this instanceof Agent)) return new Agent(options); @@ -166,10 +173,11 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */, // We have a free socket, so use that. var socket = this.freeSockets[name].shift(); // Guard against an uninitialized or user supplied Socket. - if (socket._handle && typeof socket._handle.asyncReset === 'function') { + const handle = socket._handle; + if (handle && typeof handle.asyncReset === 'function') { // Assign the handle a new asyncId and run any destroy()/init() hooks. - socket._handle.asyncReset(); - socket[async_id_symbol] = socket._handle.getAsyncId(); + handle.asyncReset(new ReusedHandle(handle.getProviderType(), handle)); + socket[async_id_symbol] = handle.getAsyncId(); } // don't leak -- cgit v1.2.3