Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,11 +709,7 @@ function responseOnEnd() {

if (!req.shouldKeepAlive) {
if (socket.writable) {
debug('AGENT socket.destroySoon()');
if (typeof socket.destroySoon === 'function')
socket.destroySoon();
else
socket.end();
socket.end();
}
assert(!socket.writable);
} else if (req.finished && !this.aborted) {
Expand Down
6 changes: 1 addition & 5 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,11 +814,7 @@ function resOnFinish(req, res, socket, state, server) {
process.nextTick(emitCloseNT, res);

if (res._last) {
if (typeof socket.destroySoon === 'function') {
socket.destroySoon();
} else {
socket.end();
}
socket.end();
} else if (state.outgoing.length === 0) {
if (server.keepAliveTimeout && typeof socket.setTimeout === 'function') {
socket.setTimeout(server.keepAliveTimeout);
Expand Down
14 changes: 12 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,20 @@ Socket.prototype.destroySoon = function() {
if (this.writable)
this.end();

if (this.writableFinished)
if (this.writableFinished && this.readableEnded)
this.destroy();
else
this.once('finish', this.destroy);
this.once('finish', () => {
// If we destroy the socket before we have received an incoming EOF
// the remote might still be sending data that will trigger a RST
// from this host once it is received
// Closing a TCP socket requires two FIN/ACK exchanges, one in every
// direction - this means two events
if (this.readableEnded)
this.destroy();
else
this.once('end', this.destroy);
});
};


Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-should-keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const countdown = new Countdown(SHOULD_KEEP_ALIVE.length, () => server.close());
const getCountdownIndex = () => SERVER_RESPONSES.length - countdown.remaining;

const server = net.createServer(function(socket) {
socket.write(SERVER_RESPONSES[getCountdownIndex()]);
socket.end(SERVER_RESPONSES[getCountdownIndex()]);
}).listen(0, function() {
function makeRequest() {
const req = http.get({ port: server.address().port }, function(res) {
Expand Down