-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add abort (or similar) to HttpClientRequest #22265
Comments
Marked this as blocking #19831. |
Added C11 label. |
When creating a HTTP request in dart:io we have the following steps: HttpClient client = new HttpClient(); 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(); 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. |
Added this to the 1.10 milestone. |
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. |
@sgjesse is anyone actually working on this? If not, should we close it as not planned? |
At the moment we don't have a good solution to this. Using |
Wow, still no timeout support. We'll continue using our custom https://gist.github.com/davenotik/169cc3a00d428516b257 It uses a Completer to build and return a Future. We start a Timer that calls completeError() to throw an error if the Future still hasn't completed after a specified duration. |
Just wondering if there are any plans to address it, or should we forget about it and use workarounds? |
cc @sortie |
Any update on this? |
cc @zichangg |
I can see three infos regarding timeouts in this thread, how they are related?
Some example with the best solution so far would be very helpful. Is dart-lang/http#21 (comment) the correct one (using @zanderso's |
zichang@ should be able to look at this once he is back from vacation. |
Seems like CancellableOperation could be helpful here? A potential solution route? |
@zichangg, I am still waiting for explanation if #22265 (comment) |
@j0nscalet – sadly not. In many cases you want the network connection to terminate. The CancellableOperation won't do that – although it will give you back control flow |
@kevmoo Ah right, makes sense. Thanks. In case this is helpful to anyone... |
connectionTimeout is probably the answer. Here is an example. HttpClient client = new HttpClient();
// Set up a time limit.
client.connectionTimeout = Duration(microseconds: 1);
client.getUrl(Uri.parse("https://www.google.com/"))
.then((HttpClientRequest request) {
// Optionally set up headers...
// Optionally write to the request object...
// Then call close.
return request.close();
})
.then((HttpClientResponse response) {
// response may not come back, so detachSocket() is not reliable.
}); For other methods like Using For providing a timeout handler, perhaps a try {
// Http connections.
} catch (e) {
// Check exception type and provide handler.
} |
There is no good solution yet |
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. |
it's almost five years and still looking for a standard solution, :( |
The breaking change request for this cl: #41904 Bug: #22265 Change-Id: I36db64b4db307b78cd188a2f1701ec733f2e73db Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/147339 Commit-Queue: Zichang Guo <zichangguo@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Five years later! Never say never! |
This reverts commit 4b96f20. Reason for revert: Windows bots are broken. Because --socket-short-read is specified, the server doesn't receive full header at once. https://dart-ci.appspot.com/log/vm-kernel-win-debug-x64/dartk-win-debug-x64/8907/standalone_2/io/http_client_connect_test/3 Original change's description: > [dart:io] Add Abort() on HttpClientRequest > > The breaking change request for this cl: #41904 > > Bug: #22265 > Change-Id: I36db64b4db307b78cd188a2f1701ec733f2e73db > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/147339 > Commit-Queue: Zichang Guo <zichangguo@google.com> > Reviewed-by: Lasse R.H. Nielsen <lrn@google.com> TBR=lrn@google.com,zichangguo@google.com Change-Id: I48f7a2ee3bb75e0e0ba0bd24ed53fcac372e016d No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: #22265 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155548 Reviewed-by: Zichang Guo <zichangguo@google.com> Commit-Queue: Zichang Guo <zichangguo@google.com>
Is the plan to reland the above CL? |
Reland was ready! Because Lasse went on vacation. He will be back next week and I'll get it land next week. |
Thanks @zichangg - marking this as part of Sept release for now. |
The test was poorly written. The response from Socket can arrive separately. So the check for content-length header will fail. This is a reland of 4b96f20 Original change's description: > [dart:io] Add Abort() on HttpClientRequest > > The breaking change request for this cl: #41904 > > Bug: #22265 > Change-Id: I36db64b4db307b78cd188a2f1701ec733f2e73db > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/147339 > Commit-Queue: Zichang Guo <zichangguo@google.com> > Reviewed-by: Lasse R.H. Nielsen <lrn@google.com> Bug: #22265 Change-Id: Ibfe9565a3f9d5ef84274fba33a68fb57dbbe28c9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155581 Reviewed-by: Siva Annamalai <asiva@google.com> Commit-Queue: Zichang Guo <zichangguo@google.com>
@zichangg looks like this was relanded? |
Yes. It has fixed the broken test and is ready to go. |
Related issue for exposing this in |
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
The text was updated successfully, but these errors were encountered: