From d203481c80156a16395a4e159e8b638c954c0b70 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Thu, 28 Sep 2017 10:30:07 +0200 Subject: [PATCH] http: do not blindly destroy UNIX domain sockets `Connection: keep-alive` is now properly supported when making client connections to UNIX domain sockets so `request.abort()` should not blindly destroy the underlying socket. PR-URL: https://github.com/nodejs/node/pull/15650 Refs: https://github.com/nodejs/node/pull/13214#issuecomment-304049523 Reviewed-By: Anatoli Papirovski --- lib/_http_client.js | 2 +- ...ent-abort-keep-alive-queued-tcp-socket.js} | 0 ...ent-abort-keep-alive-queued-unix-socket.js | 38 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) rename test/parallel/{test-http-abort-queued-2.js => test-http-client-abort-keep-alive-queued-tcp-socket.js} (100%) create mode 100644 test/parallel/test-http-client-abort-keep-alive-queued-unix-socket.js diff --git a/lib/_http_client.js b/lib/_http_client.js index da98f2669c..fedadedcc2 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -663,7 +663,7 @@ ClientRequest.prototype.onSocket = function onSocket(socket) { function onSocketNT(req, socket) { if (req.aborted) { // If we were aborted while waiting for a socket, skip the whole thing. - if (req.socketPath || !req.agent) { + if (!req.agent) { socket.destroy(); } else { socket.emit('free'); diff --git a/test/parallel/test-http-abort-queued-2.js b/test/parallel/test-http-client-abort-keep-alive-queued-tcp-socket.js similarity index 100% rename from test/parallel/test-http-abort-queued-2.js rename to test/parallel/test-http-client-abort-keep-alive-queued-tcp-socket.js diff --git a/test/parallel/test-http-client-abort-keep-alive-queued-unix-socket.js b/test/parallel/test-http-client-abort-keep-alive-queued-unix-socket.js new file mode 100644 index 0000000000..efcbfe8dc5 --- /dev/null +++ b/test/parallel/test-http-client-abort-keep-alive-queued-unix-socket.js @@ -0,0 +1,38 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +let socketsCreated = 0; + +class Agent extends http.Agent { + createConnection(options, oncreate) { + const socket = super.createConnection(options, oncreate); + socketsCreated++; + return socket; + } +} + +const server = http.createServer((req, res) => res.end()); + +const socketPath = common.PIPE; +common.refreshTmpDir(); + +server.listen(socketPath, common.mustCall(() => { + const agent = new Agent({ + keepAlive: true, + maxSockets: 1 + }); + + http.get({ agent, socketPath }, (res) => res.resume()); + + const req = http.get({ agent, socketPath }, common.mustNotCall()); + req.abort(); + + http.get({ agent, socketPath }, common.mustCall((res) => { + res.resume(); + assert.strictEqual(socketsCreated, 1); + agent.destroy(); + server.close(); + })); +}));