nodejs / nodejs/undici

Response `body` iteration doesn't throw if aborted before beginning iteration

Open
#1,940 8 comments 1 reaction 1 assignee View on GitHub

@ronag is already working on this.

Since Feb 20, 2023.

bug
Dominant language
JavaScript
Stars
7.7k
Forks
880
Avg merge
2d 16h
Merged PRs (30d)
68

Description

Bug Description

Response body iteration doesn't throw if aborted before beginning iteration.

Reproducible By

import { fetch } from "undici";

class SomeError extends Error {
  constructor() {
    super();
    this.name = SomeError.name;
  }
}

class UnexpectedCodeReachedError extends Error {
  constructor() {
    super("expected code not to be reached");
    this.name = UnexpectedCodeReachedError.name;
  }
}

async function testError() {
  const ac = new AbortController();
  const response = await fetch("https://www.google.com", { signal: ac.signal });
  if (response.body === null) {
    throw new Error("expected non-null body");
  }
  ac.abort(new SomeError());
  try {
    for await (const chunk of response.body) {
      console.log();
    }
    throw new UnexpectedCodeReachedError();
  } catch (e) {
    if (e instanceof UnexpectedCodeReachedError) {
      console.error(e.message);
    } else if (!(e instanceof SomeError)) {
      console.error(
        `expected error (${e}) to be of type SomeError`,
      );
    } else {
      console.log("passed");
    }
  }
}

await testError();

Expected Behavior

I expect the behavior to stay consistent with Firefox and Deno, where the test "passes". There is also a test for respecting the abort() reason in there, which might not be a bug seeing how Chrome doesn't respect it either, but the fact that fetch() itself respects the reason, but not response.text() or response.body (while in Firefox and Deno it's respected) was a little confusing to me, shot myself in the foot with it.

Environment

Windows 11, Node.js 18.14.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.