Skip to content

Unable to abort a dart:io http request while the connection is being established. #51267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
roblframpton opened this issue Feb 6, 2023 · 2 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io type-enhancement A request for a change that isn't a bug

Comments

@roblframpton
Copy link

In 2020, an abort method was added to the HttpClientRequest object in the dart:io library, allowing us to abort ongoing HTTP requests (see #22265, which is now closed). However, as per a comment by @sgjesse on that issue:

The call to getUrl completes when the connection has been established. This can take time, so it should be possible to cancel that part. Currently there is no object to hook up any abort call to. A Future is returned, but futures cannot be canceled/aborted.

This is still true. Whilst we can now abort the request once it starts, we can't abort during the initial stage in which a connection is established. In my testing, I frequently encounter a situation where the device has a poor internet connection, and thus gets stuck at this point, with no way for the user to abort it.

I think there should be a better solution for aborting requests which does not only abort one part of the request timeline. A potential solution would be some sort of cancellation token which could be passed into the initial openUrl (or getUrl, postUrl, etc.) call, something also suggested by @sgjesse in that same comment:

One option could be to add an additional argument to getUrl (and friends) where the called can pass an object where getUrl can place a callback for aborting the HTTP transaction, e.g.:

var c = new HttpClientConnectionController();
new Timer(new Duration(seconds: 2), () {
  c.cancel();
});
client.getUrl(Uri.parse("http://www.example.com/"), controller: c)
   .then((HttpClientRequest request) {
   ...
   .catchError((e) => ..., test: (e) => e is CancledException);
@devoncarew devoncarew added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io type-enhancement A request for a change that isn't a bug labels Feb 6, 2023
@devoncarew
Copy link
Member

cc @brianquinlan for fyi

@mrlika
Copy link

mrlika commented Feb 14, 2023

I think it can be implemented in similar way as AbortController and AbortSignal in JavaScript.

Passing a controller directly is not a very "clean" approach. It should be a signal: client.getUrl(Uri.parse("http://www.example.com/"), signal: controller.signal)

@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. and removed area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants