Repository navigation
Description
This would solve...
undici closes the connection after every HEAD request (client-h1.js), a decision made in #258 in 2020 to interoperate with servers that send a body in response to HEAD. That makes HEAD-heavy workloads, such as link checkers, open a new connection, and often a new TLS session, for every request.
In js-link-checker, which checks links with HEAD and falls back to GET, opting out with reset: false (src/request.js, dblock/js-link-checker#14) checks 5,000 links on one local host about 37% faster (577 ms to 361 ms median) and 1,000 links about 28% faster (116 ms to 84 ms).
Its Ruby counterpart, ruby-link-checker, never had to do anything about HEAD returning a body. Its recommended backend, Typhoeus::Hydra, runs on libcurl through Ethon, which only sets CURLOPT_NOBODY for HEAD and keeps the connection alive. libcurl handles misbehaving servers with two guards:
- A body that arrives for a request that did not want one marks the connection for closing (lib/sendf.c, "did not want a BODY").
- Before reusing an idle connection, it drops one with unread input pending, per RFC 9112 §9.2 (lib/conncache.c, "connection has no transfer but input, not healthy").
The async-http backend (protocol-http1) also keeps HEAD connections persistent and never reads a body for them.
undici now has the equivalent of the second guard, which it didn't have when #258 was decided: with pipelining 1 it waits a full event loop turn before reusing a connection (client-h1.js), and bytes that arrive with no request running destroy the socket with bad response (client-h1.js). The concern raised in #258, "what if the extra data contains data that looks like a proper response?", no longer applies to a stray body that arrives before the next request: the connection is dropped no matter what the bytes look like. js-link-checker tests this with a server that always sends a body with HEAD responses: every check succeeds, and undici opens a new connection for each one (test).
What remains is a body that arrives after the next request has already been written, which libcurl doesn't protect against either.
The implementation should look like...
Ideally, HEAD stops forcing socket[kReset] = true and relies on the idle-data guard, as libcurl does. If changing the default is too risky, either of these would help:
- An option on
Client,Pool, andAgent(for exampleresetAfterHead: false), so callers don't have to passreset: falseon every request. - Documenting
reset: falseas the way to keep connections open afterHEAD, along with its trade-off.
I have also considered...
Passing reset: false per request, which js-link-checker now does. Two details surprised me along the way:
- perf: allow keep alive for HEAD requests #1858 made this possible, but with
reset: falseaHEADresponse withoutContent-Lengthstill closes the connection unless the server sendsConnection: keep-alive, because llhttp doesn't keep such responses alive (see also Improve Keep-Alive #1861). With undici 8.14.0 andAgent({ connections: 1 }), threeHEADrequests to a server answeringHTTP/1.1 200 OKwith no headers opened three connections; addingContent-Length: 5orConnection: keep-alivebrought it down to one. - The
resetoption isn't documented as affectingHEAD.
Additional context
The deciding argument in #258 was that Node's http client closes after HEAD (nodejs/node#12396, closed in 2017 without a change). That's no longer the case in general: Node's http client today keeps the connection open after HEAD when the response has a Content-Length. Three HEAD requests with Node 26.13.0, http.Agent({ keepAlive: true, maxSockets: 1 }), and the same raw server:
HEAD response |
Node http |
undici with reset: false |
|---|---|---|
Content-Length, no body |
1 connection | 1 connection |
No Content-Length |
3 connections | 3 connections |
No Content-Length, Connection: keep-alive |
3 connections | 1 connection (#1858) |
Content-Length and a stray body |
3 connections, every request fails with HPE_INVALID_CONSTANT |
3 connections, every request succeeds |
So undici is the only one of the two that forces a close after every HEAD, even though its idle-data guard already handles a stray body better than Node does. The case nodejs/node#12396 reported, a HEAD response without Content-Length, still closes in both, because llhttp can't tell where such a response ends.
I'm happy to open a PR for whichever direction you prefer.
This issue was written by an AI (Claude) acting on behalf of @dblock.
Activity
dblock commentedon Nov 29, 2026
#6272 adds a test that a change to this default can be checked against.
test/client-head-reset.jsrunsHEADagainst a raw server, including one that sends a body with itsHEADresponses, with the default,reset: true, andreset: false, and checks both the responses and the number of connections. A PR that changes the default only needs to update the expectations for the default case, so the change in behavior is spelled out in the test.The test also narrows what I proposed above. With
reset: false, a body that arrives right after the headers is handled as libcurl handles it: the idle check drops the connection and every request succeeds. But a body that arrives after the next request was written, or any body withpipeliningabove 1, is handed to the next request as its response. So the change should be narrower than flipping the default outright: keep the connection afterHEADonly when pipelining is 1, and keep resetting it when pipelining is above 1. Even then, a body that arrives late at pipelining 1 still goes to the next request, the same gap libcurl has, and the test records that trade-off rather than hiding it.This comment was written by an AI (Claude) acting on behalf of @dblock.