Skip to content

Keep connections open after HEAD requests #6271

Backlog

Description

@dblock

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, and Agent (for example resetAfterHead: false), so callers don't have to pass reset: false on every request.
  • Documenting reset: false as the way to keep connections open after HEAD, 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: false a HEAD response without Content-Length still closes the connection unless the server sends Connection: keep-alive, because llhttp doesn't keep such responses alive (see also Improve Keep-Alive #1861). With undici 8.14.0 and Agent({ connections: 1 }), three HEAD requests to a server answering HTTP/1.1 200 OK with no headers opened three connections; adding Content-Length: 5 or Connection: keep-alive brought it down to one.
  • The reset option isn't documented as affecting HEAD.

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

  1. dblock commented on Nov 29, 2026

    @dblock
    Author

    #6272 adds a test that a change to this default can be checked against. test/client-head-reset.js runs HEAD against a raw server, including one that sends a body with its HEAD responses, with the default, reset: true, and reset: 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 with pipelining above 1, is handed to the next request as its response. So the change should be narrower than flipping the default outright: keep the connection after HEAD only 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions