Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: replace destroySoon with socket.end #36205

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
8 changes: 1 addition & 7 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,7 @@ function responseOnEnd() {
req._ended = true;

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) {
// We can assume `req.finished` means all data has been written since:
Expand Down
21 changes: 21 additions & 0 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,27 @@ tls_wrap.TLSWrap.prototype.close = function close(cb) {
return this._parent.close(done);
};

TLSSocket.prototype.destroySoon = function() {
if (this.writable)
this.end();

Copy link
Member

@ronag ronag Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this.end() is enough here. The code below is possibly redundant (at least in Node 14+).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also should we have a timeout waiting for 'close' here? @lpinca

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, the kernel will expire sockets stuck in FIN_WAIT_2 state as it has always been a problem and there was an old DoS attack that starved out the system of sockets by deliberately not closing them

if (this.writableFinished && this.readableEnded)
this.destroy();
else
this.once('finish', () => {
// TLS needs special handling when the server initiates the closing
// of the connection because a TLS-compliant client will send more data
// after receiving the server FIN
// If the server immediately destroys its socket, this data will trigger a
// RST packet in response
// https://github.com/nodejs/node/issues/36180
if (this.readableEnded)
this.destroy();
else
this.once('end', this.destroy);
});
};

TLSSocket.prototype.disableRenegotiation = function disableRenegotiation() {
this[kDisableRenegotiation] = true;
};
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
40 changes: 40 additions & 0 deletions test/parallel/test-https-end-rst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
const common = require('../common');

if (!common.hasCrypto)
common.skip('missing crypto');

const https = require('https');
const tls = require('tls');
const fixtures = require('../common/fixtures');

const opt = {
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem')
};

const data = 'hello';
const server = https.createServer(opt, common.mustCallAtLeast((req, res) => {
res.setHeader('content-length', data.length);
res.end(data);
}, 2));

server.listen(0, function() {
const options = {
host: '127.0.0.1',
port: server.address().port,
rejectUnauthorized: false,
ALPNProtocols: ['http/1.1'],
allowHalfOpen: true
};
const socket = tls.connect(options, common.mustCall(() => {
socket.write('GET /\n\n');
socket.once('data', common.mustCall(() => {
socket.write('GET /\n\n');
setTimeout(common.mustCall(() => {
socket.destroy();
server.close();
}), common.platformTimeout(10));
}));
}));
});