Skip to content

fetch will always hang up about 4s #2348

Description

@himself65

Bug Description

Upstream: nodejs/node#50188

Related: nodejs/node#43522, nodejs/node#48383

undici.fetch will still be hanging up even socket.end has been called, this will cause the node.js 18 server not to close at the right time.

node.js 19/20 won't have this issue because the server will always terminate all socket connections when calling server.close

Reproducible By

I did some small test code on node.js 20. Comparing undici.fetch with node-fetch

You will see that

const http = require("node:http")
const {promisify} = require("node:util")
const net = require("node:net");

async function test1() {
    const server = http.createServer(async (req, res) => {
        res.writeHead(200, {"Content-Type": "text/plain"});
        res.write("Hello world");
        res.end();
    });

    const listenPromisied = promisify(server.listen.bind(server));
    const closePromisied = promisify(net.Server.prototype.close.bind(server));

    await listenPromisied(0, "127.0.0.1");
    const address = server.address();
    const nodeFetch = (await import("node-fetch")).default
    await nodeFetch(`http://${address.address}:${address.port}`);
    console.time("server close");
    await closePromisied();
    console.timeEnd("server close");
}

async function test2() {
    const server = http.createServer(async (req, res) => {
        res.writeHead(200, {"Content-Type": "text/plain"});
        res.write("Hello world");
        res.end();
    });

    const listenPromisied = promisify(server.listen.bind(server));
    const closePromisied = promisify(net.Server.prototype.close.bind(server));

    await listenPromisied(0, "127.0.0.1");
    const address = server.address();
    const nodeFetch = (await import("undici")).fetch
    await nodeFetch(`http://${address.address}:${address.port}`);
    console.time("server close");
    await closePromisied();
    console.timeEnd("server close");
}

test1().then(() => test2());

Expected Behavior

closePromisied should finish in within 100ms

Logs & Screenshots

> git:(main) ✗ node index.cjs        
server close: 0.193ms
server close: 4.001s

Environment

undici >= 4.4.1

Node.js 18.x, 19.x, 20.x

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions