deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

`abortableAsyncIterable()` should call `.return()` on the generator once aborted

Open chromakode opened this issue 2 years ago • 1 comments

Describe the bug

Async generators canceled by abortableAsyncIterable don't have their return method called, so they don't get the opportunity to clean up resources.

https://github.com/denoland/deno_std/blob/d84f4b1c947b6b09df5e9a2e50e8f1549fe615fd/async/abortable.ts#L135-L139

Expected behavior

An example of a library which handles this is abortable-iterator:

https://github.com/alanshaw/abortable-iterator/blob/68c554cb40bd160216abb7200b54015aad0bd9a8/src/index.ts#L103-L106

Environment

  • std version: 1.36.0

chromakode avatar Aug 06 '23 06:08 chromakode

Contributions are welcome 🙂

iuioiua avatar Nov 23 '23 10:11 iuioiua

const stream = source
      .pipeThrough(new TextDecoderStream())
      .pipeThrough(new TextLineStream({ allowCR: true }))

try {
  for await (const message of abortable(stream, signal)) {
    this.dispatch({ message });
  }
} catch (e) {
  console.error(e)
} 

this results with:

error: Leaks detected:
  - A text decoder was created during the test, but not finished during the test. Close the text decoder by calling `textDecoder.decode('')` or `await textDecoderStream.readable.cancel()`.

and when trying to stream.cancel() in catch I get this:

TypeError: Cannot cancel a locked ReadableStream.

raaymax avatar Jul 27 '24 10:07 raaymax

@iuioiua Interestingly this works when stream is created by fetch for example but I'm having strange errors when using streams created in code but this is a problem with deno itself I think. Reported it here: https://github.com/denoland/deno/issues/19946#issuecomment-2254489046

raaymax avatar Aug 01 '24 10:08 raaymax