Skip to content

Commit 3ce13df

Browse files
koji-1009okorohelijahvashworth
authored andcommitted
[flutter_tools] Wait for iOS simulator log stream before launch (flutter#193142)
Fixes flutter#181771 Fixes a race where `flutter test integration_test` on an iOS simulator could hang forever at "Waiting for VM Service port to be available...". `_IOSSimulatorLogReader` starts `xcrun simctl spawn … log stream` from `onListen`, and nothing waited for the process to start, so `startApp` could launch the app before `log stream` was ready. `log stream` drops events logged before it prints its first line (`Filtering the log data using ...`), so the Dart VM Service URL could be missed. The verbose log attached to flutter#181771 shows `simctl launch` running before `simctl spawn … log stream`. This PR makes `startApp` wait, right before `simctl launch`, until `log stream` prints its first line or exits. If that does not happen within 30 seconds, `startApp` prints a warning and keeps waiting. A log reader starts a new `log stream` each time it is listened to again, so the wait applies to each of them. The process is still started from `onListen`, so `flutter run`, which listens to the log reader before building the app, still starts `log stream` during the build. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Elijah Okoroh <[email protected]> Co-authored-by: Victoria Ashworth <[email protected]>
1 parent 677f8e2 commit 3ce13df

2 files changed

Lines changed: 232 additions & 5 deletions

File tree

‎packages/flutter_tools/lib/src/ios/simulators.dart‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ class IOSSimulator extends Device {
403403
@override
404404
Future<CpuArch> get cpuArch async => _cpuArch;
405405

406-
final _logReaders = <IOSApp?, DeviceLogReader>{};
406+
final _logReaders = <IOSApp?, _IOSSimulatorLogReader>{};
407407
_IOSSimulatorDevicePortForwarder? _portForwarder;
408408

409409
@override
@@ -496,13 +496,25 @@ class IOSSimulator extends Device {
496496

497497
ProtocolDiscovery? vmServiceDiscovery;
498498
if (debuggingOptions.debuggingEnabled) {
499+
final _IOSSimulatorLogReader logReader = getLogReader(app: package);
499500
vmServiceDiscovery = ProtocolDiscovery.vmService(
500-
getLogReader(app: package),
501+
logReader,
501502
ipv6: debuggingOptions.ipv6,
502503
hostPort: debuggingOptions.hostVmServicePort,
503504
devicePort: debuggingOptions.deviceVmServicePort,
504505
logger: globals.logger,
505506
);
507+
// Launch the app only after the log stream is ready; otherwise the
508+
// Dart VM Service URL it logs on startup can be missed.
509+
const warningDelay = Duration(seconds: 30);
510+
final timer = Timer(warningDelay, () {
511+
globals.printError(
512+
'The iOS simulator log stream did not start after ${warningDelay.inSeconds} seconds. '
513+
'This is taking much longer than expected...',
514+
);
515+
});
516+
await logReader._ready.future;
517+
timer.cancel();
506518
}
507519

508520
// Launch the updated application in the simulator.
@@ -630,7 +642,7 @@ class IOSSimulator extends Device {
630642
}
631643

632644
@override
633-
DeviceLogReader getLogReader({covariant IOSApp? app, bool includePastLogs = false}) {
645+
_IOSSimulatorLogReader getLogReader({covariant IOSApp? app, bool includePastLogs = false}) {
634646
assert(!includePastLogs, 'Past log reading not supported on iOS simulators.');
635647
return _logReaders.putIfAbsent(app, () => _IOSSimulatorLogReader(this, app));
636648
}
@@ -857,6 +869,14 @@ class _IOSSimulatorLogReader extends SharedIOSDeviceLogReader {
857869
onCancel: _stop,
858870
);
859871

872+
/// Completes once the log stream is ready to deliver events, or has exited.
873+
///
874+
/// `log stream` is ready once it prints its first line (a "Filtering the log
875+
/// data using ..." header).
876+
///
877+
/// Recreated on every listen, since each one starts a new `log stream`.
878+
var _ready = Completer<void>();
879+
860880
@override
861881
@visibleForTesting
862882
StreamController<String> get linesController => _linesController;
@@ -872,10 +892,18 @@ class _IOSSimulatorLogReader extends SharedIOSDeviceLogReader {
872892
String get name => device.name;
873893

874894
Future<void> _start() async {
895+
// Use a local so that a previous process exiting late cannot complete it.
896+
_ready = Completer<void>();
897+
final Completer<void> ready = _ready;
875898
// Unified logging iOS 11 and greater (introduced in iOS 10).
876899
if (await device.sdkMajorVersion >= 11) {
877900
_deviceProcess = await launchDeviceUnifiedLogging(device, _appName);
878-
_deviceProcess?.stdout.transform(utf8LineDecoder).listen(_onUnifiedLoggingLine);
901+
_deviceProcess?.stdout.transform(utf8LineDecoder).listen((String line) {
902+
if (!ready.isCompleted) {
903+
ready.complete();
904+
}
905+
_onUnifiedLoggingLine(line);
906+
});
879907
_deviceProcess?.stderr.transform(utf8LineDecoder).listen(_onUnifiedLoggingLine);
880908
} else {
881909
// Fall back to syslog parsing.
@@ -897,6 +925,9 @@ class _IOSSimulatorLogReader extends SharedIOSDeviceLogReader {
897925
// cleanup in the callback.
898926
unawaited(
899927
_deviceProcess?.exitCode.whenComplete(() {
928+
if (!ready.isCompleted) {
929+
ready.complete();
930+
}
900931
if (_linesController.hasListener) {
901932
_linesController.close();
902933
}

‎packages/flutter_tools/test/general.shard/ios/simulators_test.dart‎

Lines changed: 197 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66

7+
import 'package:fake_async/fake_async.dart';
78
import 'package:file/memory.dart';
89
import 'package:flutter_tools/src/base/common.dart';
910
import 'package:flutter_tools/src/base/file_system.dart';
@@ -14,6 +15,7 @@ import 'package:flutter_tools/src/base/platform.dart';
1415
import 'package:flutter_tools/src/base/process.dart';
1516
import 'package:flutter_tools/src/base/version.dart';
1617
import 'package:flutter_tools/src/build_info.dart';
18+
import 'package:flutter_tools/src/convert.dart';
1719
import 'package:flutter_tools/src/devfs.dart';
1820
import 'package:flutter_tools/src/device.dart';
1921
import 'package:flutter_tools/src/device_port_forwarder.dart';
@@ -29,7 +31,7 @@ import 'package:test/fake.dart';
2931
import '../../src/common.dart';
3032
import '../../src/context.dart';
3133
import '../../src/fake_process_manager.dart';
32-
import '../../src/fakes.dart';
34+
import '../../src/fakes.dart' show FakeLogger, FakePlistParser;
3335

3436
final Platform macosPlatform = FakePlatform(
3537
operatingSystem: 'macos',
@@ -1679,6 +1681,183 @@ Dec 20 17:04:32 md32-11-vm1 Another App[88374]: Ignore this text''',
16791681
Xcode: () => xcode,
16801682
},
16811683
);
1684+
1685+
group('waiting for the log stream', () {
1686+
final logStreamCommand = <Pattern>[
1687+
'xcrun',
1688+
'simctl',
1689+
'spawn',
1690+
'x',
1691+
'log',
1692+
'stream',
1693+
'--style',
1694+
'json',
1695+
'--predicate',
1696+
RegExp('.*'),
1697+
];
1698+
1699+
late FakeProcessManager processManager;
1700+
1701+
setUp(() {
1702+
processManager = FakeProcessManager.empty();
1703+
testPlistParser.setProperty('CFBundleIdentifier', 'correct');
1704+
});
1705+
1706+
IOSSimulator buildDevice() => IOSSimulator(
1707+
'x',
1708+
cpuArch: CpuArch.x64,
1709+
name: 'iPhone SE',
1710+
simulatorCategory: 'iOS 11.2',
1711+
simControl: simControl,
1712+
logger: logger,
1713+
);
1714+
1715+
IOSApp buildPackage() => PrebuiltIOSApp(
1716+
projectBundleId: 'correct',
1717+
bundleName: 'name',
1718+
uncompressedBundle: globals.fs.currentDirectory,
1719+
applicationPackage: globals.fs.currentDirectory,
1720+
);
1721+
1722+
testUsingContext(
1723+
'startApp waits for the log stream to be ready before each launch',
1724+
() {
1725+
// Regression test for https://github.com/flutter/flutter/issues/181771.
1726+
fakeAsync((FakeAsync async) {
1727+
final firstLogProcess = FakeStreamingProcess();
1728+
final secondLogProcess = FakeStreamingProcess();
1729+
processManager.addCommand(
1730+
FakeCommand(command: logStreamCommand, process: firstLogProcess),
1731+
);
1732+
processManager.addCommand(
1733+
FakeCommand(command: logStreamCommand, process: secondLogProcess),
1734+
);
1735+
final IOSSimulator device = buildDevice();
1736+
final IOSApp package = buildPackage();
1737+
1738+
device.startApp(
1739+
package,
1740+
prebuiltApplication: true,
1741+
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
1742+
);
1743+
async.elapse(const Duration(seconds: 1));
1744+
expect(simControl.requests, isEmpty);
1745+
1746+
firstLogProcess.write('Filtering the log data using "type == 1024"\n');
1747+
async.flushMicrotasks();
1748+
expect(simControl.requests, hasLength(1));
1749+
1750+
firstLogProcess.write(
1751+
'"eventMessage" : "The Dart VM service is listening on http://127.0.0.1:1234/abcdef/"\n',
1752+
);
1753+
async.elapse(const Duration(seconds: 1));
1754+
1755+
// The first launch stopped the log stream, so this one starts a new one.
1756+
device.startApp(
1757+
package,
1758+
prebuiltApplication: true,
1759+
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
1760+
);
1761+
async.elapse(const Duration(seconds: 1));
1762+
expect(simControl.requests, hasLength(1));
1763+
1764+
secondLogProcess.write('Filtering the log data using "type == 1024"\n');
1765+
async.flushMicrotasks();
1766+
expect(simControl.requests, hasLength(2));
1767+
1768+
async.elapse(const Duration(seconds: 30));
1769+
expect(logger.errorText, isEmpty);
1770+
expect(processManager, hasNoRemainingExpectations);
1771+
});
1772+
},
1773+
overrides: <Type, Generator>{
1774+
PlistParser: () => testPlistParser,
1775+
FileSystem: () => fileSystem,
1776+
ProcessManager: () => processManager,
1777+
Logger: () => logger,
1778+
Xcode: () => xcode,
1779+
},
1780+
);
1781+
1782+
testUsingContext(
1783+
'startApp finds the VM Service when the log reader is listened to before startApp',
1784+
() async {
1785+
// `flutter run` listens to the log reader to echo logs before it calls startApp.
1786+
final logProcess = FakeStreamingProcess();
1787+
processManager.addCommand(
1788+
FakeCommand(
1789+
command: logStreamCommand,
1790+
onRun: (_) => logProcess.write('Filtering the log data using "type == 1024"\n'),
1791+
process: logProcess,
1792+
),
1793+
);
1794+
simControl = FakeSimControl(
1795+
onLaunch: () {
1796+
logProcess.write(
1797+
'"eventMessage" : "The Dart VM service is listening on http://127.0.0.1:1234/abcdef/"\n',
1798+
);
1799+
},
1800+
);
1801+
final IOSSimulator device = buildDevice();
1802+
final IOSApp package = buildPackage();
1803+
final echoedLines = <String>[];
1804+
device.getLogReader(app: package).logLines.listen(echoedLines.add);
1805+
1806+
final LaunchResult result = await device.startApp(
1807+
package,
1808+
prebuiltApplication: true,
1809+
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
1810+
);
1811+
1812+
expect(result.vmServiceUri, Uri.parse('http://127.0.0.1:1234/abcdef/'));
1813+
expect(echoedLines, <String>[
1814+
'The Dart VM service is listening on http://127.0.0.1:1234/abcdef/',
1815+
]);
1816+
expect(processManager, hasNoRemainingExpectations);
1817+
},
1818+
overrides: <Type, Generator>{
1819+
PlistParser: () => testPlistParser,
1820+
FileSystem: () => fileSystem,
1821+
ProcessManager: () => processManager,
1822+
Logger: () => logger,
1823+
Xcode: () => xcode,
1824+
},
1825+
);
1826+
1827+
testUsingContext(
1828+
'startApp warns and keeps waiting if the log stream is slow to become ready',
1829+
() {
1830+
fakeAsync((FakeAsync async) {
1831+
final logProcess = FakeStreamingProcess();
1832+
processManager.addCommand(FakeCommand(command: logStreamCommand, process: logProcess));
1833+
1834+
buildDevice().startApp(
1835+
buildPackage(),
1836+
prebuiltApplication: true,
1837+
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
1838+
);
1839+
async.elapse(const Duration(seconds: 30));
1840+
expect(
1841+
logger.errorText,
1842+
contains('The iOS simulator log stream did not start after 30 seconds.'),
1843+
);
1844+
expect(simControl.requests, isEmpty);
1845+
1846+
logProcess.write('Filtering the log data using "type == 1024"\n');
1847+
async.flushMicrotasks();
1848+
expect(simControl.requests, hasLength(1));
1849+
expect(processManager, hasNoRemainingExpectations);
1850+
});
1851+
},
1852+
overrides: <Type, Generator>{
1853+
PlistParser: () => testPlistParser,
1854+
FileSystem: () => fileSystem,
1855+
ProcessManager: () => processManager,
1856+
Logger: () => logger,
1857+
Xcode: () => xcode,
1858+
},
1859+
);
1860+
});
16821861
});
16831862

16841863
group('IOSDevice.isSupportedForProject', () {
@@ -1797,14 +1976,19 @@ class FakeIosProject extends Fake implements IosProject {
17971976
}
17981977

17991978
class FakeSimControl extends Fake implements SimControl {
1979+
FakeSimControl({this.onLaunch});
1980+
18001981
final requests = <LaunchRequest>[];
18011982

1983+
final void Function()? onLaunch;
1984+
18021985
@override
18031986
Future<RunResult> launch(
18041987
String deviceId,
18051988
String appIdentifier, [
18061989
List<String>? launchArgs,
18071990
]) async {
1991+
onLaunch?.call();
18081992
requests.add(LaunchRequest(appIdentifier, launchArgs));
18091993
return RunResult(ProcessResult(0, 0, '', ''), <String>['test']);
18101994
}
@@ -1815,6 +1999,18 @@ class FakeSimControl extends Fake implements SimControl {
18151999
}
18162000
}
18172001

2002+
/// A [FakeProcess] that keeps running, and whose stdout is written by the test.
2003+
class FakeStreamingProcess extends FakeProcess {
2004+
FakeStreamingProcess() : super(completer: Completer<void>());
2005+
2006+
final _stdoutController = StreamController<List<int>>();
2007+
2008+
@override
2009+
Stream<List<int>> get stdout => _stdoutController.stream;
2010+
2011+
void write(String output) => _stdoutController.add(utf8.encode(output));
2012+
}
2013+
18182014
class LaunchRequest {
18192015
const LaunchRequest(this.appIdentifier, this.launchArgs);
18202016

0 commit comments

Comments
 (0)