msw icon indicating copy to clipboard operation
msw copied to clipboard

Use of AbortSignal causes "Cannot construct a Request with a Request object that has already been used." error

Open ezzatron opened this issue 1 year ago • 4 comments

Prerequisites

Environment check

  • [X] I'm using the latest msw version
  • [X] I'm using Node.js version 18 or higher

Node.js version

v21.4.0

Reproduction repository

https://github.com/ezzatron/msw-abort-signal-repro

Reproduction steps

  1. npm ci
  2. npm test

Current behavior

Instead of producing an abort error, the repro code produces the following error, despite only a single request ever being made:

Cannot construct a Request with a Request object that has already been used.

See example output: https://github.com/ezzatron/msw-abort-signal-repro/actions/runs/7177213214/job/19543363576#step:5:25

Expected behavior

The reproduction code should produce an abort error consistent with use of an AbortSignal.

ezzatron avatar Dec 12 '23 05:12 ezzatron

You are using the controller.abort() before the fetch, the controller is already used, you can't reuse it after its been aborted. You need to switch the order, remove the await on the fetch request and put the controller.abort() right after the fetch, it should be caught in the .catch(error).

liorpevzner avatar Jan 11 '24 12:01 liorpevzner

You are using the controller.abort() before the fetch, the controller is already used, you can't reuse it after its been aborted.

I appreciate the reply, but I don't agree. An AbortSignal is not guaranteed to be aborted only after it is used in a fetch call, and the abort signal is not always created by the same code that uses it. It should not be causing MSW to change behaviour here.

As an example, fetch handles this just fine: https://codepen.io/ezzatron/pen/xxBEevM?editors=0010

To clarify, MSW should still be throwing an exception in this case. But it should be an abort error, not a generic error.

ezzatron avatar Jan 11 '24 21:01 ezzatron

so,any iead to resolve this problem?

master1lan avatar Jun 04 '24 06:06 master1lan

I'm facing this issue too. Would love to see the needle move on fixing this.

aaditmshah-commversion avatar Jun 21 '24 12:06 aaditmshah-commversion