Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Android] Fix BasicMessageChannel.resizeChannelBuffer #41982

Merged

Conversation

bleroux
Copy link
Contributor

@bleroux bleroux commented May 12, 2023

Description

This PR updates BasicMessageChannel.resizeChannelBuffer implementation. Previous implementation builds a wrong ByteBuffer and was not tested so it is difficult to know when it stopped working.

Related Issue

Fixes flutter/flutter#126632

Tests

Adds 1 test.

@gmackall
Copy link
Member

Note from android triage: @gaaclarke Do you know who might be a good reviewer for this?

@chinmaygarde
Copy link
Member

I don't think this is a fix. We are just swallowing an exception.

Comment on lines 34 to 42
ByteBuffer packet = null;
try {
final String content = String.format("resize\r%s\r%d", "flutter/test", 3);
final byte[] bytes = content.getBytes("UTF-8");
packet = ByteBuffer.allocateDirect(bytes.length);
packet.put(bytes);
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 only");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this code in the test? It's not actually contributing to the test.

Copy link
Contributor Author

@bleroux bleroux May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The packet variable is used below, line 47.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I see now.

Comment on lines 153 to 155
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Failed to resize channel buffer named " + channel, e);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be unrelated to the bug filed upstream and intended to support channel names that aren't valid as UTF-8 strings or something. Can it just be removed or perhaps serpated out into its own PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The catch is necessary because String.getBytes(charset) is declared as throws UnsupportedEncodingException

(In practice the exception will not be thrown because UTF-8 is guaranteed to be supported)

Alternatively, this could continue using Charset.forName (which does not throw any checked exceptions)

@jason-simmons
Copy link
Member

I don't think this is a fix. We are just swallowing an exception.

The fix here is the use of ByteBuffer.allocateDirect instead of ByteBuffer.wrap.

(The current Android Java/JNI platform message implementation assumes that all buffers passed to native are direct buffers)

@bleroux
Copy link
Contributor Author

bleroux commented May 18, 2023

I don't think this is a fix. We are just swallowing an exception.

@chinmaygarde The fix is not to swallow an exception, it is to replace the way the ByteBuffer is build.
Previously it relied on ByteBuffer.wrap and throws a RangeError, see flutter/flutter#126632 (comment).

The fix is to rely on ByteBuffer.allocateDirect.

Edited: Oops I just read Jason comment after posting mine 😅

@bleroux bleroux force-pushed the android_fix_resize_channel_buffer branch from 4639ff9 to 688b0f4 Compare May 18, 2023 21:50
@bleroux
Copy link
Contributor Author

bleroux commented May 18, 2023

I have updated the PR to use Charset.forName as suggested by Jason and it makes the PR simpler and less confusing. Thanks @jason-simmons!

@bleroux bleroux requested review from dnfield and jason-simmons May 19, 2023 00:10
@dnfield dnfield added the autosubmit Merge PR when tree becomes green via auto submit App label May 19, 2023
@auto-submit auto-submit bot merged commit 6bc60c8 into flutter:main May 19, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 19, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request May 19, 2023
…127162)

flutter/engine@bca11a4...6bc60c8

2023-05-19 [email protected] [Android] Fix BasicMessageChannel.resizeChannelBuffer (flutter/engine#41982)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
@bleroux bleroux deleted the android_fix_resize_channel_buffer branch May 19, 2023 14:08
CaseyHillers pushed a commit to CaseyHillers/flutter that referenced this pull request May 24, 2023
…lutter#127162)

flutter/engine@bca11a4...6bc60c8

2023-05-19 [email protected] [Android] Fix BasicMessageChannel.resizeChannelBuffer (flutter/engine#41982)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
@Hixie
Copy link
Contributor

Hixie commented Jul 31, 2023

FWIW, the format used for the message in this PR is deprecated, ideally we would use the binary format instead. It should be slightly more efficient.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App platform-android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error with 'resizeChannelBuffer' call in MethodChannel on Android platform
6 participants