Skip to content

Unable to use h2c over a unix domain socket #4689

@chrros95

Description

@chrros95

Bug Description

When using the H2CClient with a socket path, an error is thrown that the port is not defined.
Due to the fact that any request is not completed.

Reproducible By

const { once } = require('events');
const { createServer } = require('http2');
const {H2CClient} = require('.');
const { unlinkSync } = require('fs');

let server
const test = async () => {
    const socketPath = '/tmp/server.sock';
    server = createServer((req, res) => {
        res.writeHead(200)
        res.end('Hello, world!')
    })

    server.listen(socketPath)
    await once(server, 'listening')
    const client = new H2CClient('http://localhost', {
        socketPath
    })
    await client.request({ path: '/', method: 'GET' })
}

test().catch((e) => { console.error('H2c failed:', e) }).finally(() => {
    server.close()
})

Expected Behavior

It is expected that the H2CClient connects and fulfils the request.
Basically, it should behave like the H1 client.

Logs & Screenshots

(base) ➜  undici git:(main) ✗ set -x ; cat test.cjs ; node test.cjs  ; set +x
+/usr/bin/zsh:80> cat test.cjs
const { once } = require('events');
const { createServer } = require('http2');
const {H2CClient} = require('.');
const { unlinkSync } = require('fs');

let server
const test = async () => {
    const socketPath = '/tmp/server.sock';
    server = createServer((req, res) => {
        res.writeHead(200)
        res.end('Hello, world!')
    })

    server.listen(socketPath)
    await once(server, 'listening')
    const client = new H2CClient('http://localhost', {
        socketPath
    })
    await client.request({ path: '/', method: 'GET' })
}

test().catch((e) => { console.error('H2c failed:', e) }).finally(() => {
    server.close()
})
+/usr/bin/zsh:80> node test.cjs
H2c failed: RangeError [ERR_SOCKET_BAD_PORT]: Port should be >= 0 and < 65536. Received type string ('').
    at lookupAndConnect (node:net:1330:5)
    at Socket.connect (node:net:1285:5)
    at connect (node:net:245:17)
    at Client.<anonymous> (/home/user/git/undici/lib/dispatcher/h2c-client.js:63:22)
    at connect (/home/user/git/undici/lib/dispatcher/client.js:418:21)
    at _resume (/home/user/git/undici/lib/dispatcher/client.js:593:7)
    at resume (/home/user/git/undici/lib/dispatcher/client.js:527:3)
    at Client.<computed> (/home/user/git/undici/lib/dispatcher/client.js:251:31)
    at [dispatch] (/home/user/git/undici/lib/dispatcher/client.js:308:20)
    at Client.dispatch (/home/user/git/undici/lib/dispatcher/dispatcher-base.js:152:29) {
  code: 'ERR_SOCKET_BAD_PORT'
}
+/usr/bin/zsh:80> set +x

Environment

OS: Debain 13.1
Linux 3453fb60ea28 6.8.0-79-generic #79-Ubuntu SMP PREEMPT_DYNAMIC Tue Aug 12 14:42:46 UTC 2025 x86_64 GNU/Linux
node: v24.9.0
undici: latest (repo @ 5024d1b)

Additional context

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    H2Pull requests or issues related to HTTP/2enhancementNew feature or requestgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions