Skip to content

Unexpected error when using signal with reason in fetch #49557

Closed
nodejs/undici
#2597
@alexkorsun

Description

@alexkorsun

Version

18.17.1

Platform

Linux undefined 6.2.0-32-generic #32~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 18 10:40:13 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

fetch

What steps will reproduce the bug?

import express from 'express';

const app = express()
const port = 3333

app.get('/', (req, res) => {
    setTimeout(() => {
        res.send('Hey!')
    }, 10_000)
})

app.listen(port, () => {
    console.log(`App listening on port ${port}`)
})
async function test_signal() {
    try {
        const controller = new AbortController();
        const signal = controller.signal;

        setTimeout(() => {
            const reason = 'heeeey';

            // const reason = {
            //     reason: 'reason'
            // };
            // const reason = undefined;
            controller.abort(reason);
        }, 1_000);

        const res = await fetch('http://localhost:3333/', {signal: signal});

        await res.json()

        console.log('request succeeded > ', res)
    } catch (err) {
        if (err.name === 'AbortError') {
            console.log('request aborted > ', err);
        } else {
            console.log('request failed > ', err)
        }
    }
}

test_signal().catch(err => console.error('final error > ', err));

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

The expected behavior is to have [AbortError]: This operation was aborted with the reason obtained from the signal.

What do you see instead?

When reason = undefined

request aborted >  DOMException [AbortError]: This operation was aborted
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async test_signal (/home/alex/WebstormProjects/test/abort-request.js:16:21)

When reason = object

request failed >  { reason: 'reason' }

When reason = string

request failed >  TypeError: invalid_argument
    at Function.captureStackTrace (<anonymous>)
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async test_signal (/home/alex/WebstormProjects/test/abort-request.js:16:21)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    fetchIssues and PRs related to the Fetch API

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions