Skip to content

Commit 2132595

Browse files
[flutter_tools] Add retry and timeout to getFlutterWidgetPreviews in LspPreviewDetector (flutter#190929)
Addresses flakiness in `tool_integration_tests` by adding a 5-second timeout and up to 5 retries (with 200ms delay) when calling `dtd.getFlutterWidgetPreviews()`. This handles transient crashes/hangs of the Analysis Server during test execution. Also improves diagnostics in `widget_preview_test_helpers.dart` by closing the stdout stream controller `onDone` and logging the exit code of the `widget-preview` process. Aborts retries early if the detector is disposed or the tool is shutting down to prevent hangs. Fixes flutter#189496 ## 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 documentation (doc comments with `///`). - [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. <!-- 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 [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: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 90cf7be commit 2132595

2 files changed

Lines changed: 54 additions & 22 deletions

File tree

‎packages/flutter_tools/lib/src/widget_preview/lsp_preview_detector.dart‎

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class LspPreviewDetector {
136136
}
137137

138138
Future<AnalysisServer> launchAnalysisServer() async {
139+
final String? protocolTrafficLog = platform.environment['FLUTTER_LSP_TRAFFIC_LOG'];
140+
if (protocolTrafficLog != null) {
141+
logger.printTrace('LSP Traffic Log path from env: $protocolTrafficLog');
142+
}
139143
final analysisServer = AnalysisServer(
140144
artifacts.getArtifactPath(Artifact.engineDartSdkPath),
141145
[projectRoot.path],
@@ -145,6 +149,7 @@ class LspPreviewDetector {
145149
processManager: processManager,
146150
terminal: terminal,
147151
suppressAnalytics: suppressAnalytics,
152+
protocolTrafficLog: protocolTrafficLog,
148153
);
149154
return analysisServer;
150155
}
@@ -186,20 +191,38 @@ class LspPreviewDetector {
186191
return;
187192
}
188193
await _analysisServer?.waitForAnalysis();
189-
try {
190-
final FlutterWidgetPreviews result = await dtd.getFlutterWidgetPreviews();
191-
onChangeDetected(result);
192-
} catch (e) {
194+
FlutterWidgetPreviews? result;
195+
var retries = 5;
196+
while (retries > 0) {
193197
if (_disposed || shutdownHooks.isShuttingDown) {
194-
logger.printTrace('Failed to get widget previews during shutdown: $e');
195-
} else if (e is StateError || e is Exception) {
196-
logger.printWarning(
197-
'Lost connection to the Dart Tooling Daemon (DTD). '
198-
'Live preview updates are paused. Details: $e',
199-
);
200-
} else {
201-
rethrow;
198+
break;
199+
}
200+
try {
201+
result = await dtd.getFlutterWidgetPreviews().timeout(const Duration(seconds: 5));
202+
break;
203+
} catch (e) {
204+
retries--;
205+
if (retries == 0) {
206+
if (_disposed || shutdownHooks.isShuttingDown) {
207+
logger.printTrace('Failed to get widget previews during shutdown: $e');
208+
} else if (e is StateError || e is Exception) {
209+
logger.printWarning(
210+
'Lost connection to the Dart Tooling Daemon (DTD). '
211+
'Live preview updates are paused. Details: $e',
212+
);
213+
} else {
214+
rethrow;
215+
}
216+
} else {
217+
logger.printTrace(
218+
'Failed to get widget previews, retrying in 200ms... ($retries retries left). Error: $e',
219+
);
220+
await Future<void>.delayed(const Duration(milliseconds: 200));
221+
}
202222
}
203223
}
224+
if (result != null) {
225+
onChangeDetected(result);
226+
}
204227
}
205228
}

‎packages/flutter_tools/test/integration.shard/widget_preview_test_helpers.dart‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,23 @@ Future<Stream<String>> startWidgetPreview({
7676
});
7777

7878
final controller = StreamController<String>.broadcast();
79-
process.stdout.transform(utf8.decoder).transform(const LineSplitter()).listen((String msg) {
80-
// ignore: avoid_print
81-
print('[stdout] $msg');
82-
if (!controller.isClosed) {
83-
controller.add(msg);
84-
}
85-
});
79+
process.stdout
80+
.transform(utf8.decoder)
81+
.transform(const LineSplitter())
82+
.listen(
83+
(String msg) {
84+
// ignore: avoid_print
85+
print('[stdout] $msg');
86+
if (!controller.isClosed) {
87+
controller.add(msg);
88+
}
89+
},
90+
onDone: () {
91+
if (!controller.isClosed) {
92+
controller.close();
93+
}
94+
},
95+
);
8696

8797
process.stderr.transform(utf8.decoder).transform(const LineSplitter()).listen((String msg) {
8898
// ignore: avoid_print
@@ -91,9 +101,8 @@ Future<Stream<String>> startWidgetPreview({
91101

92102
unawaited(
93103
process.exitCode.then((int exitCode) {
94-
if (!controller.isClosed) {
95-
controller.close();
96-
}
104+
// ignore: avoid_print
105+
print('widget-preview process exited with code $exitCode');
97106
}),
98107
);
99108

0 commit comments

Comments
 (0)