@@ -59,8 +59,14 @@ void main(List<String> args) async {
5959 return ;
6060 }
6161
62+ // Capture CTRL-C.
63+ late final StreamSubscription <void > onSigint;
6264 runZonedGuarded (
6365 () async {
66+ onSigint = ProcessSignal .sigint.watch ().listen ((_) {
67+ onSigint.cancel ();
68+ panic (< String > ['Received SIGINT' ]);
69+ });
6470 await _run (
6571 verbose: options.verbose,
6672 outDir: Directory (options.outDir),
@@ -73,9 +79,11 @@ void main(List<String> args) async {
7379 contentsGolden: options.outputContentsGolden,
7480 ndkStack: options.ndkStack,
7581 );
82+ onSigint.cancel ();
7683 exit (0 );
7784 },
7885 (Object error, StackTrace stackTrace) {
86+ onSigint.cancel ();
7987 if (error is ! Panic ) {
8088 stderr.writeln ('Unhandled error: $error ' );
8189 stderr.writeln (stackTrace);
@@ -146,8 +154,9 @@ Future<void> _run({
146154 // for the screenshots.
147155 // On LUCI, the host uploads the screenshots to Skia Gold.
148156 SkiaGoldClient ? skiaGoldClient;
149- late ServerSocket server;
157+ late final ServerSocket server;
150158 final List <Future <void >> pendingComparisons = < Future <void >> [];
159+ final List <Socket > pendingConnections = < Socket > [];
151160 await step ('Starting server...' , () async {
152161 server = await ServerSocket .bind (InternetAddress .anyIPv4, _tcpPort);
153162 if (verbose) {
@@ -157,8 +166,8 @@ Future<void> _run({
157166 if (verbose) {
158167 stdout.writeln ('client connected ${client .remoteAddress .address }:${client .remotePort }' );
159168 }
160- client. transform ( const ScreenshotBlobTransformer ()). listen (
161- (Screenshot screenshot) {
169+ pendingConnections. add (client);
170+ client. transform ( const ScreenshotBlobTransformer ()). listen ( (Screenshot screenshot) {
162171 final String fileName = screenshot.filename;
163172 final Uint8List fileContent = screenshot.fileContent;
164173 if (verbose) {
@@ -182,9 +191,9 @@ Future<void> _run({
182191 });
183192 pendingComparisons.add (comparison);
184193 }
185- }, onError : (dynamic err ) {
186- panic ( < String > [ 'error while receiving bytes: $ err ' ] );
187- }, cancelOnError : true );
194+ }, onDone : () {
195+ pendingConnections. remove (client );
196+ });
188197 });
189198 });
190199
@@ -335,6 +344,16 @@ Future<void> _run({
335344 });
336345 } finally {
337346 await server.close ();
347+ for (final Socket client in pendingConnections.toList ()) {
348+ client.close ();
349+ }
350+
351+ await step ('Killing test app and test runner...' , () async {
352+ final int exitCode = await pm.runAndForward (< String > [adb.path, 'shell' , 'am' , 'force-stop' , 'dev.flutter.scenarios' ]);
353+ if (exitCode != 0 ) {
354+ panic (< String > ['could not kill test app' ]);
355+ }
356+ });
338357
339358 await step ('Killing logcat process...' , () async {
340359 final bool delivered = logcatProcess.kill (ProcessSignal .sigkill);
0 commit comments