You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add abort (or similar) to HttpClientRequest #22265
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
Reacted by Joel Trottier-Hebert, frank-rollpin, Damir, Matthew Butler, István Soós, qwilbird, Iqbal, sir-boformer, Jimmy Aumard, Ryan Heise and 13 more
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.:
Straight-line code here ensures that the cancel member of HttpClientConnectionController has been set by getUrl before the timer callback can be called.
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.
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.
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.
Activity
kevmoo commentedon Feb 4, 2015
Marked this as blocking #19831.
kevmoo commentedon Feb 5, 2015
Added C11 label.
sgjesse commentedon Feb 6, 2015
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.
kasperl commentedon Mar 20, 2015
Added this to the 1.10 milestone.
DartBot commentedon Apr 7, 2015
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.
lrhn commentedon Apr 7, 2015
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.
sgjesse commentedon Apr 20, 2015
Removed this from the 1.10 milestone.
Added this to the 1.11 milestone.
37 remaining items
januwA commentedon Apr 3, 2020
There is no good solution yet
liranzairi commentedon Apr 9, 2020
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.
zahidaz commentedon Apr 19, 2020
it's almost five years and still looking for a standard solution, :(
[dart:io] Add Abort() on HttpClientRequest
kevmoo commentedon Jul 22, 2020
Five years later! Never say never!
4b96f20
Revert "[dart:io] Add Abort() on HttpClientRequest"
vsmenon commentedon Aug 5, 2020
Is the plan to reland the above CL?
zichangg commentedon Aug 7, 2020
Reland was ready! Because Lasse went on vacation. He will be back next week and I'll get it land next week.
vsmenon commentedon Aug 10, 2020
Thanks @zichangg - marking this as part of Sept release for now.
Reland "[dart:io] Add Abort() on HttpClientRequest"
mit-mit commentedon Aug 14, 2020
@zichangg looks like this was relanded?
zichangg commentedon Aug 14, 2020
Yes. It has fixed the broken test and is ready to go.
mit-mit commentedon Sep 24, 2020
Related issue for exposing this in
package:http: dart-lang/http#424