Skip to content

Add abort (or similar) to HttpClientRequest #22265

Closed

Description

@kevmoo

HttpRequest (in dart:html) has an abort() method

There is a request to add a feature to the http package to support timeout.

This can be done with dart:html, but not dart:io

Activity

  1. kevmoo commented on Feb 4, 2015

    @kevmoo
    MemberAuthor

    Marked this as blocking #19831.

  2. kevmoo commented on Feb 5, 2015

    @kevmoo
    MemberAuthor

    Added C11 label.

  3. sgjesse commented on Feb 6, 2015

    @sgjesse
    Contributor

    When creating a HTTP request in dart:io we have the following steps:

    HttpClient client = new HttpClient();
    client.getUrl(Uri.parse("http://www.example.com/"))
       .then((HttpClientRequest request) {
         // Optionally set up headers...
         // Optionally write to the request object...
         // Then call close.
         ...
         return request.close();
       })
       .then((HttpClientResponse response) {
         // Process the response.
         ...
       });

    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.

    Then the future completes with a HttpClientRequest. That is closed to send the request and wait for the response. This can take time. In this step there is an object (the HttpClientRequest) on which you can actually call something (e.g. abort). However is seems wrong to call abort after calling close.

    Finally the HttpClientResponse comes along and the response can be processed. Here there is also an object where an abort method can be added.

    Having to add abort to both HttpClientRequest and HttpClientResponse seems wrong.

    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);

    Straight-line code here ensures that the cancel member of HttpClientConnectionController has been set by getUrl before the timer callback can be called.

    This is also related to issue #19120.


    cc @lrhn.

  4. kasperl commented on Mar 20, 2015

    @kasperl

    Added this to the 1.10 milestone.

  5. DartBot commented on Apr 7, 2015

    @DartBot

    This comment was originally written by @kaendfinger


    In all honesty, it would be nice to have some sort of Cancelable Future. In fact, in theory, it would be easy to add cancellation to futures, since it's built upon the Timer, you could either do an if statement to check if it was canceled, but it's unclear how this would propagate to the other futures.

  6. lrhn commented on Apr 7, 2015

    @lrhn
    Member

    There are definitely cases where it would be nice to have some sort of channel to tell an asynchronous computation that its result is no longer needed.

    I don't think adding a function to Future is the best way to do that. Futures can be safely shared with other code. If the future could also be used to cancel the computation for everybody, then sharing it would no longer be safe.
    I'd rather have something like a CancelableComputation class with a future and a cancel method (and a shorter name) - since we can't just return a tuple of a future and cancel function.

  7. sgjesse commented on Apr 20, 2015

    @sgjesse
    Contributor

    Removed this from the 1.10 milestone.
    Added this to the 1.11 milestone.

  8. added
    P1A high priority bug; for example, a single project is unusable or has many test failures
    on Apr 20, 2015
  9. added this to the 1.11 milestone on Apr 20, 2015
  10. added
    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
    on Jun 16, 2015
  11. added this to the 1.12 milestone on Jun 17, 2015
  12. 37 remaining items

  13. januwA commented on Apr 3, 2020

    @januwA

    There is no good solution yet

  14. liranzairi commented on Apr 9, 2020

    @liranzairi

    I'm also looking for a way to do this. I have a project where I need to upload images to the server, which of course can take time depending on the size of the image. I want provide the user a way to cancel the operation in case it's taking too long without the upload request continuing in the background. Can anybody help with a workaround? Thanks.

  15. zahidaz commented on Apr 19, 2020

    @zahidaz

    it's almost five years and still looking for a standard solution, :(

  16. kevmoo commented on Jul 22, 2020

    @kevmoo
    MemberAuthor

    Five years later! Never say never!

    4b96f20

  17. vsmenon commented on Aug 5, 2020

    @vsmenon
    Member

    Is the plan to reland the above CL?

  18. zichangg commented on Aug 7, 2020

    @zichangg
    Contributor

    Reland was ready! Because Lasse went on vacation. He will be back next week and I'll get it land next week.

  19. vsmenon commented on Aug 10, 2020

    @vsmenon
    Member

    Thanks @zichangg - marking this as part of Sept release for now.

  20. mit-mit commented on Aug 14, 2020

    @mit-mit
    Member

    @zichangg looks like this was relanded?

  21. zichangg commented on Aug 14, 2020

    @zichangg
    Contributor

    Yes. It has fixed the broken test and is ready to go.

  22. mit-mit commented on Sep 24, 2020

    @mit-mit
    Member

    Related issue for exposing this in package:http: dart-lang/http#424

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1A high priority bug; for example, a single project is unusable or has many test failuresarea-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-iotype-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions