Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbortController throws inside p-limit #64

Open
UnlimitedBytes opened this issue Jul 16, 2022 · 5 comments
Open

AbortController throws inside p-limit #64

UnlimitedBytes opened this issue Jul 16, 2022 · 5 comments

Comments

@UnlimitedBytes
Copy link

Describe the bug

I created a simple basic application which uses node-fetch to send some HTTP(S)-Requests and uses p-limit to reduce the amount of requests send at the same time. The problem is when the node-fetch requests get aborted by an AbortController they throw inside p-limit so I can't handle the AbortError.

Reproduction

I'm providing a stripdown version of the application which only contains the parts necessary to trigger the bug.

Here is the stripdown version with p-limit

import fetch, { FormData, File } from 'node-fetch';
import pLimit from 'p-limit';

async function sendRequest(abortController) {
    try {
        const formData = new FormData();
        formData.set('file', new File(['Some text here..'], 'a.txt'));

        await fetch('https://example.com', {
            method: 'POST',
            body: formData,
            signal: abortController.signal,
        });
    } catch {
        console.error("Something wen't wrong doing request.");
    }
}

(async () => {
    const limit = pLimit(10);
    const abortController = new AbortController();

    const jobs = [];
    for (let i = 0; i < 10; i++) {
        jobs.push(limit(() => sendRequest(abortController)));
    }

    abortController.abort();

    await Promise.allSettled(jobs);
    console.log('Done sending all requests.');
})();

which generates the following output:

...
Something wen't wrong doing request.
Something wen't wrong doing request.
Done sending all requests.
node:events:515
      throw er; // Unhandled 'error' event
      ^

AbortError: The operation was aborted.

Here is the stripdown version without p-limit

import fetch, { FormData, File } from 'node-fetch';

async function sendRequest(abortController) {
    try {
        const formData = new FormData();
        formData.set('file', new File(['Some text here..'], 'a.txt'));

        await fetch('https://example.com', {
            method: 'POST',
            body: formData,
            signal: abortController.signal,
        });
    } catch {
        console.error("Something wen't wrong doing request.");
    }
}

(async () => {
    const abortController = new AbortController();

    const jobs = [];
    for (let i = 0; i < 10; i++) {
        jobs.push(sendRequest(abortController));
    }

    abortController.abort();

    await Promise.allSettled(jobs);
    console.log('Done sending all requests.');
})();

which generates the following output:

...
Something wen't wrong doing request.
Something wen't wrong doing request.
Done sending all requests.

Severity

blocking all usage of p-limit - as I can't abort requests without crashing the entire application.

Best regards
UnlimitedBytes

@UnlimitedBytes UnlimitedBytes changed the title AbortController throws inside plimit AbortController throws inside p-limit Jul 16, 2022
@UnlimitedBytes
Copy link
Author

@sindresorhus maybe?

@sindresorhus
Copy link
Owner

@UnlimitedBytes
Copy link
Author

BUMP @sindresorhus @copperwall please I will use the package but that blocks me from using it...

@Rinse12
Copy link

Rinse12 commented May 2, 2023

Any updates? I'm having the same issue

@tuhm1
Copy link

tuhm1 commented May 16, 2024

It's node-fetch's issue node-fetch/node-fetch#1801.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants