Skip to content

Aborting a fetch response stream throws uncatchable exceptionΒ #57360

Description

@hioman01

Version

22.12.0

Platform

Microsoft Windows NT 10.0.26100.0
x64

Subsystem

No response

What steps will reproduce the bug?

Suppose downloading a large file using fetch API.
When the response stream is being read and abort the fetch request afterwards, it will throw a uncatchable exception and crash the process.

import { Readable } from "stream";
import { finished } from "stream/promises";
import fs from "fs";

try {
  const res = await fetch("https://testfile.org/1.3GBiconpng", {
    signal: AbortSignal.timeout(5000),
  });
  const fileStream = fs.createWriteStream("./test");
  await finished(Readable.fromWeb(res.body).pipe(fileStream));
} catch (err) {
  // Cannot catch the abort error
  console.error("Error occurred!", err);
}

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

Always.

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

Error should be caught.

What do you see instead?

Uncaught exception is thrown. Process crashes.

node:events:502
      throw er; // Unhandled 'error' event
      ^
DOMException [TimeoutError]: The operation was aborted due to timeout
    at new DOMException (node:internal/per_context/domexception:53:5)
    at Timeout._onTimeout (node:internal/abort_controller:141:9)
    at listOnTimeout (node:internal/timers:594:17)
    at process.processTimers (node:internal/timers:529:7)
Emitted 'error' event on Readable instance at:
    at emitErrorNT (node:internal/streams/destroy:170:8)
    at emitErrorCloseNT (node:internal/streams/destroy:129:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:90:21)

Additional information

If you want to set a timeout for the fetch request in this case, you should use setTimeout and AbortController manually.

import { Readable } from "stream";
import { finished } from "stream/promises";
import fs from "fs";

const controller = new AbortController();
const id = setTimeout(() => {
  controller.abort();
}, 5000);
try {
  const res = await fetch("https://testfile.org/1.3GBiconpng", {
    signal: controller.signal,
  });
  clearTimeout(id);
  const fileStream = fs.createWriteStream("./test");
  await finished(Readable.fromWeb(res.body).pipe(fileStream));
} catch (err) {
  console.error("Error occurred!", err);
}

Activity

  1. geeksilva97 commented on Mar 8, 2025

    @geeksilva97
    Contributor

    Seems related to #55742

  2. github-actions commented on Apr 20, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  3. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 20, 2026
  4. github-actions commented on May 21, 2026

    @github-actions
    Contributor

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions