Skip to content

Commit 797e08d

Browse files
gabrimaticbkonyi
andauthored
[flutter_tools] Report when the browser disconnects during a web test run (flutter#192198)
Fixes flutter#191920 When the browser closes its connection to the test host during `flutter test --platform chrome`, unfinished suites are reported as `did not complete` without a diagnostic explaining the lost connection. The issue includes a deterministic renderer-kill reproduction confirmed on master. The WebSocket can close before the browser's process exit notification arrives. `BrowserManager.close()` sets `_closed`, and `Chromium.close()` sets `_didClose`, so cleanup can suppress the existing process-exit diagnostics. A renderer or page disconnect can also leave the browser process alive until the tool closes it. This change does two things when the browser disconnects before the manager initiates shutdown: - Reports the disconnect before cleanup starts, so the run explains why its suites did not complete. - Records the unexpected disconnect and keeps the manager's exit-code callback active after cleanup starts. Cleanup begins immediately, without a grace-period timeout. The exit-code message describes an exit after disconnection, rather than claiming that a cleanup-induced exit was the original crash. Intentional shutdown remains silent. ## Tests Six `BrowserManager` regressions in `test/commands.shard/hermetic/flutter_web_platform_test.dart` cover unexpected disconnection, immediate cleanup, exit notifications arriving after cleanup starts, the process exiting before the connection closes, intentional shutdown, and normal cleanup without a false crash diagnostic. All 43 tests across that file and `test/web.shard/chrome_test.dart` pass. Formatting, analysis of the changed files, and `git diff --check` pass. Also verified with real Chrome using the tool from source: normal completion, an ordinary assertion failure, a renderer kill, and a browser-process kill. Neither normal completion nor the assertion failure produces a false disconnect diagnostic. Both kill paths report the disconnect and clean up the test browser; the process-kill path reports exit code -9. Both kill paths reproduce the missing disconnect diagnostic on unpatched master. The broader Flutter Tools run attempted 340 test files. It found 16 failures that also reproduce on unpatched master, in widget-preview path normalization, native-assets filesystem-guard handling, and the workspace-member tests. A native-assets batch also hung and was stopped. The full suite is not green; the browser-related tests are. ## 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. - [ ] All existing and new tests are passing. Browser-related tests pass; the broader failures reproduce on unpatched master as noted above. <!-- 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/CONTRIBUTING.ai.md [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/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Ben Konyi <[email protected]>
1 parent bf04f8c commit 797e08d

2 files changed

Lines changed: 227 additions & 2 deletions

File tree

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:typed_data';
77

88
import 'package:async/async.dart';
99
import 'package:http_multi_server/http_multi_server.dart';
10+
import 'package:meta/meta.dart';
1011
import 'package:mime/mime.dart' as mime;
1112
import 'package:package_config/package_config.dart';
1213
import 'package:pool/pool.dart';
@@ -809,7 +810,14 @@ class BrowserManager {
809810

810811
unawaited(
811812
_browser.onExit.then((int exitCode) {
812-
if (!_closed) {
813+
if (_unexpectedDisconnect) {
814+
if (exitCode != 0) {
815+
_logger.printError(
816+
'Chrome browser process (PID: ${_browser.pid}) '
817+
'exited with code $exitCode after disconnecting from the test host.',
818+
);
819+
}
820+
} else if (!_closed) {
813821
_logger.printError(
814822
'Chrome browser process (PID: ${_browser.pid}) '
815823
'exited unexpectedly with code $exitCode during test execution.',
@@ -857,9 +865,20 @@ class BrowserManager {
857865
);
858866

859867
_environment = _loadBrowserEnvironment();
860-
_channel.stream.listen(_onMessage, onDone: close);
868+
_channel.stream.listen(_onMessage, onDone: _onDisconnect);
861869
}
862870

871+
/// Creates a manager for an already-running [browser] connected over
872+
/// [webSocket].
873+
@visibleForTesting
874+
factory BrowserManager.test(
875+
Chromium browser,
876+
Runtime runtime,
877+
WebSocketChannel webSocket,
878+
Logger logger,
879+
SystemClock systemClock,
880+
) = BrowserManager._;
881+
863882
/// The browser instance that this is connected to via [_channel].
864883
final Chromium _browser;
865884
final Runtime _runtime;
@@ -883,6 +902,9 @@ class BrowserManager {
883902
/// Whether the channel to the browser has closed.
884903
var _closed = false;
885904

905+
/// Whether the browser disconnected before this manager started closing it.
906+
var _unexpectedDisconnect = false;
907+
886908
/// The completer for [_BrowserEnvironment.displayPause].
887909
///
888910
/// This will be `null` as long as the browser isn't displaying a pause
@@ -1200,6 +1222,25 @@ class BrowserManager {
12001222
return report.toString();
12011223
}
12021224

1225+
/// Handles the browser closing its end of the channel.
1226+
///
1227+
/// Report an unexpected disconnect before cleanup starts. Keep reporting the
1228+
/// process exit code after cleanup starts, because the exit notification can
1229+
/// arrive after the connection closes. This does not imply that the process
1230+
/// crashed: cleanup may have closed a browser whose page or renderer died.
1231+
Future<void> _onDisconnect() {
1232+
if (!_closed) {
1233+
_unexpectedDisconnect = true;
1234+
_logger.printError(
1235+
'The ${_runtime.name} browser closed its connection to the test host '
1236+
'unexpectedly, so any unfinished test suites are reported as '
1237+
'"did not complete". The browser process, page, or renderer may have '
1238+
'crashed or been terminated, for example after running out of memory.',
1239+
);
1240+
}
1241+
return close();
1242+
}
1243+
12031244
/// Closes the manager and releases any resources it owns, including closing
12041245
/// the browser.
12051246
Future<void> close() {

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

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:async';
56
import 'dart:convert';
67

8+
import 'package:fake_async/fake_async.dart';
79
import 'package:file/file.dart';
810
import 'package:file/memory.dart';
911
import 'package:flutter_tools/src/artifacts.dart';
12+
import 'package:flutter_tools/src/base/io.dart';
1013
import 'package:flutter_tools/src/base/logger.dart';
1114
import 'package:flutter_tools/src/base/platform.dart';
15+
import 'package:flutter_tools/src/base/time.dart';
1216
import 'package:flutter_tools/src/build_info.dart';
1317
import 'package:flutter_tools/src/isolated/mustache_template.dart';
1418
import 'package:flutter_tools/src/project.dart';
@@ -18,7 +22,10 @@ import 'package:flutter_tools/src/web/compile.dart';
1822
import 'package:flutter_tools/src/web/memory_fs.dart';
1923
import 'package:flutter_tools/src/web/module_metadata.dart';
2024
import 'package:shelf/shelf.dart' as shelf;
25+
import 'package:stream_channel/stream_channel.dart';
2126
import 'package:test_core/backend.dart';
27+
import 'package:web_socket_channel/web_socket_channel.dart';
28+
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart' hide StackTrace;
2229

2330
import '../../src/common.dart';
2431
import '../../src/context.dart';
@@ -58,6 +65,53 @@ class _RecordingChromiumLauncher extends ChromiumLauncher {
5865
}
5966
}
6067

68+
/// A [WebSocketSink] that forwards to a plain [StreamSink].
69+
class FakeWebSocketSink implements WebSocketSink {
70+
FakeWebSocketSink(this._sink);
71+
72+
final StreamSink<Object?> _sink;
73+
74+
@override
75+
void add(Object? data) => _sink.add(data);
76+
77+
@override
78+
void addError(Object error, [StackTrace? stackTrace]) => _sink.addError(error, stackTrace);
79+
80+
@override
81+
Future<void> addStream(Stream<Object?> stream) => _sink.addStream(stream);
82+
83+
@override
84+
Future<void> close([int? closeCode, String? closeReason]) => _sink.close();
85+
86+
@override
87+
Future<void> get done => _sink.done;
88+
}
89+
90+
/// A [WebSocketChannel] whose incoming stream is driven by the test.
91+
class FakeWebSocketChannel extends StreamChannelMixin<Object?> implements WebSocketChannel {
92+
FakeWebSocketChannel(this.controller) : sink = FakeWebSocketSink(controller.foreign.sink);
93+
94+
final StreamChannelController<Object?> controller;
95+
96+
@override
97+
Stream<Object?> get stream => controller.foreign.stream;
98+
99+
@override
100+
final WebSocketSink sink;
101+
102+
@override
103+
int? get closeCode => null;
104+
105+
@override
106+
String? get closeReason => null;
107+
108+
@override
109+
String? get protocol => null;
110+
111+
@override
112+
Future<void> get ready async {}
113+
}
114+
61115
class FakeServer implements shelf.Server {
62116
shelf.Handler? mountedHandler;
63117

@@ -309,4 +363,134 @@ void main() {
309363
Logger: () => logger,
310364
},
311365
);
366+
367+
BrowserManager createBrowserManager(
368+
StreamChannelController<Object?> controller,
369+
Process process,
370+
) {
371+
final chromiumLauncher = ChromiumLauncher(
372+
fileSystem: fileSystem,
373+
platform: platform,
374+
processManager: processManager,
375+
operatingSystemUtils: operatingSystemUtils,
376+
browserFinder: (Platform platform, FileSystem fileSystem) => 'chrome',
377+
logger: logger,
378+
);
379+
final chromium = Chromium(
380+
0,
381+
ChromeConnection('localhost', 1234),
382+
chromiumLauncher: chromiumLauncher,
383+
process: process,
384+
logger: logger,
385+
);
386+
return BrowserManager.test(
387+
chromium,
388+
Runtime.chrome,
389+
FakeWebSocketChannel(controller),
390+
logger,
391+
const SystemClock(),
392+
);
393+
}
394+
395+
testWithoutContext('BrowserManager reports a disconnect and starts cleanup at once', () {
396+
// Regression test for https://github.com/flutter/flutter/issues/191920.
397+
FakeAsync().run((FakeAsync time) {
398+
logger = BufferLogger.test(verbose: true);
399+
final exitCode = Completer<int>();
400+
final controller = StreamChannelController<Object?>();
401+
createBrowserManager(controller, FakeProcess(exitCode: exitCode.future));
402+
403+
controller.local.sink.close();
404+
time.flushMicrotasks();
405+
406+
expect(logger.errorText, contains('closed its connection to the test host unexpectedly'));
407+
expect(logger.traceText, contains('Shutting down Chromium.'));
408+
409+
exitCode.complete(0);
410+
time.flushMicrotasks();
411+
});
412+
});
413+
414+
testWithoutContext('BrowserManager reports an exit code delivered after cleanup starts', () {
415+
// Regression test for https://github.com/flutter/flutter/issues/191920.
416+
FakeAsync().run((FakeAsync time) {
417+
final exitCode = Completer<int>();
418+
final controller = StreamChannelController<Object?>();
419+
createBrowserManager(controller, FakeProcess(exitCode: exitCode.future));
420+
421+
// A dying process takes its connection with it, and the connection is
422+
// seen to close before the exit code arrives.
423+
controller.local.sink.close();
424+
time.flushMicrotasks();
425+
// The exit notification can be delivered after cleanup starts. It must
426+
// not depend on an arbitrary grace period after the channel closes.
427+
time.elapse(const Duration(seconds: 2));
428+
exitCode.complete(-9);
429+
time.flushMicrotasks();
430+
431+
expect(logger.errorText, contains('closed its connection to the test host unexpectedly'));
432+
expect(
433+
logger.errorText,
434+
contains('exited with code -9 after disconnecting from the test host'),
435+
);
436+
});
437+
});
438+
439+
testWithoutContext('BrowserManager reports a process exit before the connection closes', () {
440+
FakeAsync().run((FakeAsync time) {
441+
final exitCode = Completer<int>();
442+
final controller = StreamChannelController<Object?>();
443+
createBrowserManager(controller, FakeProcess(exitCode: exitCode.future));
444+
445+
exitCode.complete(-9);
446+
time.flushMicrotasks();
447+
448+
expect(logger.errorText, contains('exited unexpectedly with code -9'));
449+
450+
controller.local.sink.close();
451+
time.flushMicrotasks();
452+
453+
expect(logger.errorText, contains('closed its connection to the test host unexpectedly'));
454+
});
455+
});
456+
457+
testWithoutContext(
458+
'BrowserManager does not diagnose an intentional shutdown as a disconnect',
459+
() {
460+
FakeAsync().run((FakeAsync time) {
461+
final exitCode = Completer<int>();
462+
final controller = StreamChannelController<Object?>();
463+
final BrowserManager manager = createBrowserManager(
464+
controller,
465+
FakeProcess(exitCode: exitCode.future),
466+
);
467+
468+
manager.close();
469+
controller.local.sink.close();
470+
time.flushMicrotasks();
471+
exitCode.complete(-15);
472+
time.flushMicrotasks();
473+
474+
expect(logger.errorText, isEmpty);
475+
});
476+
},
477+
);
478+
479+
testWithoutContext('BrowserManager does not report a clean exit after cleanup', () {
480+
FakeAsync().run((FakeAsync time) {
481+
final exitCode = Completer<int>();
482+
final controller = StreamChannelController<Object?>();
483+
createBrowserManager(controller, FakeProcess(exitCode: exitCode.future));
484+
485+
controller.local.sink.close();
486+
time.flushMicrotasks();
487+
// A healthy browser may exit normally when closed after its page dies.
488+
exitCode.complete(0);
489+
time.flushMicrotasks();
490+
491+
expect(logger.errorText, contains('closed its connection to the test host unexpectedly'));
492+
expect(logger.errorText, isNot(contains('exited with code')));
493+
expect(logger.errorText, isNot(contains('exited unexpectedly')));
494+
});
495+
});
312496
}

0 commit comments

Comments
 (0)