diff --git a/lib/_http_server.js b/lib/_http_server.js index 2cddb8cf04d529..582054a58fe3e9 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -515,6 +515,9 @@ function setupConnectionsTracking() { this[kConnections] = new ConnectionsList(); } + if (this[kConnectionsCheckingInterval]) { + clearInterval(this[kConnectionsCheckingInterval]); + } // This checker is started without checking whether any headersTimeout or requestTimeout is non zero // otherwise it would not be started if such timeouts are modified after createServer. this[kConnectionsCheckingInterval] = diff --git a/test/parallel/test-http-server-clear-timer.js b/test/parallel/test-http-server-clear-timer.js new file mode 100644 index 00000000000000..b56334860355ea --- /dev/null +++ b/test/parallel/test-http-server-clear-timer.js @@ -0,0 +1,24 @@ +'use strict'; +const common = require('../common'); +const http = require('http'); +const assert = require('assert'); +const { kConnectionsCheckingInterval } = require('_http_server'); + +let i = 0; +let timer; +const server = http.createServer(); +server.on('listening', common.mustCall(() => { + // If there was a timer, it must be destroyed + if (timer) { + assert.ok(timer._destroyed); + } + // Save the last timer + timer = server[kConnectionsCheckingInterval]; + if (++i === 2) { + server.close(() => { + assert.ok(timer._destroyed); + }); + } +}, 2)); +server.emit('listening'); +server.emit('listening');