Skip to content

Commit

Permalink
http: start connections checking interval on listen
Browse files Browse the repository at this point in the history
Fixes: nodejs#48604.
  • Loading branch information
ShogunPanda committed Jun 30, 2023
1 parent 32eb492 commit e5c4298
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ function storeHTTPOptions(options) {
}

function setupConnectionsTracking(server) {
if (!server) {
server = this;
}

// Start connection handling
server[kConnections] = new ConnectionsList();

Expand Down Expand Up @@ -542,11 +546,12 @@ function Server(options, requestListener) {
this.httpAllowHalfOpen = false;

this.on('connection', connectionListener);
this.on('listening', setupConnectionsTracking, this);

this.timeout = 0;
this.maxHeadersCount = null;
this.maxRequestsPerSocket = 0;
setupConnectionsTracking(this);

this[kUniqueHeaders] = parseUniqueHeadersOption(options.uniqueHeaders);
}
ObjectSetPrototypeOf(Server.prototype, net.Server.prototype);
Expand All @@ -558,15 +563,15 @@ Server.prototype.close = function() {
};

Server.prototype.closeAllConnections = function() {
const connections = this[kConnections].all();
const connections = this[kConnections]?.all() ?? [];

for (let i = 0, l = connections.length; i < l; i++) {
connections[i].socket.destroy();
}
};

Server.prototype.closeIdleConnections = function() {
const connections = this[kConnections].idle();
const connections = this[kConnections]?.idle() ?? [];

for (let i = 0, l = connections.length; i < l; i++) {
if (connections[i].socket._httpMessage && !connections[i].socket._httpMessage.finished) {
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-http-server-connections-checking-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

// Flags: --expose-gc

require('../common');
const assert = require('assert');
const http = require('http');

// Check that creating a server without listening is not leaking resources.

const step = 10000;
const max = 100000;

const usages = [];

for (let i = 0; i < max; i++) {
if (i % 100 === 0) {
global.gc();
}

if (i % step === 0) {
usages.push(process.memoryUsage());
}

http.createServer((req, res) => {});
}

for (let i = 1; i < usages.length; i++) {
const current = usages[i].heapTotal;
const reference = usages[0].heapTotal;
assert.ok(current / reference < 1.1, `memory usage is increasing (${i}): ${current} > ${reference} * 1.1`);
}

0 comments on commit e5c4298

Please sign in to comment.