Re: [whatwg/streams] Do the abort steps of ReadableStreamPipeTo really guarantee the abort callback to be called before cancel? (Issue #1229)

This happens because the writable stream is not yet [started](https://streams.spec.whatwg.org/commit-snapshots/e9355ce79925947e8eb496563d599c329769d315/#writablestreamdefaultcontroller-started). If you add a delay before you call abort and cancel, you get the events in the expected order:

```javascript
promise_test(async t => {
  const events = [];
  const rs = new ReadableStream({
    pull(controller) {
      controller.error('failed to abort');
    },
    cancel() {
      events.push('cancel');
      return Promise.reject(error1);
    }
  }, { highWaterMark: 0 });
  const ws = new WritableStream({
    abort() {
      events.push('abort');
      return Promise.reject(error2);
    }
  });
  await flushAsyncEvents(); // <<< the important bit
  await promise_rejects_exactly(t, error2, Promise.all([ws.abort(), rs.cancel()]), 'The abort rejection happens first in this case');
  assert_array_equals(events, ['abort', 'cancel'], 'abort() is called first in this case');
}, '#1229');
```
I agree that this is surprising: we don't wait for the readable stream to be started before we call its `cancel` method. But there's a reason for this: the `start()` method may a long-running async producer.
```javascript
let abortController = new AbortController();
const rs = new ReadableStream({
  async start(controller) {
    while (!abortController.signal.aborted) {
      controller.enqueue("a");
      await new Promise(r => setTimeout(r, 1000));
    }
  },
  cancel(reason) {
    controller.abort(reason);
  }
});
```
See also https://github.com/whatwg/streams/pull/1208#discussion_r793942484.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1229#issuecomment-1110256873
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/streams/issues/1229/1110256873@github.com>

Received on Tuesday, 26 April 2022 21:16:43 UTC