From 4e9ce7c0357f66b3902edb587c0b7971da286a32 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Tue, 19 Mar 2024 20:42:33 +0800 Subject: [PATCH] lib: make sure clear the old timer in http server PR-URL: https://github.com/nodejs/node/pull/52118 Reviewed-By: Paolo Insogna Reviewed-By: Matteo Collina --- lib/_http_server.js | 3 +++ test/parallel/test-http-server-clear-timer.js | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/parallel/test-http-server-clear-timer.js 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');