From 5f99783deb3730182d9e22e907ba2ee4f56bba43 Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Wed, 1 Jun 2022 10:08:13 -0700 Subject: [PATCH] [flutter_tools] flutter daemon handles a closed stdout IOSink (#105075) --- packages/flutter_tools/lib/src/daemon.dart | 4 ++++ .../test/general.shard/daemon_test.dart | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/flutter_tools/lib/src/daemon.dart b/packages/flutter_tools/lib/src/daemon.dart index 1102c2b7270d1..39c621a412a62 100644 --- a/packages/flutter_tools/lib/src/daemon.dart +++ b/packages/flutter_tools/lib/src/daemon.dart @@ -219,6 +219,10 @@ class DaemonStreams { if (binary != null) { _outputSink.add(binary); } + } on StateError catch (error) { + _logger.printError('Failed to write daemon command response: $error'); + // Failed to send, close the connection + _outputSink.close(); } on IOException catch (error) { _logger.printError('Failed to write daemon command response: $error'); // Failed to send, close the connection diff --git a/packages/flutter_tools/test/general.shard/daemon_test.dart b/packages/flutter_tools/test/general.shard/daemon_test.dart index b70633525bf3e..300c55485b09f 100644 --- a/packages/flutter_tools/test/general.shard/daemon_test.dart +++ b/packages/flutter_tools/test/general.shard/daemon_test.dart @@ -364,6 +364,20 @@ void main() { await daemonStreams.dispose(); expect(outputStream.isClosed, true); }); + + testWithoutContext('handles sending to a closed sink', () async { + // Unless the stream is listened to, the call to .close() will never + // complete + outputStream.stream.listen((List _) {}); + await outputStream.sink.close(); + daemonStreams.send(testCommand); + expect( + bufferLogger.errorText, + contains( + 'Failed to write daemon command response: Bad state: Cannot add event after closing', + ), + ); + }); }); }