-
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
[HttpClient] Request stream seems still being occupied even if the HttpClientRequest
has been aborted
#54392
Comments
20231218_120453.mp4 |
Hmmm...the likely cause is that the stream is not being closed if an exception occurs. |
The same issue appears to us, making it impossible to launch or even debug the app written in Flutter. Surprised that there aren't more reports - perhaps Flutter has not really gotten on serious use yet? |
You can try destroying the
Arguing with frameworks is not interesting, we should focus on the problem. |
Greetings! I've facing the same problem when solving issue in localsend/localsend and I found a few solution for this. The simplest solution is using const fileName = "some/file/path";
final file = File(fileName);
final fileStream = file.openRead().asBroadcastStream();
final client = HttpClient();
try {
final request = await client.postUrl(
Uri.parse('https://httpbun.com/post'),
);
final future = request.addStream(fileStream);
await future.catchError((e, s) {
request.abort(e, s);
debugPrint("Aborted");
return future;
});
} catch (_) {
await file.delete(); // Still throws `PathAccessException` because the file stream has not yet canceled
// Using the `retry` package may solve the problem, but it's not the best approach in this case
} but the downside is that you have to wait until the broadcast stream cancels itself, so here's a more complex but manageable way to do it, const fileName = "some/file/path";
final file = File(fileName);
final fileStream = file.openRead();
final fileReadController = StreamController<List<int>>();
final fileReadSubscription = fileStream.listen(
fileReadController.add,
onError: fileReadController.addError,
onDone: () => fileReadController.close()
);
final client = HttpClient();
try {
final request = await client.postUrl(
Uri.parse('https://httpbun.com/post'),
);
final future = request.addStream(fileReadController.stream);
await future.catchError((e, s) {
request.abort(e, s);
debugPrint("Aborted");
return future;
});
} catch (_) {
await fileReadSubscription.cancel(); // Cancel manually to delete files right away
await file.delete();
} which is basically just extracting the implementation of the That's my solution, please let me know if there are any problems or there is a better solution. By the way, I think it's good to mention this in the API docs, as it seems that a lot of people are experiencing this problem. |
This tracker is for issues related to:
dart:io
.The following issue can be easily reproduced on Windows.
Dart SDK version: 3.2.3 (stable) (Tue Dec 5 17:58:33 2023 +0000) on "windows_x64"
Reproducible code:
When the debugger stops at the breakpoint, disconnect the network and continue. The exception will thrown:
The text was updated successfully, but these errors were encountered: