Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

`fetchEventSource` doesn't abort when passing in an already aborted signal

オープン
#98 コメント 0 件 リアクション 9 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
typescript
領域
api

調査の方向性

Issue に示されている fetchEventSource の実装から始め、すでに abort された AbortController シグナルでこのケースを再現します。リクエストが直ちに abort され、abort エラーで reject されることを確認してから、該当するテストの場所を特定できる場合は、この動作のカバレッジを追加または更新します。

索引モデルが issue の本文から書いたものです。

説明

Javascript's default fetch rejects with an AbortError when passing an already aborted signal:

const controller = new AbortController()
controller.abort()
await fetch("https://google.com", { signal: controller.signal })
Uncaught:
DOMException [AbortError]: This operation was aborted
    at node:internal/deps/undici/undici:13392:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async REPL17:1:33

However, when using fetch-event-source it fails to detect that the signal is already aborted:

const controller = new AbortController()
controller.abort()
await fetchEventSource("https://any-website-supporting-sse.com", { signal: controller.signal })
// the call above is never aborted

This doesn't abort the fetch.

The reason is that fetchEventSource creates a new aborter and subscribes to the original signal's abort event to then abort its own controller, but the abort event does not trigger retroactively (i.e. if the signal is already aborted). Here's the relevant snippet from the fetchEventSource implementation:

let curRequestController: AbortController;

function dispose() {
  // ...
  curRequestController.abort();
}

inputSignal?.addEventListener('abort', () => {
  // this callback is not called if the inputSignal is already aborted!
  // hence, curRequestController is not aborted
  dispose();
  resolve();
});

async function create() {
  curRequestController = new AbortController();
    try {
      const response = await fetch(input, {
        ...rest,
        headers,
        signal: curRequestController.signal,
      });
      // ...
    } catch (err) {
      if (!curRequestController.signal.aborted) {
        // ...
      }
    }
};

I patched it locally as follows:

inputSignal?.addEventListener('abort', () => {
  dispose();
  // no longer resolving the promise here
  // because 1) we should reject instead of resolve when aborted
  // and 2) dispose() will abort the curRequestController.signal
  // which will cause the ongoing fetch request to throw an AbortError
  // which we will catch and then we will reject the promise with the error
});

async function create() {
  curRequestController = new AbortController();
  // use the input signal if it is already aborted
  const sig = inputSignal.aborted ? inputSignal : curRequestController.signal
    try {
      const response = await fetch(input, {
        ...rest,
        headers,
        signal: sig,
      });
      // ...
    } catch (err) {
      if (sig.aborted) {
        dispose();
        reject(err);
      } else if (!curRequestController.signal.aborted) {
        // ...
      }
    }
};
主要言語
TypeScript
スター
2.9k
フォーク
197
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Azure/fetch-event-source のほかの issue

Azure/fetch-event-source の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。