Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flutter_tools] do not add events to closed sink in throttle transform #66468

Merged
merged 1 commit into from Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/flutter_tools/lib/src/protocol_discovery.dart
Expand Up @@ -234,6 +234,7 @@ StreamTransformer<S, S> _throttle<S>({
S latestLine;
int lastExecution;
Future<void> throttleFuture;
bool done = false;

return StreamTransformer<S, S>
.fromHandlers(
Expand All @@ -249,14 +250,20 @@ StreamTransformer<S, S> _throttle<S>({
final int nextExecutionTime = isFirstMessage || remainingTime > waitDuration.inMilliseconds
? 0
: waitDuration.inMilliseconds - remainingTime;

throttleFuture ??= Future<void>
.delayed(Duration(milliseconds: nextExecutionTime))
.whenComplete(() {
if (done) {
return;
}
sink.add(latestLine);
throttleFuture = null;
lastExecution = DateTime.now().millisecondsSinceEpoch;
});
},
handleDone: (EventSink<S> sink) {
done = true;
sink.close();
}
);
}
Expand Up @@ -195,6 +195,18 @@ void main() {
expect('$uri', 'http://127.0.0.1:12345/PTwjm8Ii8qg=/');
});

testUsingContext('protocol discovery does not crash if the log reader is closed while delaying', () async {
initialize(devicePort: 12346, throttleDuration: const Duration(milliseconds: 10));
final Future<List<Uri>> results = discoverer.uris.toList();
logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12346/PTwjm8Ii8qg=/');
logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12346/PTwjm8Ii8qg=/');
await logReader.dispose();

// Give time for throttle to finish.
await Future<void>.delayed(const Duration(milliseconds: 11));
expect(await results, isEmpty);
});

testUsingContext('uris in the stream are throttled', () async {
const Duration kThrottleDuration = Duration(milliseconds: 10);

Expand Down