Skip to content

Commit

Permalink
Convert a java.lang.OutOfMemoryError into a ClientException (#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan authored Aug 28, 2024
1 parent a41141d commit dfeecf0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
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

0 comments on commit dfeecf0

Please sign in to comment.