From dfeecf0a495925456ff5d0216a559dcc422bb4a1 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Wed, 28 Aug 2024 14:43:51 -0700 Subject: [PATCH] Convert a `java.lang.OutOfMemoryError` into a `ClientException` (#1299) --- pkgs/cronet_http/CHANGELOG.md | 5 +++++ pkgs/cronet_http/lib/src/cronet_client.dart | 16 +++++++++++++++- pkgs/cronet_http/pubspec.yaml | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) 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/lib/src/cronet_client.dart b/pkgs/cronet_http/lib/src/cronet_client.dart index f55617eeb1..440de8ae35 100644 --- a/pkgs/cronet_http/lib/src/cronet_client.dart +++ b/pkgs/cronet_http/lib/src/cronet_client.dart @@ -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; 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