From 0f68125934125e7918724439c8fcc68902992b11 Mon Sep 17 00:00:00 2001 From: WhatCats Date: Fri, 18 Sep 2026 21:21:00 +0200 Subject: [PATCH] http: close connections that go idle after server.close() `server.close()` closes the connections that are idle at that moment. One that is sending a request or waiting for a response is left alone, and once its response finishes nothing rechecks it: it stays keep-alive until `keepAliveTimeout` reaps it. Mark the server as closing, and check that mark where a connection would otherwise be kept alive: the response stops advertising `Connection: keep-alive`, and the socket closes once that response finishes with nothing queued behind it. The mark is cleared when the server is listened on again. Signed-off-by: WhatCats --- doc/api/http.md | 9 +- lib/_http_outgoing.js | 10 +- lib/_http_server.js | 10 +- lib/internal/http.js | 1 + test/parallel/test-http-raw-headers.js | 4 +- .../test-http-server-close-when-idle.js | 105 ++++++++++++++++++ test/parallel/test-http-server-unconsume.js | 3 +- 7 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 test/parallel/test-http-server-close-when-idle.js diff --git a/doc/api/http.md b/doc/api/http.md index 5351b05eeaa9..8e2aefef062b 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1761,6 +1761,11 @@ unless the user specified another socket type.