Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/internal/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,13 @@ Server.prototype.closeIdleConnections = function closeIdleConnections() {
return;
}

// Get connections which don't have an incomplete incoming request.
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) {
// Skip connections with incomplete outgoing responses.
if (connections[i].socket._httpMessage &&
!connections[i].socket._httpMessage.writableFinished) {
continue;
}

Expand Down
64 changes: 64 additions & 0 deletions test/parallel/test-http-server-close-idle-pending-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

// Test that server.close() (which calls closeIdleConnections()) does not
// destroy a connection whose response has been ended but has not yet been
// fully written to the socket. Such a response has `finished` set, but is
// not `writableFinished`; destroying the socket would drop the queued data
// and truncate the response.

const common = require('../common');
const assert = require('assert');
const { createServer, get } = require('http');

const body = 'hello';

const server = createServer(common.mustCall((req, res) => {
// Wait for the request to be fully received, so that the connection is
// idle as far as the parser is concerned and the sweep considers it.
req.resume();
req.on('end', common.mustCall(() => {
const socket = res.socket;
res.writeHead(200, { 'Content-Length': body.length });
res.flushHeaders();

// Keep the body queued in the socket's write buffer while server.close()
// runs: cork the socket, and make the uncork() that end() performs a
// no-op. (Writing a body larger than the socket buffers is not a reliable
// way to leave data queued, because on some platforms, including Windows,
// the kernel may accept all of it synchronously.)
socket.cork();
// uncork() is inherited, so it is restored below by deleting the override.
assert.ok(!Object.hasOwn(socket, 'uncork'));
socket.uncork = () => {};
res.end(body, common.mustCall());
assert.strictEqual(res.finished, true);
assert.strictEqual(res.writableFinished, false);
assert.ok(socket.writableLength >= body.length);

// The response is still being written, so this must leave the connection
// alone and wait for it.
server.close(common.mustCall());

// Now let the body be written.
delete socket.uncork;
socket.uncork();
}));
}));

server.listen(0, common.mustCall(() => {
// No keep-alive agent: once the response is complete the client closes the
// connection, so server.close() finishes without waiting for an idle
// keep-alive connection to time out.
get({ port: server.address().port, agent: false }, common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
let received = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
received += chunk;
});
res.on('error', common.mustNotCall());
res.on('end', common.mustCall(() => {
assert.strictEqual(received, body);
}));
}));
}));
Loading