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

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
@bleroux bleroux deleted the android_fix_resize_channel_buffer branch May 19, 2023 14:08
@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