Skip to content
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

Convert a java.lang.OutOfMemoryError into a ClientException #1299

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkgs/cronet_http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.3.3-wip

* Throw `ClientException` if `CronetClient.send` runs out of Java heap while
allocating memory for the request body.

## 1.3.2

* Upgrade `package:jni` to 0.10.1 and `package:jnigen` to 0.10.0 to fix method
Expand Down
16 changes: 15 additions & 1 deletion pkgs/cronet_http/lib/src/cronet_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,22 @@ class CronetClient extends BaseClient {
headers.forEach((k, v) => builder.addHeader(k.toJString(), v.toJString()));

if (body.isNotEmpty) {
final JByteBuffer data;
try {
data = body.toJByteBuffer();
} on JniException catch (e) {
// There are no unit tests for this code. You can verify this behavior
// manually by incrementally increasing the amount of body data in
// `CronetClient.post` until you get this exception.
if (e.message.contains('java.lang.OutOfMemoryError:')) {
throw ClientException(
'Not enough memory for request body: ${e.message}', request.url);
}
rethrow;
}

builder.setUploadDataProvider(
jb.UploadDataProviders.create2(body.toJByteBuffer()), _executor);
jb.UploadDataProviders.create2(data), _executor);
}
builder.build().start();
return responseCompleter.future;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/cronet_http/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: cronet_http
version: 1.3.2
version: 1.3.3-wip
description: >-
An Android Flutter plugin that provides access to the Cronet HTTP client.
repository: https://github.com/dart-lang/http/tree/master/pkgs/cronet_http
Expand Down
Loading