From e96c47f9535435d5585ec7727eae5537b8811999 Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Mon, 7 Oct 2024 11:48:22 -0700 Subject: [PATCH] handle any `RPCError` due to vm service disconnection in `flutter run` (#156346) Follow-up to https://github.com/flutter/flutter/pull/153714. While reading through run.dart, I noticed we missed a case. --- .../flutter_tools/lib/src/commands/run.dart | 3 ++- .../commands.shard/hermetic/run_test.dart | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index cc718b4515..57a8643b2f 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -856,7 +856,8 @@ class RunCommand extends RunCommandBase { throwToolExit(null, exitCode: result); } } on RPCError catch (error) { - if (error.code == RPCErrorCodes.kServiceDisappeared) { + if (error.code == RPCErrorCodes.kServiceDisappeared || + error.message.contains('Service connection disposed')) { throwToolExit('Lost connection to device.'); } rethrow; diff --git a/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart index f589ba4fd0..6e74270b3d 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart @@ -1096,9 +1096,13 @@ void main() { }); }); - testUsingContext('Flutter run catches service has disappear errors and throws a tool exit', () async { + testUsingContext('Flutter run catches catches errors due to vm service disconnection and throws a tool exit', () async { final FakeResidentRunner residentRunner = FakeResidentRunner(); - residentRunner.rpcError = RPCError('flutter._listViews', RPCErrorCodes.kServiceDisappeared, ''); + residentRunner.rpcError = RPCError( + 'flutter._listViews', + RPCErrorCodes.kServiceDisappeared, + '', + ); final TestRunCommandWithFakeResidentRunner command = TestRunCommandWithFakeResidentRunner(); command.fakeResidentRunner = residentRunner; @@ -1106,6 +1110,17 @@ void main() { 'run', '--no-pub', ]), contains('Lost connection to device.')); + + residentRunner.rpcError = RPCError( + 'flutter._listViews', + RPCErrorCodes.kServerError, + 'Service connection disposed.', + ); + + await expectToolExitLater(createTestCommandRunner(command).run([ + 'run', + '--no-pub', + ]), contains('Lost connection to device.')); }, overrides: { Cache: () => Cache.test(processManager: FakeProcessManager.any()), FileSystem: () => MemoryFileSystem.test(),