diff --git a/packages/flutter_tools/lib/src/proxied_devices/devices.dart b/packages/flutter_tools/lib/src/proxied_devices/devices.dart index 9ead48d897..59dcf9977f 100644 --- a/packages/flutter_tools/lib/src/proxied_devices/devices.dart +++ b/packages/flutter_tools/lib/src/proxied_devices/devices.dart @@ -519,21 +519,31 @@ class ProxiedPortForwarder extends DevicePortForwarder { // Do nothing here. })); socket.listen((Uint8List data) { - connection.sendRequest('proxy.write', { + unawaited(connection.sendRequest('proxy.write', { 'id': id, - }, data); + }, data).catchError((Object error, StackTrace stackTrace) { + // Log the error, but proceed normally. Network failure should not + // crash the tool. If this is critical, the place where the connection + // is being used would crash. + _logger.printWarning('Write to remote proxy error: $error'); + _logger.printTrace('Write to remote proxy error: $error, stack trace: $stackTrace'); + })); }); _connectedSockets.add(socket); - unawaited(socket.done.then((dynamic value) { - connection.sendRequest('proxy.disconnect', { + unawaited(socket.done.catchError((Object error, StackTrace stackTrace) { + // Do nothing here. Everything will be handled in the `then` block below. + return false; + }).whenComplete(() { + // Send a proxy disconnect event just in case. + unawaited(connection.sendRequest('proxy.disconnect', { 'id': id, - }); - _connectedSockets.remove(socket); - }).onError((Object? error, StackTrace stackTrace) { - connection.sendRequest('proxy.disconnect', { - 'id': id, - }); + }).catchError((Object error, StackTrace stackTrace) { + // Ignore the error here. There might be a race condition when the + // remote end also disconnects. In any case, this request is just to + // notify the remote end to disconnect and we should not crash when + // there is an error here. + })); _connectedSockets.remove(socket); })); }, onError: (Object error, StackTrace stackTrace) { diff --git a/packages/flutter_tools/test/general.shard/proxied_devices/proxied_devices_test.dart b/packages/flutter_tools/test/general.shard/proxied_devices/proxied_devices_test.dart index 41e77b65bb..25c4adb082 100644 --- a/packages/flutter_tools/test/general.shard/proxied_devices/proxied_devices_test.dart +++ b/packages/flutter_tools/test/general.shard/proxied_devices/proxied_devices_test.dart @@ -137,6 +137,71 @@ void main() { 'hostPort': 350, }); }); + + group('socket done', () { + late Stream broadcastOutput; + late FakeSocket fakeSocket; + const String id = 'random_id'; + + setUp(() async { + final FakeServerSocket fakeServerSocket = FakeServerSocket(400); + final ProxiedPortForwarder portForwarder = ProxiedPortForwarder( + clientDaemonConnection, + deviceId: 'device_id', + logger: bufferLogger, + createSocketServer: (Logger logger, int? hostPort) async => + fakeServerSocket, + ); + + broadcastOutput = serverDaemonConnection.incomingCommands.asBroadcastStream(); + + unawaited(portForwarder.forward(300)); + + // Consumes the message. + DaemonMessage message = await broadcastOutput.first; + serverDaemonConnection.sendResponse(message.data['id']!, {'hostPort': 350}); + + fakeSocket = FakeSocket(); + fakeServerSocket.controller.add(fakeSocket); + // Consumes the message. + message = await broadcastOutput.first; + + serverDaemonConnection.sendResponse(message.data['id']!, id); + // Pump the event queue so that the socket future error handler has a + // chance to be listened to. + await pumpEventQueue(); + }); + + testWithoutContext('without error, should calls proxy.disconnect', () async { + // It will try to disconnect the remote port when socket is done. + fakeSocket.doneCompleter.complete(true); + final DaemonMessage message = await broadcastOutput.first; + + expect(message.data['id'], isNotNull); + expect(message.data['method'], 'proxy.disconnect'); + expect(message.data['params'], { + 'id': 'random_id', + }); + }); + + testWithoutContext('with error, should also calls proxy.disconnect', () async { + + fakeSocket.doneCompleter.complete(true); + final DaemonMessage message = await broadcastOutput.first; + + expect(message.data['id'], isNotNull); + expect(message.data['method'], 'proxy.disconnect'); + expect(message.data['params'], { + 'id': 'random_id', + }); + + // Send an error response and make sure that it won't crash the client. + serverDaemonConnection.sendErrorResponse(message.data['id']!, 'some error', StackTrace.current); + + // Wait the event queue and make sure that it doesn't crash. + await pumpEventQueue(); + }); + }); }); }