Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hang that occurs when streaming calls are ongoing and a hot-restart occurs in Flutter web #718

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Remove generated status codes.
* Remove dependency on `package:archive`.
* Move `codec.dart`.
* Work around hang during Flutter hot restart by adding default case handler in _GrpcWebConversionSink.add.

## 3.2.4

Expand Down
13 changes: 13 additions & 0 deletions lib/src/client/transport/web_streams.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ class _GrpcWebConversionSink implements ChunkedConversionSink<ByteBuffer> {
void add(ByteBuffer chunk) {
_chunkOffset = 0;
final chunkData = chunk.asUint8List();
// in flutter web, when a hot-restart is requested, the code can get stuck
// in the following loop if there is an active streaming call. the
// switch statement is invoked but doesn't match any of the cases. this
// presumably has something to do how enums work across isolates.
// possibly related to https://github.com/dart-lang/sdk/issues/35626.
//
// in any case, adding a default handler to the switch statement
// causes this loop to end in that case, allowing the old isolate
// to shut down and the hot-restart to work properly.
processingLoop:
while (_chunkOffset < chunk.lengthInBytes) {
switch (_state) {
case _GrpcWebParseState.init:
Expand All @@ -143,6 +153,9 @@ class _GrpcWebConversionSink implements ChunkedConversionSink<ByteBuffer> {
case _GrpcWebParseState.message:
_parseMessage(chunkData);
break;
default:
// only expected to be hit when hot-restarting, see above
break processingLoop;
}
}
_chunkOffset = 0;
Expand Down