From e564fd0e9fb7a5028a4517a67cecaf845b681119 Mon Sep 17 00:00:00 2001 From: Kelvin Jin Date: Wed, 3 May 2017 14:04:45 -0700 Subject: [PATCH] http2: client session destroy also destroys associated socket PR-URL: https://github.com/nodejs/http2/pull/64 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/internal/http2/core.js | 13 ++++++++++--- test/parallel/test-http2-create-client-connect.js | 2 +- test/parallel/test-http2-create-client-session.js | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 6afc87e6f7..3e14dc0336 100755 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -435,7 +435,16 @@ class Http2Session extends EventEmitter { destroy() { const state = this[kState]; const streams = state.streams; + const socket = this[kSocket]; + if (state.destroyed) { + return; + } state.destroyed = true; + if (!socket.destroyed) { + socket.destroy(); + } + this[kSocket] = undefined; + this[kServer] = undefined; timers.unenroll(this); streams.forEach((value, key) => { value[kSession] = undefined; @@ -955,12 +964,10 @@ Object.defineProperties(Http2Session.prototype, { // establishment of a new connection. function socketDestroy(error) { const session = this[kSession]; - session.destroy(); - session[kServer] = undefined; - session[kSocket] = undefined; this[kServer] = undefined; this.destroy = this[kDestroySocket]; this.destroy(error); + session.destroy(); } function socketOnResume() { diff --git a/test/parallel/test-http2-create-client-connect.js b/test/parallel/test-http2-create-client-connect.js index e9b394858e..5a548dcb43 100755 --- a/test/parallel/test-http2-create-client-connect.js +++ b/test/parallel/test-http2-create-client-connect.js @@ -28,7 +28,7 @@ const URL = url.URL; let count = items.length; const maybeClose = common.mustCall((client) => { - client.socket.destroy(); + client.destroy(); if (--count === 0) { setImmediate(() => server.close()); } diff --git a/test/parallel/test-http2-create-client-session.js b/test/parallel/test-http2-create-client-session.js index ba826d9987..ea71a40492 100755 --- a/test/parallel/test-http2-create-client-session.js +++ b/test/parallel/test-http2-create-client-session.js @@ -51,7 +51,7 @@ server.on('listening', common.mustCall(function() { assert.strictEqual(body, data); if (--expected === 0) { server.close(); - client.socket.destroy(); + client.destroy(); } })); req.end();