Skip to content

useH2c with connections > 1 causes HTTPParserError #4737

@rambo-panda

Description

@rambo-panda

Bug Description

When using undici's Agent with useH2c: true and connections > 1, it results in a HTTPParserError: Response does not match the HTTP/1.1 protocol error.

Reproducible By

Successful Case (connections=1)

const agent = new Agent({
useH2c: true,
connections: 1,// ✅ Works correctly
});

await request("http://localhost:3002", {
dispatcher: agent,
path: "/test",
});

Failure Case (connections>1)

const agent = new Agent({
useH2c: true,
connections: 2,// ❌ Fails
});

await Promise.all([
request("http://localhost:3002", { dispatcher: agent, path: "/test1" }),
request("http://localhost:3002", { dispatcher: agent, path: "/test2" }),
request("http://localhost:3002", { dispatcher: agent, path: "/test3" }),
]);

Expected Behavior

When useH2c: true is set, the Agent should properly handle HTTP/2 cleartext connections regardless of the connections parameter value, maintaining multiple HTTP/2 connections if specified.

Logs & Screenshots

Error Message

HTTPParserError: Response does not match the HTTP/1.1 protocol (Expected HTTP/, RTSP/ or ICE/)
at Parser.execute (/path/to/undici/lib/dispatcher/client-h1.js:361:17)
at Parser.readMore (/path/to/undici/lib/dispatcher/client-h1.js:301:12)
at Socket.onHttpSocketReadable (/path/to/undici/lib/dispatcher/client-h1.js:879:18)

Error data fragment: 0000000400000000000000080700000000000000 (HTTP/2 SETTINGS frame)

TCP Connection Monitoring

connections=1:

[TCP connection #1] Establishing connection
[TCP connection #1] ✅ Connection established
[TCP connection #1] 📥 Receiving HTTP/2 data
✅ Test passed

connections=2:

[TCP connection #2] Establishing connection
[TCP connection #3] Establishing connection
[TCP connection #2] ✅ Connection established
[TCP connection #3] ✅ Connection established
[TCP connection #2] 📥 Receiving data [HTTP/2 binary frame]: 0000000400000000...
[TCP connection #3] 📥 Receiving data [HTTP/2 binary frame]: 0000000400000000...
[TCP connection #3] 🔴 Connection closed
[TCP connection #4] Establishing connection ← Attempting reconnect with HTTP/1.1 parser
❌ HTTPParserError

Environment

  • Node.js version: v23.6.1
  • undici version: 7.18.2
  • Test scenario: HTTP/2 Cleartext (h2c) connection

Additional context

Root cause analysis:

  1. When connections > 1, Agent creates Pool instead of Client
  2. Pool's constructor doesn't explicitly handle useH2c option
  3. Pool doesn't pass useH2c to created Client instances
  4. Subsequent connections use HTTP/1.1 parser but receive HTTP/2 frames

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions