Terminate non-detached test devices on flutter run completion (#159170)

See https://github.com/flutter/flutter/issues/159154.
See https://github.com/flutter/flutter/pull/159169.

Before this PR, it appeared we were accidentally leaking (keeping
active) `flutter_tester` instances (or any test device) after `flutter
run` completion, even if the runner was not explicitly detached. I
_think_ this is a bug, but I'll check with the tools team and possibly
@jonahwilliams before finalizing this.

/cc @jason-simmons

---------

Co-authored-by: Andrew Kolos <andrewrkolos@gmail.com>
This commit is contained in:
Matan Lurey
2024-11-20 17:21:25 -08:00
committed by GitHub
parent 1d24fa8f60
commit 5ead4e15a7
6 changed files with 179 additions and 13 deletions

View File

@@ -1118,6 +1118,16 @@ abstract class ResidentRunner extends ResidentHandlers {
return 'main.dart${swap ? '.swap' : ''}.dill';
}
/// Whether the app being instrumented by the runner should be stopped during
/// cleanup.
///
/// A detached app can happen one of two ways:
/// - [run] is used, and then the created application is manually [detach]ed;
/// - [attach] is used to explicitly connect to an already running app.
@protected
@visibleForTesting
bool stopAppDuringCleanup = true;
bool get debuggingEnabled => debuggingOptions.debuggingEnabled;
@override
@@ -1254,7 +1264,10 @@ abstract class ResidentRunner extends ResidentHandlers {
}
@override
@mustCallSuper
Future<void> detach() async {
stopAppDuringCleanup = false;
// TODO(bkonyi): remove when ready to serve DevTools from DDS.
await residentDevtoolsHandler!.shutdown();
await stopEchoingDeviceLog();
@@ -1398,6 +1411,7 @@ abstract class ResidentRunner extends ResidentHandlers {
}
}
@protected
void appFinished() {
if (_finished.isCompleted) {
return;

View File

@@ -125,9 +125,6 @@ class HotRunner extends ResidentRunner {
/// reload process do not have this issue.
bool _swap = false;
/// Whether the resident runner has correctly attached to the running application.
bool _didAttach = false;
final Map<String, List<int>> benchmarkData = <String, List<int>>{};
String? _targetPlatform;
@@ -220,15 +217,29 @@ class HotRunner extends ResidentRunner {
throw Exception('Failed to compile $expression');
}
// Returns the exit code of the flutter tool process, like [run].
@override
@nonVirtual
Future<int> attach({
Completer<DebugConnectionInfo>? connectionInfoCompleter,
Completer<void>? appStartedCompleter,
bool allowExistingDdsInstance = false,
bool needsFullRestart = true,
}) async {
_didAttach = true;
stopAppDuringCleanup = false;
return _attach(
connectionInfoCompleter: connectionInfoCompleter,
appStartedCompleter: appStartedCompleter,
allowExistingDdsInstance: allowExistingDdsInstance,
needsFullRestart: needsFullRestart,
);
}
Future<int> _attach({
Completer<DebugConnectionInfo>? connectionInfoCompleter,
Completer<void>? appStartedCompleter,
bool allowExistingDdsInstance = false,
bool needsFullRestart = true,
}) async {
try {
await connectToServiceProtocol(
reloadSources: _reloadSourcesService,
@@ -464,7 +475,7 @@ class HotRunner extends ResidentRunner {
return 1;
}
return attach(
return _attach(
connectionInfoCompleter: connectionInfoCompleter,
appStartedCompleter: appStartedCompleter,
needsFullRestart: false,
@@ -1142,7 +1153,7 @@ class HotRunner extends ResidentRunner {
} else {
commandHelp.hWithoutDetails.print();
}
if (_didAttach) {
if (stopAppDuringCleanup) {
commandHelp.d.print();
}
commandHelp.c.print();
@@ -1239,11 +1250,10 @@ class HotRunner extends ResidentRunner {
Future<void> cleanupAfterSignal() async {
await stopEchoingDeviceLog();
await hotRunnerConfig!.runPreShutdownOperations();
if (_didAttach) {
appFinished();
} else {
await exitApp();
if (stopAppDuringCleanup) {
return exitApp();
}
appFinished();
}
@override