Skip to content

Commit 3d2c811

Browse files
authored
[flutter_tools] Handle all DartDevelopmentServiceExceptions gracefully in resident web runner (flutter#191183)
## Description This PR ensures that `ResidentWebRunner` catches all variants of `DartDevelopmentServiceException` during startup, rather than just `failedToStartError`. When DDS fails to establish a WebSocket connection or upgrade the connection to DWDS (e.g., if the target browser exits prematurely or rejects the handshake), `package:dds` throws `DartDevelopmentServiceException.connectionIssue(...)` with `errorCode = DartDevelopmentServiceException.connectionError` (Code 3), rather than `failedToStartError` (Code 2). Previously, `ResidentWebRunner` only caught `failedToStartError` and rethrew all other `DartDevelopmentServiceException`s, which caused the CLI to crash with an unhandled top-level exception. With this change, we catch all `DartDevelopmentServiceException`s, log the error details with stack trace, perform necessary cleanup, and gracefully exit via `throwToolExit(kExitMessage)`. ## Related Issues Fixes flutter#191179 ## Tests Added a new unit test in `resident_web_runner_test.dart`: - `Does not crash if DDS fails to upgrade WebSocket during startup`
1 parent 7c08f43 commit 3d2c811

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎packages/flutter_tools/lib/src/isolated/resident_web_runner.dart‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,14 @@ class ResidentWebRunner extends ResidentRunner {
406406
stackTrace: stackTrace,
407407
);
408408
throwToolExit('Failed to connect to the web debug service.');
409-
} on DartDevelopmentServiceException catch (error) {
409+
} on DartDevelopmentServiceException catch (error, stackTrace) {
410410
// The application may have started shutting down before DDS was able to finish establishing
411411
// its connection to DWDS. Don't treat this as an unhandled exception.
412412
appFailedToStart();
413-
if (error.errorCode == DartDevelopmentServiceException.failedToStartError) {
414-
throwToolExit(kExitMessage);
413+
if (error.errorCode != DartDevelopmentServiceException.failedToStartError) {
414+
_logger.printError(error.message, stackTrace: stackTrace);
415415
}
416-
rethrow;
416+
throwToolExit(kExitMessage);
417417
} on Exception {
418418
appFailedToStart();
419419
rethrow;

‎packages/flutter_tools/test/general.shard/resident_web_runner_test.dart‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,36 @@ name: my_app
275275
'Does not crash if the application exits during DDS startup',
276276
() async {
277277
// Regression test for https://github.com/flutter/flutter/issues/178151
278-
final ResidentRunner residentWebRunner = setUpResidentRunner(flutterDevice);
278+
final logger = BufferLogger.test();
279+
final ResidentRunner residentWebRunner = setUpResidentRunner(flutterDevice, logger: logger);
279280
fakeVmServiceHost = FakeVmServiceHost(requests: kAttachExpectations.toList());
280281
setupMocks();
281282
webDevFS.exception = DartDevelopmentServiceException.failedToStart();
282283

283284
await expectLater(residentWebRunner.run(), throwsToolExit());
285+
expect(logger.errorText, isEmpty);
286+
},
287+
overrides: <Type, Generator>{
288+
FileSystem: () => fileSystem,
289+
ProcessManager: () => processManager,
290+
Pub: ThrowingPub.new,
291+
},
292+
);
293+
294+
testUsingContext(
295+
'Does not crash if DDS fails to upgrade WebSocket during startup',
296+
() async {
297+
final logger = BufferLogger.test();
298+
final ResidentRunner residentWebRunner = setUpResidentRunner(flutterDevice, logger: logger);
299+
fakeVmServiceHost = FakeVmServiceHost(requests: kAttachExpectations.toList());
300+
setupMocks();
301+
const errorMessage =
302+
'WebSocketChannelException: WebSocketException: Connection to '
303+
"'http://127.0.0.1:62932/9fVOXxsamo0=/ws#' was not upgraded to websocket";
304+
webDevFS.exception = DartDevelopmentServiceException.connectionIssue(errorMessage);
305+
306+
await expectLater(residentWebRunner.run(), throwsToolExit());
307+
expect(logger.errorText, contains(errorMessage));
284308
},
285309
overrides: <Type, Generator>{
286310
FileSystem: () => fileSystem,

0 commit comments

Comments
 (0)