Skip to content

Commit 819b5f6

Browse files
committed
[flutter_tools] Shut down isolates in --experimental-faster-testing when suite channels close
When running multiple test files with `--experimental-faster-testing`, lightweight engine isolates remained alive because `IsolateChannel` ports lack an EOF disconnect signal on the child isolate. This caused isolates and native threads to accumulate across suites. Notify the child test isolate when the suite runner closes the channel sink, and invoke native `ForceShutdownIsolate` followed by `Isolate.exit()` once the suite channel completes. Fixes flutter#146603
1 parent 66b3ad9 commit 819b5f6

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ import 'dart:ui';
352352
import 'package:ffi/ffi.dart';
353353
import 'package:flutter_test/flutter_test.dart';
354354
import 'package:stream_channel/isolate_channel.dart';
355+
import 'package:stream_channel/stream_channel.dart';
355356
import 'package:test_api/backend.dart'; // flutter_ignore: test_api_import
356357
''');
357358

@@ -406,6 +407,9 @@ import 'package:test_api/backend.dart'; // flutter_ignore: test_api_import
406407
buffer.writeln();
407408

408409
buffer.writeln(r'''
410+
@Native<Void Function()>(symbol: 'ForceShutdownIsolate')
411+
external void _forceShutdownIsolate();
412+
409413
@Native<Void Function(Pointer<Utf8>, Pointer<Utf8>)>(symbol: 'Spawn')
410414
external void _spawn(Pointer<Utf8> entrypoint, Pointer<Utf8> route);
411415
@@ -427,7 +431,12 @@ void createChannelAndConnect(String path, String name, Function testMain) {
427431
final IsolateChannel<dynamic> channel = IsolateChannel<dynamic>.connectSend(
428432
IsolateNameServer.lookupPortByName(name)!,
429433
);
430-
channel.pipe(RemoteListener.start(() => testMain));
434+
final StreamChannel<Object?> suiteChannel = RemoteListener.start(() => testMain);
435+
channel.pipe(suiteChannel);
436+
suiteChannel.sink.done.whenComplete(() {
437+
_forceShutdownIsolate();
438+
Isolate.exit();
439+
});
431440
}
432441
433442
@pragma('vm:entry-point')
@@ -610,10 +619,20 @@ class SpawnPlugin extends PlatformPlugin {
610619
final String correctedPath = ${platform.isWindows ? r'"/$path"' : 'path'};
611620
await launchIsolate(correctedPath);
612621
613-
final StreamChannel<dynamic> channel = _channels[pathToImport(correctedPath)]!;
614-
final RunnerSuiteController controller = deserializeSuite(correctedPath, platform,
622+
final StreamChannel<dynamic> rawChannel = _channels[pathToImport(correctedPath)]!;
623+
final StreamController<dynamic> controller = StreamController<dynamic>(sync: true);
624+
controller.stream.listen(
625+
rawChannel.sink.add,
626+
onError: rawChannel.sink.addError,
627+
onDone: () {
628+
rawChannel.sink.add(<Object?>[0, <String, Object?>{'type': 'close'}]);
629+
rawChannel.sink.close();
630+
},
631+
);
632+
final StreamChannel<dynamic> channel = StreamChannel<dynamic>(rawChannel.stream, controller.sink);
633+
final RunnerSuiteController suiteController = deserializeSuite(correctedPath, platform,
615634
suiteConfig, const PluginEnvironment(), channel, message);
616-
return controller.suite;
635+
return suiteController.suite;
617636
}
618637
}
619638
''');

‎packages/flutter_tools/test/commands.shard/hermetic/test_test.dart‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,55 @@ const List<String> packageTestArgs = <String>[
877877
},
878878
);
879879

880+
testUsingContext(
881+
'Configures isolate teardown in generated spawners when --experimental-faster-testing is set',
882+
() async {
883+
final testCommand = TestCommand(toolContext: toolContext);
884+
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);
885+
886+
var caughtToolExit = false;
887+
await asyncGuard<void>(
888+
() => commandRunner.run(const <String>[
889+
'test',
890+
'--no-pub',
891+
'--experimental-faster-testing',
892+
'--',
893+
'test/fake_test.dart',
894+
'test/fake_test_2.dart',
895+
]),
896+
onError: (Object error) async {
897+
expect(error, isA<ToolExit>());
898+
expect((error as ToolExit).message, contains('The Dart compiler exited unexpectedly.'));
899+
caughtToolExit = true;
900+
901+
final Directory spawnerDir = fs.directory(
902+
fs.path.join('build', 'isolate_spawning_tester'),
903+
);
904+
final File childSourceFile = spawnerDir.childFile('child_test_isolate_spawner.dart');
905+
final File rootSourceFile = spawnerDir.childFile('root_test_isolate_spawner.dart');
906+
907+
expect(childSourceFile.existsSync(), true);
908+
final String childSource = childSourceFile.readAsStringSync();
909+
expect(childSource, contains('external void _forceShutdownIsolate();'));
910+
expect(childSource, contains('_forceShutdownIsolate();'));
911+
expect(childSource, contains('Isolate.exit();'));
912+
expect(childSource, contains('suiteChannel.sink.done.whenComplete'));
913+
914+
expect(rootSourceFile.existsSync(), true);
915+
final String rootSource = rootSourceFile.readAsStringSync();
916+
expect(rootSource, contains("<String, Object?>{'type': 'close'}"));
917+
},
918+
);
919+
expect(caughtToolExit, true);
920+
},
921+
overrides: <Type, Generator>{
922+
AnsiTerminal: () => _FakeTerminal(),
923+
FileSystem: () => fs,
924+
ProcessManager: () => FakeProcessManager.any(),
925+
DeviceManager: () => _FakeDeviceManager(<Device>[]),
926+
},
927+
);
928+
880929
testUsingContext(
881930
'Verbose prints phase timings',
882931
() async {

0 commit comments

Comments
 (0)