From 09d8c55306755578d8c50c3441b1948f4667de66 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Wed, 28 Aug 2024 10:47:49 -0700 Subject: [PATCH 1/3] Convert a `java.lang.OutOfMemoryError` into a `ClientException` Fixes https://github.com/dart-lang/http/issues/1271 --- pkgs/cronet_http/lib/src/cronet_client.dart | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkgs/cronet_http/lib/src/cronet_client.dart b/pkgs/cronet_http/lib/src/cronet_client.dart index f55617eeb1..28ac921f84 100644 --- a/pkgs/cronet_http/lib/src/cronet_client.dart +++ b/pkgs/cronet_http/lib/src/cronet_client.dart @@ -392,8 +392,19 @@ 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) { + 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; From 48b2bcf1808622b99320baeb9067bf027808d8ba Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Wed, 28 Aug 2024 12:42:01 -0700 Subject: [PATCH 2/3] Update cronet_client.dart --- pkgs/cronet_http/lib/src/cronet_client.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkgs/cronet_http/lib/src/cronet_client.dart b/pkgs/cronet_http/lib/src/cronet_client.dart index 28ac921f84..440de8ae35 100644 --- a/pkgs/cronet_http/lib/src/cronet_client.dart +++ b/pkgs/cronet_http/lib/src/cronet_client.dart @@ -396,6 +396,9 @@ class CronetClient extends BaseClient { 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); From ff41d8ebb725b862d05e3470de7e7499f6b0aa1b Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Wed, 28 Aug 2024 13:25:33 -0700 Subject: [PATCH 3/3] changelog --- pkgs/cronet_http/CHANGELOG.md | 5 +++++ pkgs/cronet_http/pubspec.yaml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pkgs/cronet_http/CHANGELOG.md b/pkgs/cronet_http/CHANGELOG.md index 0ce43fceb3..14dcf79b65 100644 --- a/pkgs/cronet_http/CHANGELOG.md +++ b/pkgs/cronet_http/CHANGELOG.md @@ -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 diff --git a/pkgs/cronet_http/pubspec.yaml b/pkgs/cronet_http/pubspec.yaml index 19e5e8509f..ee14e309a9 100644 --- a/pkgs/cronet_http/pubspec.yaml +++ b/pkgs/cronet_http/pubspec.yaml @@ -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