Skip to content

[Breaking Change Request] Support abort() in HttpClientRequest in Dart:IO #41904

@zichangg

Description

@zichangg

Summary

With current APIs in Dart:IO, there is no way to abort the HttpClientRequest. Once HttpClientRequest has been sent, the request cannot be canceled until HttpClientResponse comes back. See the example below:

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

Abort() will allow users to stop waiting for HttpClientResponse, so users can abort the request if it takes too long.

Function signature

  abort([Object? exception, StackTrace? stackTrace]);

The intended behavior

  • The future returned by close() will complete with exception and stackTrace if abort() is called prior to HttpClientResponse being available. The default value for exception and stackTrace will be HttpException and StackTrace.empty.
  • Data cannot be sent through IOSink's API after abort().
  • In case of HttpClientResponse returning before abort, abort() will not make any effect.

Expected use case

HttpClient client = new HttpClient();
client.getUrl(Uri.parse("http://www.example.com/"))
   .then((HttpClientRequest request) {
     // Optionally set up headers...
     request.write();
     // Optionally write to the request object...
     // Then call close.
     Timer(Duration(seconds: 1), () {
       request.abort(Exception("Customized value"));
     }); 
     return request.close();
   })
   .then((HttpClientResponse response) {
     // Process the response.
     ...
   }, onError: (e) {
      // e will be `Exception("Customized value")`
   });

Expected impact

All classes extends or implements HttpClientRequest in Dart:IO will need to update accordingly.

Proposed Implementation

https://dart-review.googlesource.com/c/sdk/+/147339

Relative discussions

#22265

Metadata

Metadata

Assignees

Labels

area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.breaking-change-requestThis tracks requests for feedback on breaking changeslibrary-io

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions