Skip to content

Commit 98d83ce

Browse files
Linkgoronsparist
authored andcommitted
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: nodejs#48373 PR-URL: nodejs#48383 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent ef85828 commit 98d83ce

6 files changed

+52
-3
lines changed

lib/_http_server.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ function setupConnectionsTracking(server) {
508508
setInterval(checkConnections.bind(server), server.connectionsCheckingInterval).unref();
509509
}
510510

511+
function httpServerPreClose(server) {
512+
server.closeIdleConnections();
513+
clearInterval(server[kConnectionsCheckingInterval]);
514+
}
515+
511516
function Server(options, requestListener) {
512517
if (!(this instanceof Server)) return new Server(options, requestListener);
513518

@@ -549,7 +554,7 @@ ObjectSetPrototypeOf(Server.prototype, net.Server.prototype);
549554
ObjectSetPrototypeOf(Server, net.Server);
550555

551556
Server.prototype.close = function() {
552-
clearInterval(this[kConnectionsCheckingInterval]);
557+
httpServerPreClose(this);
553558
ReflectApply(net.Server.prototype.close, this, arguments);
554559
};
555560

@@ -1188,4 +1193,6 @@ module.exports = {
11881193
storeHTTPOptions,
11891194
_connectionListener: connectionListener,
11901195
kServerResponse,
1196+
httpServerPreClose,
1197+
kConnectionsCheckingInterval,
11911198
};

lib/https.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
JSONStringify,
3232
ObjectAssign,
3333
ObjectSetPrototypeOf,
34+
ReflectApply,
3435
ReflectConstruct,
3536
} = primordials;
3637

@@ -43,6 +44,7 @@ assertCrypto();
4344
const tls = require('tls');
4445
const { Agent: HttpAgent } = require('_http_agent');
4546
const {
47+
httpServerPreClose,
4648
Server: HttpServer,
4749
setupConnectionsTracking,
4850
storeHTTPOptions,
@@ -97,6 +99,11 @@ Server.prototype.closeIdleConnections = HttpServer.prototype.closeIdleConnection
9799

98100
Server.prototype.setTimeout = HttpServer.prototype.setTimeout;
99101

102+
Server.prototype.close = function() {
103+
httpServerPreClose(this);
104+
ReflectApply(tls.Server.prototype.close, this, arguments);
105+
};
106+
100107
/**
101108
* Creates a new `https.Server` instance.
102109
* @param {{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { createServer } = require('http');
5+
const { kConnectionsCheckingInterval } = require('_http_server');
6+
7+
const server = createServer(function(req, res) {});
8+
server.listen(0, common.mustCall(function() {
9+
assert.strictEqual(server[kConnectionsCheckingInterval]._destroyed, false);
10+
server.close(common.mustCall(() => {
11+
assert(server[kConnectionsCheckingInterval]._destroyed);
12+
}));
13+
}));

test/parallel/test-http-server-close-idle.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ server.listen(0, function() {
4242
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));
4343
assert.strictEqual(connections, 2);
4444

45-
server.closeIdleConnections();
4645
server.close(common.mustCall());
4746

4847
// Check that only the idle connection got closed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
if (!common.hasCrypto) {
5+
common.skip('missing crypto');
6+
}
7+
8+
const { createServer } = require('https');
9+
const { kConnectionsCheckingInterval } = require('_http_server');
10+
11+
const fixtures = require('../common/fixtures');
12+
13+
const options = {
14+
key: fixtures.readKey('agent1-key.pem'),
15+
cert: fixtures.readKey('agent1-cert.pem')
16+
};
17+
18+
const server = createServer(options, function(req, res) {});
19+
server.listen(0, common.mustCall(function() {
20+
assert.strictEqual(server[kConnectionsCheckingInterval]._destroyed, false);
21+
server.close(common.mustCall(() => {
22+
assert(server[kConnectionsCheckingInterval]._destroyed);
23+
}));
24+
}));

test/parallel/test-https-server-close-idle.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ server.listen(0, function() {
5252
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));
5353
assert.strictEqual(connections, 2);
5454

55-
server.closeIdleConnections();
5655
server.close(common.mustCall());
5756

5857
// Check that only the idle connection got closed

0 commit comments

Comments
 (0)