From bd7a8087a581557c636525b65a5c0e0db58735e7 Mon Sep 17 00:00:00 2001 From: linkgoron Date: Mon, 12 Jun 2023 10:47:25 +0300 Subject: [PATCH] https: fix connection checking interval not clearing on server close The connection interval should close when httpsServer.close is called similarly to how it gets cleared when httpServer.close is called. fixes: https://github.com/nodejs/node/issues/48373 PR-URL: https://github.com/nodejs/node/pull/48383 Reviewed-By: Moshe Atlow Reviewed-By: Paolo Insogna Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca Reviewed-By: Marco Ippolito Reviewed-By: Minwoo Jung --- lib/_http_server.js | 10 ++++++-- lib/https.js | 7 ++++++ .../test-http-server-close-destroy-timeout.js | 13 ++++++++++ test/parallel/test-http-server-close-idle.js | 1 - ...test-https-server-close-destroy-timeout.js | 24 +++++++++++++++++++ test/parallel/test-https-server-close-idle.js | 1 - 6 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-http-server-close-destroy-timeout.js create mode 100644 test/parallel/test-https-server-close-destroy-timeout.js diff --git a/lib/_http_server.js b/lib/_http_server.js index c6a070115569ad..d72faed56e6056 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -507,6 +507,11 @@ function setupConnectionsTracking(server) { setInterval(checkConnections.bind(server), server.connectionsCheckingInterval).unref(); } +function httpServerPreClose(server) { + server.closeIdleConnections(); + clearInterval(server[kConnectionsCheckingInterval]); +} + function Server(options, requestListener) { if (!(this instanceof Server)) return new Server(options, requestListener); @@ -548,8 +553,7 @@ ObjectSetPrototypeOf(Server.prototype, net.Server.prototype); ObjectSetPrototypeOf(Server, net.Server); Server.prototype.close = function() { - this.closeIdleConnections(); - clearInterval(this[kConnectionsCheckingInterval]); + httpServerPreClose(this); ReflectApply(net.Server.prototype.close, this, arguments); }; @@ -1193,4 +1197,6 @@ module.exports = { storeHTTPOptions, _connectionListener: connectionListener, kServerResponse, + httpServerPreClose, + kConnectionsCheckingInterval, }; diff --git a/lib/https.js b/lib/https.js index ad67a2f7244bd6..ca695555640a32 100644 --- a/lib/https.js +++ b/lib/https.js @@ -31,6 +31,7 @@ const { JSONStringify, ObjectAssign, ObjectSetPrototypeOf, + ReflectApply, ReflectConstruct, } = primordials; @@ -43,6 +44,7 @@ assertCrypto(); const tls = require('tls'); const { Agent: HttpAgent } = require('_http_agent'); const { + httpServerPreClose, Server: HttpServer, setupConnectionsTracking, storeHTTPOptions, @@ -103,6 +105,11 @@ Server.prototype.closeIdleConnections = HttpServer.prototype.closeIdleConnection Server.prototype.setTimeout = HttpServer.prototype.setTimeout; +Server.prototype.close = function() { + httpServerPreClose(this); + ReflectApply(tls.Server.prototype.close, this, arguments); +}; + /** * Creates a new `https.Server` instance. * @param {{ diff --git a/test/parallel/test-http-server-close-destroy-timeout.js b/test/parallel/test-http-server-close-destroy-timeout.js new file mode 100644 index 00000000000000..b1138ee36d5a90 --- /dev/null +++ b/test/parallel/test-http-server-close-destroy-timeout.js @@ -0,0 +1,13 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { createServer } = require('http'); +const { kConnectionsCheckingInterval } = require('_http_server'); + +const server = createServer(function(req, res) {}); +server.listen(0, common.mustCall(function() { + assert.strictEqual(server[kConnectionsCheckingInterval]._destroyed, false); + server.close(common.mustCall(() => { + assert(server[kConnectionsCheckingInterval]._destroyed); + })); +})); diff --git a/test/parallel/test-http-server-close-idle.js b/test/parallel/test-http-server-close-idle.js index af99d1b42c20ea..bb18cf8b00ef43 100644 --- a/test/parallel/test-http-server-close-idle.js +++ b/test/parallel/test-http-server-close-idle.js @@ -42,7 +42,6 @@ server.listen(0, function() { assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive')); assert.strictEqual(connections, 2); - server.closeIdleConnections(); server.close(common.mustCall()); // Check that only the idle connection got closed diff --git a/test/parallel/test-https-server-close-destroy-timeout.js b/test/parallel/test-https-server-close-destroy-timeout.js new file mode 100644 index 00000000000000..e876721f610964 --- /dev/null +++ b/test/parallel/test-https-server-close-destroy-timeout.js @@ -0,0 +1,24 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +if (!common.hasCrypto) { + common.skip('missing crypto'); +} + +const { createServer } = require('https'); +const { kConnectionsCheckingInterval } = require('_http_server'); + +const fixtures = require('../common/fixtures'); + +const options = { + key: fixtures.readKey('agent1-key.pem'), + cert: fixtures.readKey('agent1-cert.pem') +}; + +const server = createServer(options, function(req, res) {}); +server.listen(0, common.mustCall(function() { + assert.strictEqual(server[kConnectionsCheckingInterval]._destroyed, false); + server.close(common.mustCall(() => { + assert(server[kConnectionsCheckingInterval]._destroyed); + })); +})); diff --git a/test/parallel/test-https-server-close-idle.js b/test/parallel/test-https-server-close-idle.js index 62a01ac743f4b0..9a9f3758746181 100644 --- a/test/parallel/test-https-server-close-idle.js +++ b/test/parallel/test-https-server-close-idle.js @@ -52,7 +52,6 @@ server.listen(0, function() { assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive')); assert.strictEqual(connections, 2); - server.closeIdleConnections(); server.close(common.mustCall()); // Check that only the idle connection got closed