Skip to content

Commit ed37ea9

Browse files
authored
[flutter_tools] Safely handle non-JSON messages in test stream parsers (flutter#192089)
When communicating with remote test runners or browsers over test streams or WebSockets, unhandled non-JSON strings (such as dynamic library loading errors or missing snapshot warnings) caused `_ChunkedJsonParser` to throw an unhandled `FormatException` at character 1, crashing the tool. This changes `pipeHarnessToRemote` in `flutter_platform.dart` and `BrowserManager` in `flutter_web_platform.dart` to intercept non-JSON payloads, log warnings and traces, and ignore invalid frames rather than crashing the tool process. Fixes flutter#191898 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [ ] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 74cef67 commit ed37ea9

3 files changed

Lines changed: 76 additions & 17 deletions

File tree

‎packages/flutter_tools/lib/src/test/flutter_platform.dart‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ class FlutterPlatform extends PlatformPlugin {
721721
'test $ourTestCount: connected to test device, now awaiting test result',
722722
);
723723

724-
await _pipeHarnessToRemote(
724+
await pipeHarnessToRemote(
725725
id: ourTestCount,
726726
harnessChannel: testHarnessChannel,
727727
remoteChannel: remoteChannel,
@@ -889,9 +889,10 @@ class _AsyncError {
889889
///
890890
/// The returned future completes when either side is closed, which also
891891
/// indicates when the tests have finished.
892-
Future<void> _pipeHarnessToRemote({
892+
@visibleForTesting
893+
Future<void> pipeHarnessToRemote({
893894
required int id,
894-
required StreamChannel<dynamic> harnessChannel,
895+
required StreamChannel<Object?> harnessChannel,
895896
required StreamChannel<String> remoteChannel,
896897
}) async {
897898
globals.printTrace('test $id: Waiting for test harness or tests to finish');
@@ -902,10 +903,18 @@ Future<void> _pipeHarnessToRemote({
902903
) {
903904
globals.printTrace('test $id: Test process is no longer needed by test harness');
904905
}),
905-
remoteChannel.stream.map<dynamic>(json.decode).pipe(harnessChannel.sink).then<void>((
906-
void value,
907-
) {
908-
globals.printTrace('test $id: Test harness is no longer needed by test process');
909-
}),
906+
remoteChannel.stream
907+
.map<Object?>(json.decode)
908+
.handleError((Object error) {
909+
final formatException = error as FormatException;
910+
globals.printWarning(
911+
'Received unexpected non-JSON output from test runner: ${formatException.source}',
912+
);
913+
globals.printTrace('test $id: JSON decoding failed: $formatException');
914+
}, test: (error) => error is FormatException)
915+
.pipe(harnessChannel.sink)
916+
.then<void>((void value) {
917+
globals.printTrace('test $id: Test harness is no longer needed by test process');
918+
}),
910919
]);
911920
}

‎packages/flutter_tools/lib/src/test/flutter_web_platform.dart‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -797,16 +797,24 @@ class BrowserManager {
797797
// the browser is still running code which means the user isn't debugging.
798798
_channel = MultiChannel<dynamic>(
799799
webSocket.cast<String>().transform(jsonDocument).changeStream((Stream<Object?> stream) {
800-
return stream.map((Object? message) {
801-
if (!_closed) {
802-
_timer.reset();
803-
}
804-
for (final RunnerSuiteController controller in _controllers) {
805-
controller.setDebugging(false);
806-
}
800+
return stream
801+
.handleError((Object error) {
802+
final formatException = error as FormatException;
803+
_logger.printWarning(
804+
'Received unexpected non-JSON message from browser WebSocket: ${formatException.source}',
805+
);
806+
_logger.printTrace('JSON decode error: $formatException');
807+
}, test: (error) => error is FormatException)
808+
.map((Object? message) {
809+
if (!_closed) {
810+
_timer.reset();
811+
}
812+
for (final RunnerSuiteController controller in _controllers) {
813+
controller.setDebugging(false);
814+
}
807815

808-
return message;
809-
});
816+
return message;
817+
});
810818
}),
811819
);
812820

‎packages/flutter_tools/test/general.shard/flutter_platform_test.dart‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,48 @@ void main() {
246246
expect(flutterPlatform.testAssetDirectory, '/build/test');
247247
expect(flutterPlatform.icudtlPath, equals('ghi'));
248248
});
249+
250+
testUsingContext('pipeHarnessToRemote safely ignores non-JSON string and logs warning', () async {
251+
final harnessController = StreamChannelController<Object?>();
252+
final remoteController = StreamChannelController<String>();
253+
254+
final Future<void> pipeFuture = pipeHarnessToRemote(
255+
id: 0,
256+
harnessChannel: harnessController.foreign,
257+
remoteChannel: remoteController.foreign,
258+
);
259+
260+
final receivedFromRemote = <Object?>[];
261+
harnessController.local.stream.listen(receivedFromRemote.add);
262+
263+
// Send non-JSON error string from remote channel followed by valid JSON.
264+
remoteController.local.sink.add(
265+
'Loading dynamic library failed: dlopen(/opt/homebrew/share/flutter/bin/cache/libflutter.dylib)',
266+
);
267+
remoteController.local.sink.add('{"valid": true}');
268+
269+
await pumpEventQueue();
270+
271+
await remoteController.local.sink.close();
272+
await harnessController.local.sink.close();
273+
274+
await pipeFuture;
275+
276+
expect(
277+
receivedFromRemote,
278+
equals(<Object?>[
279+
<String, Object?>{'valid': true},
280+
]),
281+
);
282+
final logger = globals.logger as BufferLogger;
283+
expect(
284+
logger.warningText,
285+
contains(
286+
'Received unexpected non-JSON output from test runner: Loading dynamic library failed: dlopen',
287+
),
288+
);
289+
expect(logger.traceText, contains('test 0: JSON decoding failed:'));
290+
});
249291
});
250292

251293
group('generateTestBootstrap', () {

0 commit comments

Comments
 (0)